BSON: support objects with objects as members
This commit is contained in:
parent
83b427ad67
commit
5ce7d6bdd7
4 changed files with 77 additions and 4 deletions
|
@ -231,10 +231,17 @@ class binary_reader
|
|||
sax->null();
|
||||
}
|
||||
break;
|
||||
case 0x03: // object
|
||||
{
|
||||
string_t key;
|
||||
get_bson_cstr(key);
|
||||
sax->key(key);
|
||||
parse_bson_internal();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
get();
|
||||
const auto result = sax->end_object();
|
||||
|
||||
return result;
|
||||
|
|
|
@ -775,6 +775,18 @@ class binary_writer
|
|||
}
|
||||
}
|
||||
|
||||
std::size_t write_bson_object_internal(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
oa->write_character(static_cast<CharType>(0x03)); // object
|
||||
oa->write_characters(
|
||||
reinterpret_cast<const CharType*>(name.c_str()),
|
||||
name.size() + 1u);
|
||||
|
||||
auto const embedded_document_size = write_bson_object(j);
|
||||
|
||||
return /*id*/ 1ul + name.size() + 1ul + embedded_document_size;
|
||||
}
|
||||
|
||||
std::size_t write_bson_object_entry(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
switch (j.type())
|
||||
|
@ -782,6 +794,8 @@ class binary_writer
|
|||
default:
|
||||
JSON_THROW(type_error::create(317, "JSON value cannot be serialized to requested format"));
|
||||
break;
|
||||
case value_t::object:
|
||||
return write_bson_object_internal(name, j);
|
||||
case value_t::boolean:
|
||||
return write_bson_boolean(name, j);
|
||||
case value_t::number_float:
|
||||
|
@ -803,7 +817,7 @@ class binary_writer
|
|||
@param[in] j JSON value to serialize
|
||||
@pre j.type() == value_t::object
|
||||
*/
|
||||
void write_bson_object(const BasicJsonType& j)
|
||||
std::size_t write_bson_object(const BasicJsonType& j)
|
||||
{
|
||||
assert(j.type() == value_t::object);
|
||||
auto document_size_offset = oa->reserve_characters(4ul);
|
||||
|
@ -816,6 +830,7 @@ class binary_writer
|
|||
|
||||
oa->write_character(static_cast<CharType>(0x00));
|
||||
write_number_little_endian_at(document_size_offset, document_size);
|
||||
return document_size;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
@ -6215,10 +6215,17 @@ class binary_reader
|
|||
sax->null();
|
||||
}
|
||||
break;
|
||||
case 0x03: // object
|
||||
{
|
||||
string_t key;
|
||||
get_bson_cstr(key);
|
||||
sax->key(key);
|
||||
parse_bson_internal();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
get();
|
||||
const auto result = sax->end_object();
|
||||
|
||||
return result;
|
||||
|
@ -8609,6 +8616,18 @@ class binary_writer
|
|||
}
|
||||
}
|
||||
|
||||
std::size_t write_bson_object_internal(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
oa->write_character(static_cast<CharType>(0x03)); // object
|
||||
oa->write_characters(
|
||||
reinterpret_cast<const CharType*>(name.c_str()),
|
||||
name.size() + 1u);
|
||||
|
||||
auto const embedded_document_size = write_bson_object(j);
|
||||
|
||||
return /*id*/ 1ul + name.size() + 1ul + embedded_document_size;
|
||||
}
|
||||
|
||||
std::size_t write_bson_object_entry(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
switch (j.type())
|
||||
|
@ -8616,6 +8635,8 @@ class binary_writer
|
|||
default:
|
||||
JSON_THROW(type_error::create(317, "JSON value cannot be serialized to requested format"));
|
||||
break;
|
||||
case value_t::object:
|
||||
return write_bson_object_internal(name, j);
|
||||
case value_t::boolean:
|
||||
return write_bson_boolean(name, j);
|
||||
case value_t::number_float:
|
||||
|
@ -8637,7 +8658,7 @@ class binary_writer
|
|||
@param[in] j JSON value to serialize
|
||||
@pre j.type() == value_t::object
|
||||
*/
|
||||
void write_bson_object(const BasicJsonType& j)
|
||||
std::size_t write_bson_object(const BasicJsonType& j)
|
||||
{
|
||||
assert(j.type() == value_t::object);
|
||||
auto document_size_offset = oa->reserve_characters(4ul);
|
||||
|
@ -8650,6 +8671,7 @@ class binary_writer
|
|||
|
||||
oa->write_character(static_cast<CharType>(0x00));
|
||||
write_number_little_endian_at(document_size_offset, document_size);
|
||||
return document_size;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
@ -351,5 +351,34 @@ TEST_CASE("BSON")
|
|||
CHECK(json::from_bson(result) == j);
|
||||
CHECK(json::from_bson(result, true, false) == j);
|
||||
}
|
||||
|
||||
SECTION("non-empty object with object member")
|
||||
{
|
||||
// directly encoding uint64 is not supported in bson (only for timestamp values)
|
||||
json j =
|
||||
{
|
||||
{ "entry", json::object() }
|
||||
};
|
||||
|
||||
std::vector<uint8_t> expected =
|
||||
{
|
||||
0x11, 0x00, 0x00, 0x00, // size (little endian)
|
||||
0x03, /// entry: embedded document
|
||||
'e', 'n', 't', 'r', 'y', '\x00',
|
||||
|
||||
0x05, 0x00, 0x00, 0x00, // size (little endian)
|
||||
// no entries
|
||||
0x00, // end marker (embedded document)
|
||||
|
||||
0x00 // end marker
|
||||
};
|
||||
|
||||
const auto result = json::to_bson(j);
|
||||
CHECK(result == expected);
|
||||
|
||||
// roundtrip
|
||||
CHECK(json::from_bson(result) == j);
|
||||
CHECK(json::from_bson(result, true, false) == j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue