From a9d5ae4fad1d40932e138781611c9b3680b26330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Sat, 14 Jan 2017 01:09:14 +0100 Subject: [PATCH] put back a specialization for containers with a reserve method --- src/json.hpp | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 98f78262..96221664 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -642,6 +642,41 @@ void from_json(Json const&j, std::forward_list& l) l.push_front(it->template get()); } + +template +void from_json_array_impl(Json const &j, CompatibleArrayType &arr, long) +{ + using std::begin; + using std::end; + + std::transform( + j.begin(), j.end(), std::inserter(arr, end(arr)), [](Json const &i) + { + // get() returns *this, this won't call a from_json method when + // value_type is Json + return i.template get(); + }); +} + +template +auto from_json_array_impl(Json const &j, CompatibleArrayType &arr, int) + -> decltype( + arr.reserve(std::declval()), + void()) +{ + using std::begin; + using std::end; + + arr.reserve(j.size()); + std::transform( + j.begin(), j.end(), std::inserter(arr, end(arr)), [](Json const &i) + { + // get() returns *this, this won't call a from_json method when + // value_type is Json + return i.template get(); + }); +} + template < typename Json, typename CompatibleArrayType, enable_if_t::value and @@ -658,16 +693,7 @@ void from_json(Json const &j, CompatibleArrayType &arr) if (!j.is_array()) throw std::domain_error("type must be array, but is " + type_name(j)); } - - using std::begin; - using std::end; - std::transform( - j.begin(), j.end(), std::inserter(arr, end(arr)), [](Json const &i) - { - // get() returns *this, this won't call a from_json method when - // value_type is Json - return i.template get(); - }); + from_json_array_impl(j, arr, 0); }