mirror of
https://github.com/nlohmann/json.git
synced 2024-11-24 06:29:03 +08:00
BSON: Support for arrays
This commit is contained in:
parent
120d1d77d4
commit
cf485c2907
@ -157,17 +157,8 @@ class binary_reader
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
bool parse_bson_internal()
|
||||
void parse_bson_entries()
|
||||
{
|
||||
std::int32_t documentSize;
|
||||
get_number_little_endian(documentSize);
|
||||
|
||||
if (not JSON_UNLIKELY(sax->start_object(-1)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
while (auto entry_type = get())
|
||||
{
|
||||
switch (entry_type)
|
||||
@ -239,8 +230,46 @@ class binary_reader
|
||||
parse_bson_internal();
|
||||
}
|
||||
break;
|
||||
case 0x04: // array
|
||||
{
|
||||
string_t key;
|
||||
get_bson_cstr(key);
|
||||
sax->key(key);
|
||||
parse_bson_array();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool parse_bson_array()
|
||||
{
|
||||
std::int32_t documentSize;
|
||||
get_number_little_endian(documentSize);
|
||||
|
||||
if (not JSON_UNLIKELY(sax->start_array(-1)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
parse_bson_entries();
|
||||
|
||||
const auto result = sax->end_array();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool parse_bson_internal()
|
||||
{
|
||||
std::int32_t documentSize;
|
||||
get_number_little_endian(documentSize);
|
||||
|
||||
if (not JSON_UNLIKELY(sax->start_object(-1)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
parse_bson_entries();
|
||||
|
||||
const auto result = sax->end_object();
|
||||
|
||||
|
@ -787,6 +787,28 @@ class binary_writer
|
||||
return /*id*/ 1ul + name.size() + 1ul + embedded_document_size;
|
||||
}
|
||||
|
||||
std::size_t write_bson_array(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
oa->write_character(static_cast<CharType>(0x04)); // object
|
||||
oa->write_characters(
|
||||
reinterpret_cast<const CharType*>(name.c_str()),
|
||||
name.size() + 1u);
|
||||
|
||||
|
||||
auto document_size_offset = oa->reserve_characters(4ul);
|
||||
std::int32_t embedded_document_size = 5ul;
|
||||
|
||||
for (const auto& el : *j.m_value.array)
|
||||
{
|
||||
embedded_document_size += write_bson_object_entry("", el);
|
||||
}
|
||||
|
||||
oa->write_character(static_cast<CharType>(0x00));
|
||||
write_number_little_endian_at(document_size_offset, embedded_document_size);
|
||||
|
||||
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())
|
||||
@ -796,6 +818,8 @@ class binary_writer
|
||||
break;
|
||||
case value_t::object:
|
||||
return write_bson_object_internal(name, j);
|
||||
case value_t::array:
|
||||
return write_bson_array(name, j);
|
||||
case value_t::boolean:
|
||||
return write_bson_boolean(name, j);
|
||||
case value_t::number_float:
|
||||
|
@ -6141,17 +6141,8 @@ class binary_reader
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
bool parse_bson_internal()
|
||||
void parse_bson_entries()
|
||||
{
|
||||
std::int32_t documentSize;
|
||||
get_number_little_endian(documentSize);
|
||||
|
||||
if (not JSON_UNLIKELY(sax->start_object(-1)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
while (auto entry_type = get())
|
||||
{
|
||||
switch (entry_type)
|
||||
@ -6223,8 +6214,46 @@ class binary_reader
|
||||
parse_bson_internal();
|
||||
}
|
||||
break;
|
||||
case 0x04: // array
|
||||
{
|
||||
string_t key;
|
||||
get_bson_cstr(key);
|
||||
sax->key(key);
|
||||
parse_bson_array();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool parse_bson_array()
|
||||
{
|
||||
std::int32_t documentSize;
|
||||
get_number_little_endian(documentSize);
|
||||
|
||||
if (not JSON_UNLIKELY(sax->start_array(-1)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
parse_bson_entries();
|
||||
|
||||
const auto result = sax->end_array();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool parse_bson_internal()
|
||||
{
|
||||
std::int32_t documentSize;
|
||||
get_number_little_endian(documentSize);
|
||||
|
||||
if (not JSON_UNLIKELY(sax->start_object(-1)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
parse_bson_entries();
|
||||
|
||||
const auto result = sax->end_object();
|
||||
|
||||
@ -8628,6 +8657,28 @@ class binary_writer
|
||||
return /*id*/ 1ul + name.size() + 1ul + embedded_document_size;
|
||||
}
|
||||
|
||||
std::size_t write_bson_array(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
oa->write_character(static_cast<CharType>(0x04)); // object
|
||||
oa->write_characters(
|
||||
reinterpret_cast<const CharType*>(name.c_str()),
|
||||
name.size() + 1u);
|
||||
|
||||
|
||||
auto document_size_offset = oa->reserve_characters(4ul);
|
||||
std::int32_t embedded_document_size = 5ul;
|
||||
|
||||
for (const auto& el : *j.m_value.array)
|
||||
{
|
||||
embedded_document_size += write_bson_object_entry("", el);
|
||||
}
|
||||
|
||||
oa->write_character(static_cast<CharType>(0x00));
|
||||
write_number_little_endian_at(document_size_offset, embedded_document_size);
|
||||
|
||||
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())
|
||||
@ -8637,6 +8688,8 @@ class binary_writer
|
||||
break;
|
||||
case value_t::object:
|
||||
return write_bson_object_internal(name, j);
|
||||
case value_t::array:
|
||||
return write_bson_array(name, j);
|
||||
case value_t::boolean:
|
||||
return write_bson_boolean(name, j);
|
||||
case value_t::number_float:
|
||||
|
@ -354,7 +354,6 @@ TEST_CASE("BSON")
|
||||
|
||||
SECTION("non-empty object with object member")
|
||||
{
|
||||
// directly encoding uint64 is not supported in bson (only for timestamp values)
|
||||
json j =
|
||||
{
|
||||
{ "entry", json::object() }
|
||||
@ -381,6 +380,34 @@ TEST_CASE("BSON")
|
||||
CHECK(json::from_bson(result, true, false) == j);
|
||||
}
|
||||
|
||||
SECTION("non-empty object with array member")
|
||||
{
|
||||
json j =
|
||||
{
|
||||
{ "entry", json::array() }
|
||||
};
|
||||
|
||||
std::vector<uint8_t> expected =
|
||||
{
|
||||
0x11, 0x00, 0x00, 0x00, // size (little endian)
|
||||
0x04, /// 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);
|
||||
}
|
||||
|
||||
SECTION("Some more complex document")
|
||||
{
|
||||
// directly encoding uint64 is not supported in bson (only for timestamp values)
|
||||
|
Loading…
Reference in New Issue
Block a user