From 8b686b30eb4bcc39feded9b4cda2c5861edd6b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camille=20B=C3=A9gu=C3=A9?= Date: Thu, 31 Oct 2019 18:39:01 +0100 Subject: [PATCH] Add restriction for tuple specialization of to_json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fix the issue #1825 Signed-off-by: Camille Bégué --- .../nlohmann/detail/conversions/to_json.hpp | 6 +++--- include/nlohmann/detail/meta/type_traits.hpp | 13 +++++++++++++ single_include/nlohmann/json.hpp | 19 ++++++++++++++++--- test/src/unit-regression.cpp | 6 ++++++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 457f445d..a1def699 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -322,10 +322,10 @@ void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence j = { std::get(t)... }; } -template -void to_json(BasicJsonType& j, const std::tuple& t) +template::value, int > = 0> +void to_json(BasicJsonType& j, const T& t) { - to_json_tuple_impl(j, t, index_sequence_for {}); + to_json_tuple_impl(j, t, make_index_sequence::value> {}); } struct to_json_fn diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index 2b2b2d3b..280f6953 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -357,5 +357,18 @@ struct is_compatible_type_impl < template struct is_compatible_type : is_compatible_type_impl {}; + +// https://en.cppreference.com/w/cpp/types/conjunction +template struct conjunction : std::true_type { }; +template struct conjunction : B1 { }; +template +struct conjunction +: std::conditional, B1>::type {}; + +template +struct is_constructible_tuple : std::false_type {}; + +template +struct is_constructible_tuple> : conjunction...> {}; } // namespace detail } // namespace nlohmann diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index b959ec5e..b0ce05fd 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2794,6 +2794,19 @@ struct is_compatible_type_impl < template struct is_compatible_type : is_compatible_type_impl {}; + +// https://en.cppreference.com/w/cpp/types/conjunction +template struct conjunction : std::true_type { }; +template struct conjunction : B1 { }; +template +struct conjunction +: std::conditional, B1>::type {}; + +template +struct is_constructible_tuple : std::false_type {}; + +template +struct is_constructible_tuple> : conjunction...> {}; } // namespace detail } // namespace nlohmann @@ -3753,10 +3766,10 @@ void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence j = { std::get(t)... }; } -template -void to_json(BasicJsonType& j, const std::tuple& t) +template::value, int > = 0> +void to_json(BasicJsonType& j, const T& t) { - to_json_tuple_impl(j, t, index_sequence_for {}); + to_json_tuple_impl(j, t, make_index_sequence::value> {}); } struct to_json_fn diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index d60f7a55..1a60efdc 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -1842,6 +1842,12 @@ TEST_CASE("regression tests") static_assert(!std::is_constructible>::value, ""); static_assert(std::is_constructible>::value, ""); } + SECTION("issue #1825 - A tuple is json constructible only if all T in Args 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)