Keep the byte range in exceptions about the root value (#5987)

With JSON_DIAGNOSTICS and JSON_DIAGNOSTIC_POSITIONS both set, exception::diagnostics()
and the json_view diagnostics() returned early for a value without a path (the
root), dropping "(bytes a-b)". Build the "(path) " prefix only when there are
tokens and always append the byte range.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-01 21:03:07 +01:00
1 parent ddc34f546a
commit ae792a4c95
7 files changed
+88 -48

No files matched your search

+10 -9
View File
@@ -5695,18 +5695,19 @@ class exception : public std::exception
// all other parent types (primitives) contribute no token
}
if (tokens.empty())
// the root value has no path, but still has a byte range
std::string prefix;
if (!tokens.empty())
{
return "";
const auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
[](const std::string & a, const std::string & b)
{
return concat(a, '/', detail::escape(b));
});
prefix = concat('(', str, ") ");
}
auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
[](const std::string & a, const std::string & b)
{
return concat(a, '/', detail::escape(b));
});
return concat('(', str, ") ", get_byte_positions(leaf_element));
return concat(prefix, get_byte_positions(leaf_element));
#else
return get_byte_positions(leaf_element);
#endif