From 58b83b71dcb32729dc0eb56bd75754fc344b1278 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Sun, 12 Sep 2021 18:51:25 +0200 Subject: [PATCH 1/2] Set parent pointers for values inserted via update() (fixes #3007). (#3008) * Set parent pointers for values inserted via update() (fixes #3007). * Moved test for #3007 to proper file. * Enable access to private members in diagnostics unit tests. * Make style consistent with rest of code. * Forced amalgamate rerun. * Refactor test for #3007 so it doesn't use private members. Also extend to test both update() functions. * Added fix for #3007 to update(const_iterator, const_iterator) as well. * Added failing example code from #3007 as extra test. --- include/nlohmann/json.hpp | 6 ++++ single_include/nlohmann/json.hpp | 6 ++++ test/src/unit-diagnostics.cpp | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index fc3e60828..053be8f74 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -6009,6 +6009,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (auto it = j.cbegin(); it != j.cend(); ++it) { m_value.object->operator[](it.key()) = it.value(); +#if JSON_DIAGNOSTICS + m_value.object->operator[](it.key()).m_parent = this; +#endif } } @@ -6069,6 +6072,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (auto it = first; it != last; ++it) { m_value.object->operator[](it.key()) = it.value(); +#if JSON_DIAGNOSTICS + m_value.object->operator[](it.key()).m_parent = this; +#endif } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8959265da..803a83c39 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -23414,6 +23414,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (auto it = j.cbegin(); it != j.cend(); ++it) { m_value.object->operator[](it.key()) = it.value(); +#if JSON_DIAGNOSTICS + m_value.object->operator[](it.key()).m_parent = this; +#endif } } @@ -23474,6 +23477,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec for (auto it = first; it != last; ++it) { m_value.object->operator[](it.key()) = it.value(); +#if JSON_DIAGNOSTICS + m_value.object->operator[](it.key()).m_parent = this; +#endif } } diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index d4bbf93aa..8e4f2ffc3 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -184,4 +184,54 @@ TEST_CASE("Better diagnostics") j["second"] = value; j2["something"] = j; } + + SECTION("Regression test for issue #3007 - Parent pointers properly set when using update()") + { + // void update(const_reference j) + { + json j = json::object(); + + { + json j2 = json::object(); + j2["one"] = 1; + + j.update(j2); + } + + // Must call operator[] on const element, otherwise m_parent gets updated. + auto const& constJ = j; + CHECK_THROWS_WITH_AS(constJ["one"].at(0), "[json.exception.type_error.304] (/one) cannot use at() with number", json::type_error); + } + + // void update(const_iterator first, const_iterator last) + { + json j = json::object(); + + { + json j2 = json::object(); + j2["one"] = 1; + + j.update(j2.begin(), j2.end()); + } + + // Must call operator[] on const element, otherwise m_parent gets updated. + auto const& constJ = j; + CHECK_THROWS_WITH_AS(constJ["one"].at(0), "[json.exception.type_error.304] (/one) cannot use at() with number", json::type_error); + } + + // Code from #3007 triggering unwanted assertion without fix to update(). + { + json root = json::array(); + json lower = json::object(); + + { + json lowest = json::object(); + lowest["one"] = 1; + + lower.update(lowest); + } + + root.push_back(lower); + } + } } From 0b345b20c888f7dc8888485768e4bf9a6be29de0 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 12 Sep 2021 18:55:47 +0200 Subject: [PATCH 2/2] Allow allocators for output_vector_adapter (#2989) * :recycle: allow allocators for vectors * :white_check_mark: add regression tests --- .../nlohmann/detail/output/output_adapters.hpp | 11 ++++++----- single_include/nlohmann/json.hpp | 11 ++++++----- test/src/unit-regression2.cpp | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index 6f2462685..1cad57b19 100644 --- a/include/nlohmann/detail/output/output_adapters.hpp +++ b/include/nlohmann/detail/output/output_adapters.hpp @@ -37,11 +37,11 @@ template using output_adapter_t = std::shared_ptr>; /// output adapter for byte vectors -template +template> class output_vector_adapter : public output_adapter_protocol { public: - explicit output_vector_adapter(std::vector& vec) noexcept + explicit output_vector_adapter(std::vector& vec) noexcept : v(vec) {} @@ -57,7 +57,7 @@ class output_vector_adapter : public output_adapter_protocol } private: - std::vector& v; + std::vector& v; }; #ifndef JSON_NO_IO @@ -114,8 +114,9 @@ template> class output_adapter { public: - output_adapter(std::vector& vec) - : oa(std::make_shared>(vec)) {} + template> + output_adapter(std::vector& vec) + : oa(std::make_shared>(vec)) {} #ifndef JSON_NO_IO output_adapter(std::basic_ostream& s) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 803a83c39..9c27aabbe 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -13485,11 +13485,11 @@ template using output_adapter_t = std::shared_ptr>; /// output adapter for byte vectors -template +template> class output_vector_adapter : public output_adapter_protocol { public: - explicit output_vector_adapter(std::vector& vec) noexcept + explicit output_vector_adapter(std::vector& vec) noexcept : v(vec) {} @@ -13505,7 +13505,7 @@ class output_vector_adapter : public output_adapter_protocol } private: - std::vector& v; + std::vector& v; }; #ifndef JSON_NO_IO @@ -13562,8 +13562,9 @@ template> class output_adapter { public: - output_adapter(std::vector& vec) - : oa(std::make_shared>(vec)) {} + template> + output_adapter(std::vector& vec) + : oa(std::make_shared>(vec)) {} #ifndef JSON_NO_IO output_adapter(std::basic_ostream& s) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 8d0175e9d..a42220e47 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -181,6 +181,13 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser std::string* sax_no_exception::error_string = nullptr; +///////////////////////////////////////////////////////////////////// +// for #2982 +///////////////////////////////////////////////////////////////////// + +template +class my_allocator : public std::allocator +{}; TEST_CASE("regression tests 2") { @@ -679,6 +686,15 @@ TEST_CASE("regression tests 2") test3[json::json_pointer(p)] = json::object(); CHECK(test3.dump() == "{\"/root\":{}}"); } + + SECTION("issue #2982 - to_{binary format} does not provide a mechanism for specifying a custom allocator for the returned type") + { + std::vector> my_vector; + json j = {1, 2, 3, 4}; + json::to_cbor(j, my_vector); + json k = json::from_cbor(my_vector); + CHECK(j == k); + } } DOCTEST_CLANG_SUPPRESS_WARNING_POP