BSON: allow and discard values and object entries of type value_t::discarded

This commit is contained in:
Julian Becker 2018-10-07 20:08:05 +02:00
parent 062aeaf7b6
commit df0f612d1b
3 changed files with 33 additions and 2 deletions

View file

@ -9,7 +9,6 @@
#include <nlohmann/detail/input/binary_reader.hpp> #include <nlohmann/detail/input/binary_reader.hpp>
#include <nlohmann/detail/output/output_adapters.hpp> #include <nlohmann/detail/output/output_adapters.hpp>
namespace nlohmann namespace nlohmann
{ {
namespace detail namespace detail
@ -864,6 +863,8 @@ class binary_writer
assert(false); assert(false);
return 0ul; return 0ul;
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
case value_t::discarded:
return 0ul;
case value_t::object: case value_t::object:
return header_size + calc_bson_object_size(*j.m_value.object); return header_size + calc_bson_object_size(*j.m_value.object);
case value_t::array: case value_t::array:
@ -898,6 +899,8 @@ class binary_writer
assert(false); assert(false);
return; return;
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
case value_t::discarded:
return;
case value_t::object: case value_t::object:
return write_bson_object_entry(name, *j.m_value.object); return write_bson_object_entry(name, *j.m_value.object);
case value_t::array: case value_t::array:

View file

@ -7853,7 +7853,6 @@ class binary_reader
// #include <nlohmann/detail/output/output_adapters.hpp> // #include <nlohmann/detail/output/output_adapters.hpp>
namespace nlohmann namespace nlohmann
{ {
namespace detail namespace detail
@ -8708,6 +8707,8 @@ class binary_writer
assert(false); assert(false);
return 0ul; return 0ul;
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
case value_t::discarded:
return 0ul;
case value_t::object: case value_t::object:
return header_size + calc_bson_object_size(*j.m_value.object); return header_size + calc_bson_object_size(*j.m_value.object);
case value_t::array: case value_t::array:
@ -8742,6 +8743,8 @@ class binary_writer
assert(false); assert(false);
return; return;
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
case value_t::discarded:
return;
case value_t::object: case value_t::object:
return write_bson_object_entry(name, *j.m_value.object); return write_bson_object_entry(name, *j.m_value.object);
case value_t::array: case value_t::array:

View file

@ -376,6 +376,31 @@ TEST_CASE("BSON")
CHECK(json::from_bson(result, true, false) == j); CHECK(json::from_bson(result, true, false) == j);
} }
SECTION("discarded values are not serialized")
{
json j = json::value_t::discarded;
const auto result = json::to_bson(j);
CHECK(result.empty());
}
SECTION("discarded members are not serialized")
{
json j =
{
{ "entry", json::value_t::discarded }
};
std::vector<uint8_t> expected =
{
0x05, 0x00, 0x00, 0x00, // size (little endian)
// no entries
0x00 // end marker
};
const auto result = json::to_bson(j);
CHECK(result == expected);
}
SECTION("non-empty object with object member") SECTION("non-empty object with object member")
{ {