From 8bf6d54c82a232c1641fb1f136632bdf457ff9ce Mon Sep 17 00:00:00 2001 From: vittorioromeo Date: Fri, 11 Apr 2025 17:15:10 +0200 Subject: [PATCH] Remove `` dependency from `from_json.hpp` --- .../nlohmann/detail/conversions/from_json.hpp | 56 +++++++++++-------- single_include/nlohmann/json.hpp | 56 +++++++++++-------- 2 files changed, 64 insertions(+), 48 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index d647d7423..432e85fb8 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -8,7 +8,6 @@ #pragma once -#include // transform #include // array #include // forward_list #include // inserter, front_inserter, end @@ -177,11 +176,11 @@ inline void from_json(const BasicJsonType& j, std::forward_list& l JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); } l.clear(); - std::transform(j.rbegin(), j.rend(), - std::front_inserter(l), [](const BasicJsonType & i) + + for (auto it = j.rbegin(); it != j.rend(); ++it) { - return i.template get(); - }); + l.push_front(it->template get()); + } } // valarray doesn't have an insert method @@ -194,11 +193,12 @@ inline void from_json(const BasicJsonType& j, std::valarray& l) JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); } l.resize(j.size()); - std::transform(j.begin(), j.end(), std::begin(l), - [](const BasicJsonType & elem) + + auto outIt = std::begin(l); + for (auto it = j.begin(); it != j.end(); ++it) { - return elem.template get(); - }); + *outIt++ = it->template get(); + } } template @@ -290,13 +290,16 @@ auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, p ConstructibleArrayType ret; ret.reserve(j.size()); - std::transform(j.begin(), j.end(), - std::inserter(ret, end(ret)), [](const BasicJsonType & i) + + auto outIt = end(ret); + for (auto it = j.begin(); it != j.end(); ++it) { // get() returns *this, this won't call a from_json // method when value_type is BasicJsonType - return i.template get(); - }); + outIt = ret.insert(outIt, it->template get()); + ++outIt; + } + arr = std::move(ret); } @@ -310,14 +313,16 @@ inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& using std::end; ConstructibleArrayType ret; - std::transform( - j.begin(), j.end(), std::inserter(ret, end(ret)), - [](const BasicJsonType & i) + + auto outIt = end(ret); + for (auto it = j.begin(); it != j.end(); ++it) { // get() returns *this, this won't call a from_json // method when value_type is BasicJsonType - return i.template get(); - }); + outIt = ret.insert(outIt, it->template get()); + ++outIt; + } + arr = std::move(ret); } @@ -384,13 +389,16 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) ConstructibleObjectType ret; const auto* inner_object = j.template get_ptr(); using value_type = typename ConstructibleObjectType::value_type; - std::transform( - inner_object->begin(), inner_object->end(), - std::inserter(ret, ret.begin()), - [](typename BasicJsonType::object_t::value_type const & p) + + auto outIt = end(ret); + for (auto it = inner_object->begin(); it != inner_object->end(); ++it) { - return value_type(p.first, p.second.template get()); - }); + // get() returns *this, this won't call a from_json + // method when value_type is BasicJsonType + outIt = ret.insert(outIt, value_type(it->first, it->second.template get())); + ++outIt; + } + obj = std::move(ret); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 82d69f7c5..6fba04295 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -168,7 +168,6 @@ -#include // transform #include // array #include // forward_list #include // inserter, front_inserter, end @@ -4962,11 +4961,11 @@ inline void from_json(const BasicJsonType& j, std::forward_list& l JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); } l.clear(); - std::transform(j.rbegin(), j.rend(), - std::front_inserter(l), [](const BasicJsonType & i) + + for (auto it = j.rbegin(); it != j.rend(); ++it) { - return i.template get(); - }); + l.push_front(it->template get()); + } } // valarray doesn't have an insert method @@ -4979,11 +4978,12 @@ inline void from_json(const BasicJsonType& j, std::valarray& l) JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j)); } l.resize(j.size()); - std::transform(j.begin(), j.end(), std::begin(l), - [](const BasicJsonType & elem) + + auto outIt = std::begin(l); + for (auto it = j.begin(); it != j.end(); ++it) { - return elem.template get(); - }); + *outIt++ = it->template get(); + } } template @@ -5075,13 +5075,16 @@ auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, p ConstructibleArrayType ret; ret.reserve(j.size()); - std::transform(j.begin(), j.end(), - std::inserter(ret, end(ret)), [](const BasicJsonType & i) + + auto outIt = end(ret); + for (auto it = j.begin(); it != j.end(); ++it) { // get() returns *this, this won't call a from_json // method when value_type is BasicJsonType - return i.template get(); - }); + outIt = ret.insert(outIt, it->template get()); + ++outIt; + } + arr = std::move(ret); } @@ -5095,14 +5098,16 @@ inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& using std::end; ConstructibleArrayType ret; - std::transform( - j.begin(), j.end(), std::inserter(ret, end(ret)), - [](const BasicJsonType & i) + + auto outIt = end(ret); + for (auto it = j.begin(); it != j.end(); ++it) { // get() returns *this, this won't call a from_json // method when value_type is BasicJsonType - return i.template get(); - }); + outIt = ret.insert(outIt, it->template get()); + ++outIt; + } + arr = std::move(ret); } @@ -5169,13 +5174,16 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) ConstructibleObjectType ret; const auto* inner_object = j.template get_ptr(); using value_type = typename ConstructibleObjectType::value_type; - std::transform( - inner_object->begin(), inner_object->end(), - std::inserter(ret, ret.begin()), - [](typename BasicJsonType::object_t::value_type const & p) + + auto outIt = end(ret); + for (auto it = inner_object->begin(); it != inner_object->end(); ++it) { - return value_type(p.first, p.second.template get()); - }); + // get() returns *this, this won't call a from_json + // method when value_type is BasicJsonType + outIt = ret.insert(outIt, value_type(it->first, it->second.template get())); + ++outIt; + } + obj = std::move(ret); }