Skip to content

Commit 24a4142

Browse files
committed
BSON: Fixed array serialization by adding increasing integral names to the array elements
1 parent ad11b6c commit 24a4142

5 files changed

Lines changed: 3394 additions & 17 deletions

File tree

include/nlohmann/detail/output/binary_writer.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,10 @@ class binary_writer
837837
static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
838838
{
839839
std::size_t embedded_document_size = 0ul;
840-
840+
std::size_t i = 0ull;
841841
for (const auto& el : value)
842842
{
843-
embedded_document_size += calc_bson_element_size("", el);
843+
embedded_document_size += calc_bson_element_size(std::to_string(i++), el);
844844
}
845845

846846
return sizeof(std::int32_t) + embedded_document_size + 1ul;
@@ -854,9 +854,10 @@ class binary_writer
854854
write_bson_entry_header(name, 0x04); // array
855855
write_number<std::int32_t, true>(calc_bson_array_size(value));
856856

857+
std::size_t i = 0ull;
857858
for (const auto& el : value)
858859
{
859-
write_bson_element("", el);
860+
write_bson_element(std::to_string(i++), el);
860861
}
861862

862863
oa->write_character(static_cast<CharType>(0x00));

single_include/nlohmann/json.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8994,10 +8994,10 @@ class binary_writer
89948994
static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
89958995
{
89968996
std::size_t embedded_document_size = 0ul;
8997-
8997+
std::size_t i = 0ull;
89988998
for (const auto& el : value)
89998999
{
9000-
embedded_document_size += calc_bson_element_size("", el);
9000+
embedded_document_size += calc_bson_element_size(std::to_string(i++), el);
90019001
}
90029002

90039003
return sizeof(std::int32_t) + embedded_document_size + 1ul;
@@ -9011,9 +9011,10 @@ class binary_writer
90119011
write_bson_entry_header(name, 0x04); // array
90129012
write_number<std::int32_t, true>(calc_bson_array_size(value));
90139013

9014+
std::size_t i = 0ull;
90149015
for (const auto& el : value)
90159016
{
9016-
write_bson_element("", el);
9017+
write_bson_element(std::to_string(i++), el);
90179018
}
90189019

90199020
oa->write_character(static_cast<CharType>(0x00));

test/data/json_testsuite/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
https://code.google.com/p/json-test-suite/downloads/detail?name=sample.zip&can=2&q=
66

7+
The file sample_cstr_keys.json has been copied from sample.json and modified by removing all U+0000 code points from all the keys.
8+
79
## License
810

911
Apache License Version 2.0

0 commit comments

Comments
 (0)