BSON: support objects with null members

This commit is contained in:
Julian Becker 2018-09-15 11:38:26 +02:00
parent 6c447de076
commit c5ef023171
4 changed files with 63 additions and 0 deletions

View file

@ -203,6 +203,14 @@ class binary_reader
sax->boolean(static_cast<bool>(get())); sax->boolean(static_cast<bool>(get()));
} }
break; break;
case 0x0A: // null
{
string_t key;
get_bson_cstr(key);
sax->key(key);
sax->null();
}
break;
} }
} }

View file

@ -711,6 +711,16 @@ class binary_writer
return /*id*/ 1ul + name.size() + 1ul + sizeof(std::int32_t) + j.m_value.string->size() + 1ul; return /*id*/ 1ul + name.size() + 1ul + sizeof(std::int32_t) + j.m_value.string->size() + 1ul;
} }
std::size_t write_bson_null(const typename BasicJsonType::string_t& name, const BasicJsonType&)
{
oa->write_character(static_cast<CharType>(0x0A)); // null
oa->write_characters(
reinterpret_cast<const CharType*>(name.c_str()),
name.size() + 1u);
return /*id*/ 1ul + name.size() + 1ul;
}
std::size_t write_bson_object_entry(const typename BasicJsonType::string_t& name, const BasicJsonType& j) std::size_t write_bson_object_entry(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
{ {
switch (j.type()) switch (j.type())
@ -724,6 +734,8 @@ class binary_writer
return write_bson_double(name, j); return write_bson_double(name, j);
case value_t::string: case value_t::string:
return write_bson_string(name, j); return write_bson_string(name, j);
case value_t::null:
return write_bson_null(name, j);
}; };
return 0ul; return 0ul;

View file

@ -6187,6 +6187,14 @@ class binary_reader
sax->boolean(static_cast<bool>(get())); sax->boolean(static_cast<bool>(get()));
} }
break; break;
case 0x0A: // null
{
string_t key;
get_bson_cstr(key);
sax->key(key);
sax->null();
}
break;
} }
} }
@ -8517,6 +8525,16 @@ class binary_writer
return /*id*/ 1ul + name.size() + 1ul + sizeof(std::int32_t) + j.m_value.string->size() + 1ul; return /*id*/ 1ul + name.size() + 1ul + sizeof(std::int32_t) + j.m_value.string->size() + 1ul;
} }
std::size_t write_bson_null(const typename BasicJsonType::string_t& name, const BasicJsonType&)
{
oa->write_character(static_cast<CharType>(0x0A)); // null
oa->write_characters(
reinterpret_cast<const CharType*>(name.c_str()),
name.size() + 1u);
return /*id*/ 1ul + name.size() + 1ul;
}
std::size_t write_bson_object_entry(const typename BasicJsonType::string_t& name, const BasicJsonType& j) std::size_t write_bson_object_entry(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
{ {
switch (j.type()) switch (j.type())
@ -8530,6 +8548,8 @@ class binary_writer
return write_bson_double(name, j); return write_bson_double(name, j);
case value_t::string: case value_t::string:
return write_bson_string(name, j); return write_bson_string(name, j);
case value_t::null:
return write_bson_null(name, j);
}; };
return 0ul; return 0ul;

View file

@ -208,6 +208,29 @@ TEST_CASE("BSON")
CHECK(json::from_bson(result, true, false) == j); CHECK(json::from_bson(result, true, false) == j);
} }
SECTION("non-empty object with null member")
{
json j =
{
{ "entry", nullptr }
};
std::vector<uint8_t> expected =
{
0x0C, 0x00, 0x00, 0x00, // size (little endian)
0x0A, /// entry: null
'e', 'n', 't', 'r', 'y', '\x00',
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);
}
} }
} }