From 6278f31d23d77f472618b4cf3b0f9a37fc1e742a Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Tue, 12 Jan 2021 18:28:29 +0100 Subject: [PATCH] Simplify from_json overloads. --- .../nlohmann/detail/conversions/from_json.hpp | 65 +++++++------------ 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index aaf75e218..fcb6a5e60 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -249,23 +249,16 @@ void()) from_json_array_impl(j, arr, priority_tag<3> {}); } -template < typename T, typename BasicJsonType, typename ArrayType, std::size_t... Idx> -ArrayType from_json_inplace_array_impl_base(BasicJsonType&& j, identity_tag /*unused*/, - index_sequence /*unused*/) +template < typename BasicJsonType, typename T, std::size_t... Idx > +std::array from_json_inplace_array_impl(BasicJsonType&& j, + identity_tag> /*unused*/, index_sequence /*unused*/) { return { std::forward(j).at(Idx).template get()... }; } template < typename BasicJsonType, typename T, std::size_t N > -auto from_json_inplace_array_impl(BasicJsonType&& j, identity_tag> tag, priority_tag<0> /*unused*/) --> decltype(from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {})) -{ - return from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {}); -} - -template < typename BasicJsonType, typename ArrayType > -auto from_json(BasicJsonType&& j, identity_tag tag) --> decltype(from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {})) +auto from_json(BasicJsonType&& j, identity_tag> tag) +-> decltype(from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -273,7 +266,7 @@ auto from_json(BasicJsonType&& j, identity_tag tag) std::string(j.type_name()))); } - return from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {}); + return from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {}); } template @@ -358,9 +351,8 @@ std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag(j).at(1).template get()}; } -template>::value, int> = 0> -void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) +template +void from_json_pair_impl(BasicJsonType&& j, std::pair& p, priority_tag<1> /*unused*/) { p = from_json_pair_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } @@ -378,22 +370,27 @@ auto from_json(BasicJsonType&& j, PairRelatedType&& p) return from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {}); } -template -Tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/, priority_tag<0> /*unused*/) +template +std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence /*unused*/) { - return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); + return std::make_tuple(std::forward(j).at(Idx).template get()...); } -template::value, int> = 0> -void from_json_tuple_impl(BasicJsonType && j, Tuple& t, index_sequence /*unused*/, priority_tag<1> /*unused*/) +template +std::tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) { - t = from_json_tuple_impl(std::forward(j), identity_tag {}, priority_tag<0> {}); + return from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); } -template -auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence idx) --> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {})) +template +void from_json_tuple_impl(BasicJsonType&& j, std::tuple& t, priority_tag<1> /*unused*/) +{ + t = from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); +} + +template +auto from_json(BasicJsonType&& j, TupleRelated&& t) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -401,21 +398,7 @@ auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence std::string(j.type_name()))); } - return from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {}); -} - -template -auto from_json(BasicJsonType&& j, std::tuple& t) --> decltype(from_json_tuple(std::forward(j), t, index_sequence_for {})) -{ - from_json_tuple(std::forward(j), t, index_sequence_for {}); -} - -template -auto from_json(BasicJsonType&& j, identity_tag> tag) --> decltype(from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {})) -{ - return from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {}); + return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<1> {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,