Fixed std::pair trying to deserialize via array functions.

This commit is contained in:
Anthony VH 2021-01-11 18:16:15 +01:00
parent 23f462b598
commit 672e8bfc1d
3 changed files with 30 additions and 23 deletions

View File

@ -249,17 +249,23 @@ void())
from_json_array_impl(j, arr, priority_tag<3> {});
}
template < typename BasicJsonType, typename Array, std::size_t... Is >
Array from_json_array_impl(BasicJsonType&& j, identity_tag<Array> /*unused*/, index_sequence<Is...> /*unused*/)
template < typename T, typename BasicJsonType, typename ArrayType, std::size_t... Idx>
ArrayType from_json_inplace_array_impl_base(BasicJsonType&& j, identity_tag<ArrayType> /*unused*/,
index_sequence<Idx...> /*unused*/)
{
return { std::forward<BasicJsonType>(j).at(Is).template get<typename Array::value_type>()... };
return { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... };
}
template < typename BasicJsonType, typename T, std::size_t N,
enable_if_t < !std::is_default_constructible<std::array<T, N>>::value, int > = 0 >
auto from_json(BasicJsonType && j, identity_tag<std::array<T, N>> tag)
-> decltype(j.template get<T>(),
from_json_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
template < typename BasicJsonType, typename T, std::size_t N >
auto from_json_inplace_array_impl(BasicJsonType&& j, identity_tag<std::array<T, N>> tag, priority_tag<0> /*unused*/)
-> decltype(from_json_inplace_array_impl_base<T>(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
{
return from_json_inplace_array_impl_base<T>(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
}
template < typename BasicJsonType, typename ArrayType >
auto from_json(BasicJsonType&& j, identity_tag<ArrayType> tag)
-> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, priority_tag<0> {}))
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
{
@ -267,7 +273,7 @@ from_json_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N>
std::string(j.type_name())));
}
return from_json_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, priority_tag<0> {});
}
template<typename BasicJsonType>
@ -352,7 +358,6 @@ std::pair<A1, A2> from_json_pair_impl(BasicJsonType&& j, identity_tag<std::pair<
std::forward<BasicJsonType>(j).at(1).template get<A2>()};
}
template<typename BasicJsonType, typename A1, typename A2,
enable_if_t<std::is_default_constructible<std::pair<A1, A2>>::value, int> = 0>
void from_json_pair_impl(BasicJsonType && j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)

View File

@ -3746,17 +3746,23 @@ void())
from_json_array_impl(j, arr, priority_tag<3> {});
}
template < typename BasicJsonType, typename Array, std::size_t... Is >
Array from_json_array_impl(BasicJsonType&& j, identity_tag<Array> /*unused*/, index_sequence<Is...> /*unused*/)
template < typename T, typename BasicJsonType, typename ArrayType, std::size_t... Idx>
ArrayType from_json_inplace_array_impl_base(BasicJsonType&& j, identity_tag<ArrayType> /*unused*/,
index_sequence<Idx...> /*unused*/)
{
return { std::forward<BasicJsonType>(j).at(Is).template get<typename Array::value_type>()... };
return { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... };
}
template < typename BasicJsonType, typename T, std::size_t N,
enable_if_t < !std::is_default_constructible<std::array<T, N>>::value, int > = 0 >
auto from_json(BasicJsonType && j, identity_tag<std::array<T, N>> tag)
-> decltype(j.template get<T>(),
from_json_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
template < typename BasicJsonType, typename T, std::size_t N >
auto from_json_inplace_array_impl(BasicJsonType&& j, identity_tag<std::array<T, N>> tag, priority_tag<0> /*unused*/)
-> decltype(from_json_inplace_array_impl_base<T>(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
{
return from_json_inplace_array_impl_base<T>(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
}
template < typename BasicJsonType, typename ArrayType >
auto from_json(BasicJsonType&& j, identity_tag<ArrayType> tag)
-> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, priority_tag<0> {}))
{
if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
{
@ -3764,7 +3770,7 @@ from_json_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N>
std::string(j.type_name())));
}
return from_json_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, priority_tag<0> {});
}
template<typename BasicJsonType>
@ -3849,7 +3855,6 @@ std::pair<A1, A2> from_json_pair_impl(BasicJsonType&& j, identity_tag<std::pair<
std::forward<BasicJsonType>(j).at(1).template get<A2>()};
}
template<typename BasicJsonType, typename A1, typename A2,
enable_if_t<std::is_default_constructible<std::pair<A1, A2>>::value, int> = 0>
void from_json_pair_impl(BasicJsonType && j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)

View File

@ -543,9 +543,6 @@ TEST_CASE("regression tests 2")
{
{
json j = { 3, 8 };
auto x = j.at(0).get<NonDefaultConstructible>();
CHECK(x.x == 3);
auto p = j.get<std::pair<NonDefaultConstructible, NonDefaultConstructible>>();
CHECK(p.first.x == 3);
CHECK(p.second.x == 8);