From 4b4bbceebf8591bc1bb3355472311013da8846bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Thu, 6 Sep 2018 16:54:42 +0200 Subject: [PATCH] make from_json SFINAE-correct --- include/nlohmann/adl_serializer.hpp | 6 ++-- .../nlohmann/detail/conversions/from_json.hpp | 28 ++------------- single_include/nlohmann/json.hpp | 34 ++++--------------- test/src/unit-inspection.cpp | 4 +-- 4 files changed, 16 insertions(+), 56 deletions(-) diff --git a/include/nlohmann/adl_serializer.hpp b/include/nlohmann/adl_serializer.hpp index 58d26ea9..0c28d1c7 100644 --- a/include/nlohmann/adl_serializer.hpp +++ b/include/nlohmann/adl_serializer.hpp @@ -20,8 +20,10 @@ struct adl_serializer @param[in,out] val value to write to */ template - static void from_json(BasicJsonType&& j, ValueType& val) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), val))) + static auto from_json(BasicJsonType&& j, ValueType& val) noexcept( + noexcept(::nlohmann::from_json(std::forward(j), val))) -> decltype( + ::nlohmann::from_json(std::forward(j), val), void() + ) { ::nlohmann::from_json(std::forward(j), val); } diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 15215d6e..f5f8199c 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -218,8 +218,8 @@ template decltype(from_json_array_impl(j, arr, priority_tag<3> {}), -j.template get(), -void()) + j.template get(), + void()) { if (JSON_UNLIKELY(not j.is_array())) { @@ -350,35 +350,13 @@ void from_json(const BasicJsonType& j, std::unordered_map - auto call(const BasicJsonType& j, T& val, priority_tag<1> /*unused*/) const + auto operator()(const BasicJsonType& j, T& val) const noexcept(noexcept(from_json(j, val))) -> decltype(from_json(j, val), void()) { return from_json(j, val); } - - template - void call(const BasicJsonType& /*unused*/, T& /*unused*/, priority_tag<0> /*unused*/) const noexcept - { - static_assert(sizeof(BasicJsonType) == 0, - "could not find from_json() method in T's namespace"); -#ifdef _MSC_VER - // MSVC does not show a stacktrace for the above assert - using decayed = uncvref_t; - static_assert(sizeof(typename decayed::force_msvc_stacktrace) == 0, - "forcing MSVC stacktrace to show which T we're talking about."); -#endif - } - - public: - template - void operator()(const BasicJsonType& j, T& val) const - noexcept(noexcept(std::declval().call(j, val, priority_tag<1> {}))) - { - return call(j, val, priority_tag<1> {}); - } }; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 16106c2f..1754c9a9 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -1212,8 +1212,8 @@ template decltype(from_json_array_impl(j, arr, priority_tag<3> {}), -j.template get(), -void()) + j.template get(), + void()) { if (JSON_UNLIKELY(not j.is_array())) { @@ -1344,35 +1344,13 @@ void from_json(const BasicJsonType& j, std::unordered_map - auto call(const BasicJsonType& j, T& val, priority_tag<1> /*unused*/) const + auto operator()(const BasicJsonType& j, T& val) const noexcept(noexcept(from_json(j, val))) -> decltype(from_json(j, val), void()) { return from_json(j, val); } - - template - void call(const BasicJsonType& /*unused*/, T& /*unused*/, priority_tag<0> /*unused*/) const noexcept - { - static_assert(sizeof(BasicJsonType) == 0, - "could not find from_json() method in T's namespace"); -#ifdef _MSC_VER - // MSVC does not show a stacktrace for the above assert - using decayed = uncvref_t; - static_assert(sizeof(typename decayed::force_msvc_stacktrace) == 0, - "forcing MSVC stacktrace to show which T we're talking about."); -#endif - } - - public: - template - void operator()(const BasicJsonType& j, T& val) const - noexcept(noexcept(std::declval().call(j, val, priority_tag<1> {}))) - { - return call(j, val, priority_tag<1> {}); - } }; } @@ -11112,8 +11090,10 @@ struct adl_serializer @param[in,out] val value to write to */ template - static void from_json(BasicJsonType&& j, ValueType& val) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), val))) + static auto from_json(BasicJsonType&& j, ValueType& val) noexcept( + noexcept(::nlohmann::from_json(std::forward(j), val))) -> decltype( + ::nlohmann::from_json(std::forward(j), val), void() + ) { ::nlohmann::from_json(std::forward(j), val); } diff --git a/test/src/unit-inspection.cpp b/test/src/unit-inspection.cpp index 8e42c6ec..a8be566f 100644 --- a/test/src/unit-inspection.cpp +++ b/test/src/unit-inspection.cpp @@ -317,8 +317,8 @@ TEST_CASE("object inspection") SECTION("round trips") { for (const auto& s : - {"3.141592653589793", "1000000000000000010E5" - }) + {"3.141592653589793", "1000000000000000010E5" + }) { json j1 = json::parse(s); std::string s1 = j1.dump();