From 5b93e7640c10c4a59e398e9fc4fe985057ccf7b3 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 30 Aug 2021 18:04:35 +0200 Subject: [PATCH 1/2] :recycle: allow allocators for vectors --- include/nlohmann/detail/output/output_adapters.hpp | 11 ++++++----- single_include/nlohmann/json.hpp | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index 6f2462685e..1cad57b19a 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 8959265dae..de8f330004 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) From 3a4f5a8a9cb56b302226edb5b02e4de86d31da6b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 2 Sep 2021 08:32:19 +0200 Subject: [PATCH 2/2] :white_check_mark: add regression tests --- test/src/unit-regression2.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 8d0175e9d7..a42220e471 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