From 08d781058c118836c9fc8a50b7d4675558ec08d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Mon, 19 Jun 2017 12:03:38 +0200 Subject: [PATCH] add from_json support for std::array --- src/json.hpp | 11 ++++++++++- test/src/unit-constructor1.cpp | 8 ++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index ccf83d5ee..87c8d21f8 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1063,6 +1063,15 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio }); } +template +void from_json_array_impl(const BasicJsonType& j, std::array& arr, priority_tag<2>) +{ + for (std::size_t i = 0; i < N; ++i) + { + arr[i] = j.at(i).template get(); + } +} + template::value and std::is_convertible::value and @@ -1074,7 +1083,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr) JSON_THROW(type_error::create(302, "type must be array, but is " + j.type_name())); } - from_json_array_impl(j, arr, priority_tag<1> {}); + from_json_array_impl(j, arr, priority_tag<2> {}); } template(t) == j[2]); } - SECTION("std::pair/tuple failures") + SECTION("std::pair/tuple/array failures") { json j{1}; CHECK_THROWS((j.get>())); CHECK_THROWS((j.get>())); + CHECK_THROWS((j.get>())); } SECTION("std::forward_list") @@ -299,12 +300,15 @@ TEST_CASE("constructors") CHECK(j == j_reference); } - SECTION("std::array") + SECTION("std::array") { std::array a {{json(1), json(1u), json(2.2), json(false), json("string"), json()}}; json j(a); CHECK(j.type() == json::value_t::array); CHECK(j == j_reference); + + const auto a2 = j.get>(); + CHECK(a2 == a); } SECTION("std::vector")