mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 11:40:30 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a24b8c2d0 |
@@ -100,6 +100,3 @@ the latter case, it is skipped completely, or replaced by `null` if it is the to
|
||||
- Added in version 1.0.0.
|
||||
- Fixed in version 3.13.0 to also remove discarded values from a parent object; before, discarding an array or a value
|
||||
stored under an object key left a discarded member behind, which made the parse result serialize to invalid JSON.
|
||||
- Fixed in version 3.13.0 so that discarding an array or object at its start event also hides its content from the
|
||||
callback, as documented above; before, the callback was still called for the content, and the key of every member of
|
||||
a discarded object was kept in memory until the parse ended.
|
||||
|
||||
@@ -564,7 +564,7 @@ inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allo
|
||||
{
|
||||
if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
|
||||
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &p));
|
||||
}
|
||||
m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
|
||||
}
|
||||
@@ -584,7 +584,7 @@ inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Has
|
||||
{
|
||||
if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
|
||||
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &p));
|
||||
}
|
||||
m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
|
||||
}
|
||||
|
||||
@@ -582,8 +582,8 @@ class json_sax_dom_callback_parser
|
||||
|
||||
bool start_object(std::size_t len)
|
||||
{
|
||||
// check callback for object start; not called inside a discarded container
|
||||
const bool keep = keep_stack.back() && callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
|
||||
// check callback for object start
|
||||
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
|
||||
keep_stack.push_back(keep);
|
||||
|
||||
// the key this object will be stored under, read before handle_value()
|
||||
@@ -619,18 +619,6 @@ class json_sax_dom_callback_parser
|
||||
|
||||
bool key(string_t& val)
|
||||
{
|
||||
if (!keep_stack.back() || !ref_stack.back())
|
||||
{
|
||||
// the object is not stored: the value of this key is dropped in
|
||||
// handle_value() without touching the key stacks
|
||||
if (keep_stack.back())
|
||||
{
|
||||
BasicJsonType k = BasicJsonType(val);
|
||||
static_cast<void>(callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
BasicJsonType k = BasicJsonType(val);
|
||||
|
||||
// check callback for the key
|
||||
@@ -716,7 +704,7 @@ class json_sax_dom_callback_parser
|
||||
|
||||
bool start_array(std::size_t len)
|
||||
{
|
||||
const bool keep = keep_stack.back() && callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
|
||||
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
|
||||
keep_stack.push_back(keep);
|
||||
|
||||
// see start_object()
|
||||
|
||||
@@ -6061,7 +6061,7 @@ inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allo
|
||||
{
|
||||
if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
|
||||
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &p));
|
||||
}
|
||||
m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
|
||||
}
|
||||
@@ -6081,7 +6081,7 @@ inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Has
|
||||
{
|
||||
if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
|
||||
{
|
||||
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
|
||||
JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &p));
|
||||
}
|
||||
m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
|
||||
}
|
||||
@@ -11996,8 +11996,8 @@ class json_sax_dom_callback_parser
|
||||
|
||||
bool start_object(std::size_t len)
|
||||
{
|
||||
// check callback for object start; not called inside a discarded container
|
||||
const bool keep = keep_stack.back() && callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
|
||||
// check callback for object start
|
||||
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
|
||||
keep_stack.push_back(keep);
|
||||
|
||||
// the key this object will be stored under, read before handle_value()
|
||||
@@ -12033,18 +12033,6 @@ class json_sax_dom_callback_parser
|
||||
|
||||
bool key(string_t& val)
|
||||
{
|
||||
if (!keep_stack.back() || !ref_stack.back())
|
||||
{
|
||||
// the object is not stored: the value of this key is dropped in
|
||||
// handle_value() without touching the key stacks
|
||||
if (keep_stack.back())
|
||||
{
|
||||
BasicJsonType k = BasicJsonType(val);
|
||||
static_cast<void>(callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
BasicJsonType k = BasicJsonType(val);
|
||||
|
||||
// check callback for the key
|
||||
@@ -12130,7 +12118,7 @@ class json_sax_dom_callback_parser
|
||||
|
||||
bool start_array(std::size_t len)
|
||||
{
|
||||
const bool keep = keep_stack.back() && callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
|
||||
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
|
||||
keep_stack.push_back(keep);
|
||||
|
||||
// see start_object()
|
||||
|
||||
@@ -1966,102 +1966,6 @@ TEST_CASE("parser class")
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("no callback for the content of a discarded container (#5643)")
|
||||
{
|
||||
// discarding a container at its start event must also hide
|
||||
// everything inside it from the callback: none of the nested
|
||||
// keys, values, or nested containers' own start/end events may
|
||||
// be reported
|
||||
std::vector<std::string> log;
|
||||
bool first = true;
|
||||
const json j = json::parse(R"({"skip": {"k1": 1, "k2": [2, {"k3": 3}]}, "keep": 1})",
|
||||
[&](int depth, json::parse_event_t event, json & parsed)
|
||||
{
|
||||
static const char* const names[] = {"object_start", "object_end", "array_start", "array_end", "key", "value"};
|
||||
log.push_back(std::to_string(depth) + " " + names[static_cast<int>(event)] + " " + parsed.dump());
|
||||
|
||||
if (depth == 1 && event == json::parse_event_t::object_start && first)
|
||||
{
|
||||
// discard "skip" right at its object_start event
|
||||
first = false;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
CHECK(log == std::vector<std::string>
|
||||
{
|
||||
"0 object_start <discarded>",
|
||||
"1 key \"skip\"",
|
||||
"1 object_start <discarded>",
|
||||
"1 key \"keep\"",
|
||||
"1 value 1",
|
||||
"0 object_end {\"keep\":1}"
|
||||
});
|
||||
CHECK(j == json({{"keep", 1}}));
|
||||
}
|
||||
|
||||
SECTION("callback still called inside a container whose key was rejected (#5643)")
|
||||
{
|
||||
// rejecting a key does not discard its value's container at the
|
||||
// container's own start event, so the callback is still called
|
||||
// for that container's content; only storing the container
|
||||
// under the rejected key is skipped
|
||||
// (documented for parser_callback_t: "the callback is still
|
||||
// called for the associated value, but its return value has no
|
||||
// further effect")
|
||||
const auto record = [](std::vector<std::string>& log, int depth, json::parse_event_t event, const json & parsed)
|
||||
{
|
||||
static const char* const names[] = {"object_start", "object_end", "array_start", "array_end", "key", "value"};
|
||||
log.push_back(std::to_string(depth) + " " + names[static_cast<int>(event)] + " " + parsed.dump());
|
||||
};
|
||||
|
||||
std::vector<std::string> log_object;
|
||||
const json j_object = json::parse(R"({"skip": {"k1": 1}, "keep": 2})",
|
||||
[&](int depth, json::parse_event_t event, json & parsed)
|
||||
{
|
||||
record(log_object, depth, event, parsed);
|
||||
return !(event == json::parse_event_t::key && parsed == json("skip"));
|
||||
});
|
||||
|
||||
CHECK(log_object == std::vector<std::string>
|
||||
{
|
||||
"0 object_start <discarded>",
|
||||
"1 key \"skip\"",
|
||||
"1 object_start <discarded>",
|
||||
"2 key \"k1\"",
|
||||
"2 value 1",
|
||||
"1 key \"keep\"",
|
||||
"1 value 2",
|
||||
"0 object_end {\"keep\":2}"
|
||||
});
|
||||
CHECK(j_object == json({{"keep", 2}}));
|
||||
|
||||
// same for a rejected key whose value is an array rather than an object
|
||||
std::vector<std::string> log_array;
|
||||
const json j_array = json::parse(R"({"skip": [1, {"k1": 2}], "keep": 2})",
|
||||
[&](int depth, json::parse_event_t event, json & parsed)
|
||||
{
|
||||
record(log_array, depth, event, parsed);
|
||||
return !(event == json::parse_event_t::key && parsed == json("skip"));
|
||||
});
|
||||
|
||||
CHECK(log_array == std::vector<std::string>
|
||||
{
|
||||
"0 object_start <discarded>",
|
||||
"1 key \"skip\"",
|
||||
"1 array_start <discarded>",
|
||||
"2 value 1",
|
||||
"2 object_start <discarded>",
|
||||
"3 key \"k1\"",
|
||||
"3 value 2",
|
||||
"1 key \"keep\"",
|
||||
"1 value 2",
|
||||
"0 object_end {\"keep\":2}"
|
||||
});
|
||||
CHECK(j_array == json({{"keep", 2}}));
|
||||
}
|
||||
|
||||
SECTION("special cases")
|
||||
{
|
||||
// the following test cases cover the situation in which an empty
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
using nlohmann::json;
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
TEST_CASE("Better diagnostics")
|
||||
{
|
||||
SECTION("empty JSON Pointer")
|
||||
@@ -331,6 +334,28 @@ TEST_CASE("Regression tests for extended diagnostics")
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Regression test for issue #5668 - wrong path for std::map/unordered_map with non-string keys")
|
||||
{
|
||||
// a map with non-string keys is read from an array of [key, value] arrays;
|
||||
// element 2 of "m" is not an array, so the path must point at "m/2", not "m"
|
||||
json j;
|
||||
j["outer"]["m"] = json::array({json::array({1, 2}), json::array({3, 4}), 5});
|
||||
|
||||
SECTION("std::map")
|
||||
{
|
||||
CHECK_THROWS_WITH_AS((j["outer"]["m"].get<std::map<int, int>>()),
|
||||
"[json.exception.type_error.302] (/outer/m/2) type must be array, "
|
||||
"but is number", json::type_error);
|
||||
}
|
||||
|
||||
SECTION("std::unordered_map")
|
||||
{
|
||||
CHECK_THROWS_WITH_AS((j["outer"]["m"].get<std::unordered_map<int, int>>()),
|
||||
"[json.exception.type_error.302] (/outer/m/2) type must be array, "
|
||||
"but is number", json::type_error);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Regression test - swap(array_t&)/swap(object_t&) must update JSON_DIAGNOSTICS parent pointers")
|
||||
{
|
||||
// swap(array_t&)
|
||||
|
||||
Reference in New Issue
Block a user