Compare commits

...
Author SHA1 Message Date
Niels Lohmann e59b455616 Update doc example outputs for new object-conversion iteration order
Reserving capacity in from_json()'s object-conversion path before
inserting elements changes libstdc++'s std::unordered_map bucket
layout, which changes the iteration order used by
get__ValueType_const.cpp, get_to.cpp and operator__ValueType.cpp to
print the elements of a converted std::unordered_map<std::string,
json>. Verified against a clean develop checkout (built with the
same GCC/libstdc++ used in CI) that the old order was produced
without this PR's change and the new order is produced with it, and
that the three affected examples now match their updated expected
output byte-for-byte.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-09-06 11:34:53 +02:00
Niels Lohmann e29ac4f043 Reserve capacity in from_json() object conversion when supported
The object-to-container from_json() overload filled the target
container one element at a time without reserving capacity, even
when the target type supports reserve() (e.g. std::unordered_map)
and the number of elements is already known. This caused unnecessary
rehashing while parsing large objects into such containers.

Add a reserve-detecting overload (from_json_object_impl), mirroring
the priority_tag-based SFINAE technique already used by the array
conversion path (from_json_array_impl), so that reserve(size()) is
called up front when available and the loop falls back unchanged
otherwise (e.g. for std::map).

Fixes #5406

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-09-05 20:43:23 +02:00
6 changed files with 92 additions and 17 deletions
@@ -4,8 +4,8 @@
Hello, world! Hello, world!
1 2 3 4 5 1 2 3 4 5
string: "Hello, world!"
number: {"floating-point":17.23,"integer":42} number: {"floating-point":17.23,"integer":42}
null: null null: null
string: "Hello, world!"
boolean: true boolean: true
array: [1,2,3,4,5] array: [1,2,3,4,5]
+1 -1
View File
@@ -4,8 +4,8 @@
Hello, world! Hello, world!
1 2 3 4 5 1 2 3 4 5
string: "Hello, world!"
number: {"floating-point":17.23,"integer":42} number: {"floating-point":17.23,"integer":42}
null: null null: null
string: "Hello, world!"
boolean: true boolean: true
array: [1,2,3,4,5] array: [1,2,3,4,5]
@@ -4,9 +4,9 @@
Hello, world! Hello, world!
1 2 3 4 5 1 2 3 4 5
string: "Hello, world!"
number: {"floating-point":17.23,"integer":42} number: {"floating-point":17.23,"integer":42}
null: null null: null
string: "Hello, world!"
boolean: true boolean: true
array: [1,2,3,4,5] array: [1,2,3,4,5]
[json.exception.type_error.302] type must be boolean, but is string [json.exception.type_error.302] type must be boolean, but is string
@@ -398,6 +398,34 @@ inline void from_json(const BasicJsonType& j, CompatibleArrayType& bin)
} }
} }
template<typename BasicJsonType, typename ConstructibleObjectType>
auto from_json_object_impl(const BasicJsonType& j, ConstructibleObjectType& obj, priority_tag<1> /*unused*/)
-> decltype(
obj.reserve(std::declval<typename ConstructibleObjectType::size_type>()),
void())
{
ConstructibleObjectType ret;
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
ret.reserve(inner_object->size());
for (const auto& p : *inner_object)
{
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
}
obj = std::move(ret);
}
template<typename BasicJsonType, typename ConstructibleObjectType>
inline void from_json_object_impl(const BasicJsonType& j, ConstructibleObjectType& obj, priority_tag<0> /*unused*/)
{
ConstructibleObjectType ret;
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
for (const auto& p : *inner_object)
{
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
}
obj = std::move(ret);
}
template<typename BasicJsonType, typename ConstructibleObjectType, template<typename BasicJsonType, typename ConstructibleObjectType,
enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0> enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
@@ -407,13 +435,7 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j)); JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
} }
ConstructibleObjectType ret; from_json_object_impl(j, obj, priority_tag<1> {});
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
for (const auto& p : *inner_object)
{
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
}
obj = std::move(ret);
} }
// overload for arithmetic types, not chosen for basic_json template arguments // overload for arithmetic types, not chosen for basic_json template arguments
+29 -7
View File
@@ -5693,6 +5693,34 @@ inline void from_json(const BasicJsonType& j, CompatibleArrayType& bin)
} }
} }
template<typename BasicJsonType, typename ConstructibleObjectType>
auto from_json_object_impl(const BasicJsonType& j, ConstructibleObjectType& obj, priority_tag<1> /*unused*/)
-> decltype(
obj.reserve(std::declval<typename ConstructibleObjectType::size_type>()),
void())
{
ConstructibleObjectType ret;
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
ret.reserve(inner_object->size());
for (const auto& p : *inner_object)
{
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
}
obj = std::move(ret);
}
template<typename BasicJsonType, typename ConstructibleObjectType>
inline void from_json_object_impl(const BasicJsonType& j, ConstructibleObjectType& obj, priority_tag<0> /*unused*/)
{
ConstructibleObjectType ret;
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
for (const auto& p : *inner_object)
{
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
}
obj = std::move(ret);
}
template<typename BasicJsonType, typename ConstructibleObjectType, template<typename BasicJsonType, typename ConstructibleObjectType,
enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0> enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
@@ -5702,13 +5730,7 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j)); JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
} }
ConstructibleObjectType ret; from_json_object_impl(j, obj, priority_tag<1> {});
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
for (const auto& p : *inner_object)
{
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
}
obj = std::move(ret);
} }
// overload for arithmetic types, not chosen for basic_json template arguments // overload for arithmetic types, not chosen for basic_json template arguments
+31
View File
@@ -1389,6 +1389,37 @@ TEST_CASE("value conversion")
// CHECK(m5["one"] == "eins"); // CHECK(m5["one"] == "eins");
} }
SECTION("reserve is called on containers that support it (#5406)")
{
// build a larger object so that a missing/incorrect reserve()
// call would be more likely to corrupt or drop elements
json j_large;
for (int i = 0; i < 100; ++i)
{
j_large[std::to_string(i)] = i;
}
SECTION("std::unordered_map (supports reserve)")
{
const auto m = j_large.get<std::unordered_map<std::string, int>>();
CHECK(m.size() == 100);
for (int i = 0; i < 100; ++i)
{
CHECK(m.at(std::to_string(i)) == i);
}
}
SECTION("std::map (no reserve, fallback path)")
{
const auto m = j_large.get<std::map<std::string, int>>();
CHECK(m.size() == 100);
for (int i = 0; i < 100; ++i)
{
CHECK(m.at(std::to_string(i)) == i);
}
}
}
SECTION("std::multimap") SECTION("std::multimap")
{ {
j1.get<std::multimap<std::string, int>>(); j1.get<std::multimap<std::string, int>>();