mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 03:30:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54ed6480d5 |
@@ -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.
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -725,7 +725,7 @@ std::is_constructible <decltype(std::declval<Compare>()(std::declval<A>(), std::
|
||||
// avoid their instantiation on all compilers, even when the first operand
|
||||
// is false. The dispatch on is_json_pointer_of can be removed once the
|
||||
// deprecated json_pointer comparison operators have been removed.
|
||||
template<typename Compare, typename A, typename B, bool = is_json_pointer_of<A, B>::value>
|
||||
template<typename Compare, typename A, typename B, bool = is_json_pointer_of<uncvref_t<A>, uncvref_t<B>>::value>
|
||||
struct is_comparable : std::false_type {};
|
||||
|
||||
template<typename Compare, typename A, typename B>
|
||||
|
||||
@@ -4735,7 +4735,7 @@ std::is_constructible <decltype(std::declval<Compare>()(std::declval<A>(), std::
|
||||
// avoid their instantiation on all compilers, even when the first operand
|
||||
// is false. The dispatch on is_json_pointer_of can be removed once the
|
||||
// deprecated json_pointer comparison operators have been removed.
|
||||
template<typename Compare, typename A, typename B, bool = is_json_pointer_of<A, B>::value>
|
||||
template<typename Compare, typename A, typename B, bool = is_json_pointer_of<uncvref_t<A>, uncvref_t<B>>::value>
|
||||
struct is_comparable : std::false_type {};
|
||||
|
||||
template<typename Compare, typename A, typename B>
|
||||
@@ -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
|
||||
|
||||
@@ -858,6 +858,25 @@ TEST_CASE("JSON pointers")
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("value(json_pointer, default) with ordered_json #5664")
|
||||
{
|
||||
// ordered_json's transparent object comparator made value()'s
|
||||
// is_comparable_with_object_key check (which passes the pointer as
|
||||
// a reference) instantiate the deprecated json_pointer/string
|
||||
// comparison; this must compile without relying on it. The
|
||||
// deprecation warning itself is not observable here, since the
|
||||
// unit test build disables -Wdeprecated-declarations (see
|
||||
// cmake/clang_flags.cmake); it was checked manually instead.
|
||||
const nlohmann::ordered_json j = {{"n", 1}, {"s", "text"}};
|
||||
const nlohmann::ordered_json::json_pointer ptr_n("/n");
|
||||
const nlohmann::ordered_json::json_pointer ptr_s("/s");
|
||||
const nlohmann::ordered_json::json_pointer ptr_missing("/missing");
|
||||
|
||||
CHECK(j.value(ptr_n, 0) == 1);
|
||||
CHECK(j.value(ptr_s, std::string("x")) == "text");
|
||||
CHECK(j.value(ptr_missing, 42) == 42);
|
||||
}
|
||||
|
||||
// build with C++20
|
||||
// JSON_HAS_CPP_20
|
||||
#if defined(__cpp_char8_t)
|
||||
|
||||
Reference in New Issue
Block a user