Exemple #1
0
void core::append(const bsoncxx::types::value& value) {
    switch (static_cast<int>(value.type())) {
#define BSONCXX_ENUM(type, val)     \
    case val:                       \
        append(value.get_##type()); \
        break;
#include <bsoncxx/enums/type.hpp>
#undef BSONCXX_ENUM
    }
}
Exemple #2
0
		Mave FromBson(bsoncxx::types::value bson)
		{
			Mave result;
			vector<function<bool()>> continuations;
			auto root = bson;

			while (true)
			{
				switch (bson.type())
				{
					case bsoncxx::type::k_undefined:
					case bsoncxx::type::k_minkey:
					case bsoncxx::type::k_maxkey:
						break;
					case bsoncxx::type::k_null:
						result = nullptr;
						break;
					case bsoncxx::type::k_bool:
						result = bson.get_bool().value;
						break;
					case bsoncxx::type::k_int32:
						result = bson.get_int32().value;
						break;
					case bsoncxx::type::k_int64:
						result = bson.get_int64().value;
						break;
					case bsoncxx::type::k_double:
						result = bson.get_double().value;
						break;
					case bsoncxx::type::k_date:
						result = milliseconds(bson.get_date().value);
						break;
					case bsoncxx::type::k_utf8:
						result = bson.get_utf8().value.to_string();
						break;
					case bsoncxx::type::k_oid:
						result = make_pair(BSON_OID, bson.get_oid().value.to_string());
						break;
					case bsoncxx::type::k_dbpointer:
						result = make_pair(BSON_OID, bson.get_dbpointer().value.to_string());
						break;
					case bsoncxx::type::k_timestamp:
						auto t = bson.get_timestamp();
						result = (long)(((unsigned long long)t.timestamp << 32) | t.increment);
						break;
					case bsoncxx::type::k_binary:
						auto b = bson.get_binary();
						result = string((const char*)b.bytes, b.size);
						break;
					case bsoncxx::type::k_regex:
						result = bson.get_regex().regex.to_string();
						break;
					case bsoncxx::type::k_symbol:
						result = bson.get_symbol().symbol.to_string();
						break;
					case bsoncxx::type::k_code:
						result = bson.get_code().code.to_string();
						break;
					case bsoncxx::type::k_codewscope:
						result = bson.get_codewscope().code.to_string();
						break;
					case bsoncxx::type::k_document:
						continuations.push_back(
							[&
							, m = map<string, Mave>()
							, i = bson.get_document().value.cbegin()
							, e = bson.get_document().value.cend()
							, f = false]() mutable -> bool
						{
							if (f)
							{
								m.insert({ i->key().to_string(), result });
								++i;
							}
							if (i != e)
							{
								bson = i->get_value();
								return f = true;
							}
							result = move(m);
							return false;
						});
						break;
					case bsoncxx::type::k_array:
						continuations.push_back(
							[&
							, v = vector<Mave>()
							, i = bson.get_array().value.cbegin()
							, e = bson.get_array().value.cend()
							, f = false]() mutable -> bool
						{
							if (f)
							{
								v.push_back(result);
								++i;
							}
							if (i != e)
							{
								bson = i->get_value();
								return f = true;
							}
							result = move(v);
							return false;
						});
						break;
					default:
						throw exception("Mave::FromBson(): unsupported type encountered");
				}

				while (true)
				{
					if (continuations.size() == 0)
					{
						return result;
					}
					if (continuations.back()())
					{
						break;
					}
					continuations.pop_back();
				}
			}
		}