diff --git a/include/nlohmann/detail/meta/cpp_future.hpp b/include/nlohmann/detail/meta/cpp_future.hpp index e24518faf..cb8dc407a 100644 --- a/include/nlohmann/detail/meta/cpp_future.hpp +++ b/include/nlohmann/detail/meta/cpp_future.hpp @@ -138,7 +138,7 @@ using index_sequence_for = make_index_sequence; // dispatch utility (taken from ranges-v3) template struct priority_tag : priority_tag < N - 1 > {}; -template<> struct priority_tag<0> {}; +template<> struct priority_tag<-1> {}; // taken from ranges-v3 template diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index ede55acc2..35c1ba551 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -145,6 +145,26 @@ struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> T>::value; }; +template +struct has_to_json_member { +private: + template + static auto check(int) -> decltype(std::declval().to_json(std::declval()), std::true_type()); + template + static std::false_type check(...); +public: + static constexpr bool value = std::is_same_v(0)),std::true_type>; +}; +template +struct has_from_json_member { +private: + template + static auto check(int) -> decltype(std::declval().from_json(std::declval()), std::true_type()); + template + static std::false_type check(...); +public: + static constexpr bool value = std::is_same_v(0)),std::true_type>; +}; /////////////////// // is_ functions // @@ -274,6 +294,7 @@ struct is_constructible_object_type_impl < typename ConstructibleObjectType::mapped_type >::value)) || (has_from_json::value || + has_from_json_member::value || has_non_default_from_json < BasicJsonType, typename ConstructibleObjectType::mapped_type >::value); @@ -379,6 +400,7 @@ detected_t>::value >> typename BasicJsonType::array_t::value_type>::value || has_from_json::value || + has_from_json_member::value || has_non_default_from_json < BasicJsonType, typename ConstructibleArrayType::value_type >::value); }; diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 053be8f74..84ec1078d 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1655,6 +1655,13 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec assert_invariant(); } + template::value, int> = 0> + basic_json(const T& val) { + val.to_json(*this); + set_parents(); + assert_invariant(); + } + /*! @brief create a container (array or object) from an initializer list @@ -3114,6 +3121,13 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return JSONSerializer::from_json(*this); } + template::value, int> = 0> + ValueType get_impl(detail::priority_tag<-1>)const{ + ValueType v{}; + v.from_json(*this); + return v; + } + /*! @brief get special-case overload @@ -3296,6 +3310,15 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return v; } + template::value && + detail::has_from_json_member::value, int> = 0> + ValueType& get_to(ValueType& v) const + { + v.from_json(*this); + return v; + } + // specialization to allow to call get_to with a basic_json value // see https://github.com/nlohmann/json/issues/2175 template