diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 457f445de..a1def699f 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 2b2b2d3ba..280f69534 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 b959ec5e5..b0ce05fd4 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 d60f7a556..1a60efdc9 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)