From 3724295333fbb268a3cbeca5e0e1b91550bd8c3b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 2 Nov 2026 19:10:16 +0100 Subject: [PATCH] json_view: a value that is not found has no bytes in its exception context Keeping the byte range of the root value (#5990) dropped the found flag of value_path(), but a root has no tokens and is found, while a view of a value that was removed from the document is not found. Without the flag, the removed value got the bytes of its stale node, and "json_view exceptions name the path and the bytes of the value" failed on the macOS jobs with JSON_DIAGNOSTICS and JSON_DIAGNOSTIC_POSITIONS. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/errors.hpp | 8 ++++++-- single_include/nlohmann/json_view.hpp | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/view/errors.hpp b/include/nlohmann/detail/view/errors.hpp index 3c97cef5c..74597edf0 100644 --- a/include/nlohmann/detail/view/errors.hpp +++ b/include/nlohmann/detail/view/errors.hpp @@ -251,8 +251,12 @@ std::string diagnostics(const error_site site) std::string context; #if JSON_DIAGNOSTICS std::vector tokens; - // a value that is not found leaves no tokens, like the root - static_cast(edited ? value_path(d, site.value(), tokens) : value_path(d, site.value(), tokens)); + const bool found = edited ? value_path(d, site.value(), tokens) : value_path(d, site.value(), tokens); + if (!found) + { + // a value that is not found has no path, and with it no bytes + return context; + } if (!tokens.empty()) { // the root value has no path, but still has a byte range diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index ea04acdbe..1c6cd6cef 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -5080,8 +5080,12 @@ std::string diagnostics(const error_site site) std::string context; #if JSON_DIAGNOSTICS std::vector tokens; - // a value that is not found leaves no tokens, like the root - static_cast(edited ? value_path(d, site.value(), tokens) : value_path(d, site.value(), tokens)); + const bool found = edited ? value_path(d, site.value(), tokens) : value_path(d, site.value(), tokens); + if (!found) + { + // a value that is not found has no path, and with it no bytes + return context; + } if (!tokens.empty()) { // the root value has no path, but still has a byte range