Compare commits

..
Author SHA1 Message Date
Niels Lohmann 54ed6480d5 Fix deprecated json_pointer/string operator== warning in value()
value(KeyType&&, default) is constrained on is_comparable_with_object_key,
which passes KeyType as a reference. is_comparable's dispatch on
is_json_pointer_of<A, B> only matches a json_pointer as a plain type or a
plain reference, so a const-qualified reference (as produced when KeyType
is deduced from a json_pointer argument) fell through to
is_comparable_no_json_pointer, which instantiates the deprecated
json_pointer/string comparison operators. This made ordered_json's
transparent comparator (and any transparent comparator on a custom string
type) warn under -Wdeprecated-declarations when calling
value(json_pointer, default), even though no such comparison is ever
performed. at() was already fixed for this in #5289, which does not use
is_comparable_with_object_key.

Strip references and cv-qualifiers with uncvref_t before the
is_json_pointer_of dispatch, so any reference-to-json_pointer is
recognized regardless of qualifiers.

Fixes #5664.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-09-29 23:17:00 +02:00
6 changed files with 27 additions and 131 deletions
@@ -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.
+3 -15
View File
@@ -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()
+1 -1
View File
@@ -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>
+4 -16
View File
@@ -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()
-96
View File
@@ -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
+19
View File
@@ -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)