mirror of
https://github.com/nlohmann/json.git
synced 2026-09-09 01:37:57 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
553038e810 | ||
|
|
75226beb40 | ||
|
|
e59b455616 | ||
|
|
e29ac4f043 |
@@ -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,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,17 @@ inline void from_json(const BasicJsonType& j, CompatibleArrayType& bin)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ConstructibleObjectType>
|
||||||
|
auto from_json_object_reserve(ConstructibleObjectType& obj, typename ConstructibleObjectType::size_type size, priority_tag<1> /*unused*/)
|
||||||
|
-> decltype(obj.reserve(size), void())
|
||||||
|
{
|
||||||
|
obj.reserve(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ConstructibleObjectType>
|
||||||
|
inline void from_json_object_reserve(ConstructibleObjectType& /*obj*/, std::size_t /*size*/, priority_tag<0> /*unused*/)
|
||||||
|
{}
|
||||||
|
|
||||||
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)
|
||||||
@@ -409,6 +420,7 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
|
|||||||
|
|
||||||
ConstructibleObjectType ret;
|
ConstructibleObjectType ret;
|
||||||
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
|
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
|
||||||
|
from_json_object_reserve(ret, inner_object->size(), priority_tag<1> {});
|
||||||
for (const auto& p : *inner_object)
|
for (const auto& p : *inner_object)
|
||||||
{
|
{
|
||||||
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
|
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
|
||||||
|
|||||||
@@ -5693,6 +5693,17 @@ inline void from_json(const BasicJsonType& j, CompatibleArrayType& bin)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ConstructibleObjectType>
|
||||||
|
auto from_json_object_reserve(ConstructibleObjectType& obj, typename ConstructibleObjectType::size_type size, priority_tag<1> /*unused*/)
|
||||||
|
-> decltype(obj.reserve(size), void())
|
||||||
|
{
|
||||||
|
obj.reserve(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ConstructibleObjectType>
|
||||||
|
inline void from_json_object_reserve(ConstructibleObjectType& /*obj*/, std::size_t /*size*/, priority_tag<0> /*unused*/)
|
||||||
|
{}
|
||||||
|
|
||||||
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)
|
||||||
@@ -5704,6 +5715,7 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
|
|||||||
|
|
||||||
ConstructibleObjectType ret;
|
ConstructibleObjectType ret;
|
||||||
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
|
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
|
||||||
|
from_json_object_reserve(ret, inner_object->size(), priority_tag<1> {});
|
||||||
for (const auto& p : *inner_object)
|
for (const auto& p : *inner_object)
|
||||||
{
|
{
|
||||||
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
|
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
|
||||||
|
|||||||
@@ -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>>();
|
||||||
|
|||||||
Reference in New Issue
Block a user