diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index a2c82923c..b30249f49 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -397,9 +397,13 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) ConstructibleObjectType ret; const auto* inner_object = j.template get_ptr(); from_json_object_reserve(ret, inner_object->size(), priority_tag<1> {}); + using value_type = typename ConstructibleObjectType::value_type; for (const auto& p : *inner_object) { - ret.emplace(p.first, p.second.template get()); + // the keys of the source are sorted for the usual object types, so + // hinting the end makes every insertion constant-time (for containers + // that order their keys the same way) + ret.insert(ret.end(), value_type(p.first, p.second.template get())); } obj = std::move(ret); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4585ab440..851a8006d 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6101,9 +6101,13 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) ConstructibleObjectType ret; const auto* inner_object = j.template get_ptr(); from_json_object_reserve(ret, inner_object->size(), priority_tag<1> {}); + using value_type = typename ConstructibleObjectType::value_type; for (const auto& p : *inner_object) { - ret.emplace(p.first, p.second.template get()); + // the keys of the source are sorted for the usual object types, so + // hinting the end makes every insertion constant-time (for containers + // that order their keys the same way) + ret.insert(ret.end(), value_type(p.first, p.second.template get())); } obj = std::move(ret); }