From df0f612d1b32f04a191078db75c8f3deb4655cdb Mon Sep 17 00:00:00 2001 From: Julian Becker Date: Sun, 7 Oct 2018 20:08:05 +0200 Subject: [PATCH] BSON: allow and discard values and object entries of type `value_t::discarded` --- .../nlohmann/detail/output/binary_writer.hpp | 5 +++- single_include/nlohmann/json.hpp | 5 +++- test/src/unit-bson.cpp | 25 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index bce5116d..0ec237ea 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -9,7 +9,6 @@ #include #include - namespace nlohmann { namespace detail @@ -864,6 +863,8 @@ class binary_writer assert(false); return 0ul; // LCOV_EXCL_STOP + case value_t::discarded: + return 0ul; case value_t::object: return header_size + calc_bson_object_size(*j.m_value.object); case value_t::array: @@ -898,6 +899,8 @@ class binary_writer assert(false); return; // LCOV_EXCL_STOP + case value_t::discarded: + return; case value_t::object: return write_bson_object_entry(name, *j.m_value.object); case value_t::array: diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 48b17d17..98c6b1f1 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -7853,7 +7853,6 @@ class binary_reader // #include - namespace nlohmann { namespace detail @@ -8708,6 +8707,8 @@ class binary_writer assert(false); return 0ul; // LCOV_EXCL_STOP + case value_t::discarded: + return 0ul; case value_t::object: return header_size + calc_bson_object_size(*j.m_value.object); case value_t::array: @@ -8742,6 +8743,8 @@ class binary_writer assert(false); return; // LCOV_EXCL_STOP + case value_t::discarded: + return; case value_t::object: return write_bson_object_entry(name, *j.m_value.object); case value_t::array: diff --git a/test/src/unit-bson.cpp b/test/src/unit-bson.cpp index 33ef25c4..4a1b387e 100644 --- a/test/src/unit-bson.cpp +++ b/test/src/unit-bson.cpp @@ -376,6 +376,31 @@ TEST_CASE("BSON") 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 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") {