From d1d0e303a06e26b0df4d952ff869068901a9a99d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 11 Oct 2026 12:44:42 +0200 Subject: [PATCH] Report one-character non-numeric array indices like basic_json in the view Develop's #5800 made basic_json report a JSON pointer array index such as "/a" as parse_error.109 instead of out_of_range.404, as longer ones were; the view's array_index() follows, with "-" and the empty token still unresolved. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/pointer.hpp | 4 +++- single_include/nlohmann/json_view.hpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/view/pointer.hpp b/include/nlohmann/detail/view/pointer.hpp index d2ac497d7..c95e5a165 100644 --- a/include/nlohmann/detail/view/pointer.hpp +++ b/include/nlohmann/detail/view/pointer.hpp @@ -51,7 +51,9 @@ index_status array_index(const StringType& s, std::size_t& idx) noexcept { return index_status::leading_zero; } - if (s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')) + // as basic_json: also a single character, but "-" and the empty token are + // left to the conversion below and reported as unresolved + if (!s.empty() && !(s.size() == 1 && s[0] == '-') && !(s[0] >= '0' && s[0] <= '9')) { return index_status::not_number; } diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index de979270e..53bdf5b60 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -2551,7 +2551,9 @@ index_status array_index(const StringType& s, std::size_t& idx) noexcept { return index_status::leading_zero; } - if (s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')) + // as basic_json: also a single character, but "-" and the empty token are + // left to the conversion below and reported as unresolved + if (!s.empty() && !(s.size() == 1 && s[0] == '-') && !(s[0] >= '0' && s[0] <= '9')) { return index_status::not_number; }