From 794a3d411a2e78c8a17a8488a02be50ba488562a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camille=20B=C3=A9gu=C3=A9?= Date: Mon, 21 Oct 2019 17:50:15 +0200 Subject: [PATCH] Fix issue #1805 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add some restriction on pair partial specialization of to_json Signed-off-by: Camille Bégué --- include/nlohmann/detail/conversions/to_json.hpp | 4 ++-- single_include/nlohmann/json.hpp | 4 ++-- test/src/unit-regression.cpp | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index c3ac5aa8..457f445d 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -302,8 +302,8 @@ void to_json(BasicJsonType& j, const T(&arr)[N]) external_constructor::construct(j, arr); } -template -void to_json(BasicJsonType& j, const std::pair& p) +template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible::value&& std::is_constructible::value, int > = 0 > +void to_json(BasicJsonType& j, const std::pair& p) { j = { p.first, p.second }; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e680d8b7..b1f4184f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3733,8 +3733,8 @@ void to_json(BasicJsonType& j, const T(&arr)[N]) external_constructor::construct(j, arr); } -template -void to_json(BasicJsonType& j, const std::pair& p) +template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible::value&& std::is_constructible::value, int > = 0 > +void to_json(BasicJsonType& j, const std::pair& p) { j = { p.first, p.second }; } diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index e0c5b20a..cebf6f7e 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -159,6 +159,16 @@ bool operator==(Data const& lhs, Data const& rhs) using float_json = nlohmann::basic_json; +///////////////////////////////////////////////////////////////////// +// for #1805 +///////////////////////////////////////////////////////////////////// + +struct NotSerializableData +{ + int mydata; + float myfloat; +}; + TEST_CASE("regression tests") { @@ -1820,6 +1830,12 @@ TEST_CASE("regression tests") CHECK(j.contains(jptr1)); CHECK(j.contains(jptr2)); } + SECTION("issue #1805 - A pair is json constructible only if T1 and T2 are json constructible") + { + static_assert(!std::is_constructible>::value, ""); + static_assert(!std::is_constructible>::value, ""); + static_assert(std::is_constructible>::value, ""); + } } #if not defined(JSON_NOEXCEPTION)