From 6d2ee1361b21463b86929feba80bc99eea88fd43 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 1 Oct 2026 17:09:36 +0200 Subject: [PATCH] Address review: switch in array_index, drop redundant inline - json_pointer::array_index() dispatches on array_index_status with a switch, matching the other parse_array_index() caller - drop `inline` from the function templates this PR adds or moves in from_json.hpp - reword a comment that described the change rather than the code Signed-off-by: Niels Lohmann --- .../nlohmann/detail/conversions/from_json.hpp | 8 ++-- include/nlohmann/detail/json_pointer.hpp | 33 ++++++--------- single_include/nlohmann/json.hpp | 41 ++++++++----------- 3 files changed, 34 insertions(+), 48 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 4689450f4..3f6c382ab 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -263,7 +263,7 @@ auto from_json_array_reserve(ConstructibleArrayType& arr, typename Constructible } template -inline void from_json_array_reserve(ConstructibleArrayType& /*arr*/, std::size_t /*size*/, priority_tag<0> /*unused*/) +void from_json_array_reserve(ConstructibleArrayType& /*arr*/, std::size_t /*size*/, priority_tag<0> /*unused*/) {} template -inline void from_json_pair_array_to_map(const BasicJsonType& j, MapType& m) +void from_json_pair_array_to_map(const BasicJsonType& j, MapType& m) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -533,7 +533,7 @@ inline void from_json_pair_array_to_map(const BasicJsonType& j, MapType& m) template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, typename = enable_if_t < !std::is_constructible < typename BasicJsonType::string_t, Key >::value >> -inline void from_json(const BasicJsonType& j, std::map& m) +void from_json(const BasicJsonType& j, std::map& m) { from_json_pair_array_to_map(j, m); } @@ -541,7 +541,7 @@ inline void from_json(const BasicJsonType& j, std::map::value >> -inline void from_json(const BasicJsonType& j, std::unordered_map& m) +void from_json(const BasicJsonType& j, std::unordered_map& m) { from_json_pair_array_to_map(j, m); } diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index c1e491789..c53cddb11 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -320,26 +320,19 @@ class json_pointer static typename BasicJsonType::size_type array_index(const string_t& s) { typename BasicJsonType::size_type idx{}; - const auto status = parse_array_index(s, idx); - - if (JSON_HEDLEY_UNLIKELY(status == array_index_status::leading_zero)) + switch (parse_array_index(s, idx)) { - JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr)); - } - - if (JSON_HEDLEY_UNLIKELY(status == array_index_status::not_a_number)) - { - JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr)); - } - - if (JSON_HEDLEY_UNLIKELY(status == array_index_status::unresolved)) - { - JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr)); - } - - if (JSON_HEDLEY_UNLIKELY(status == array_index_status::exceeds_size_type)) - { - JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE + case array_index_status::leading_zero: + JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr)); + case array_index_status::not_a_number: + JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr)); + case array_index_status::unresolved: + JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr)); + case array_index_status::exceeds_size_type: + JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE + case array_index_status::ok: + default: + break; } return idx; @@ -679,7 +672,7 @@ class json_pointer return nullptr; } - // a malformed index still throws parse_error.106/109; an + // a malformed index throws parse_error.106/109; an // index that is syntactically valid but cannot be // represented (out_of_range.404/410) is treated like an // out-of-range index below diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e44972cc1..29a3a6ec9 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5681,7 +5681,7 @@ auto from_json_array_reserve(ConstructibleArrayType& arr, typename Constructible } template -inline void from_json_array_reserve(ConstructibleArrayType& /*arr*/, std::size_t /*size*/, priority_tag<0> /*unused*/) +void from_json_array_reserve(ConstructibleArrayType& /*arr*/, std::size_t /*size*/, priority_tag<0> /*unused*/) {} template -inline void from_json_pair_array_to_map(const BasicJsonType& j, MapType& m) +void from_json_pair_array_to_map(const BasicJsonType& j, MapType& m) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -5951,7 +5951,7 @@ inline void from_json_pair_array_to_map(const BasicJsonType& j, MapType& m) template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, typename = enable_if_t < !std::is_constructible < typename BasicJsonType::string_t, Key >::value >> -inline void from_json(const BasicJsonType& j, std::map& m) +void from_json(const BasicJsonType& j, std::map& m) { from_json_pair_array_to_map(j, m); } @@ -5959,7 +5959,7 @@ inline void from_json(const BasicJsonType& j, std::map::value >> -inline void from_json(const BasicJsonType& j, std::unordered_map& m) +void from_json(const BasicJsonType& j, std::unordered_map& m) { from_json_pair_array_to_map(j, m); } @@ -19774,26 +19774,19 @@ class json_pointer static typename BasicJsonType::size_type array_index(const string_t& s) { typename BasicJsonType::size_type idx{}; - const auto status = parse_array_index(s, idx); - - if (JSON_HEDLEY_UNLIKELY(status == array_index_status::leading_zero)) + switch (parse_array_index(s, idx)) { - JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr)); - } - - if (JSON_HEDLEY_UNLIKELY(status == array_index_status::not_a_number)) - { - JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr)); - } - - if (JSON_HEDLEY_UNLIKELY(status == array_index_status::unresolved)) - { - JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr)); - } - - if (JSON_HEDLEY_UNLIKELY(status == array_index_status::exceeds_size_type)) - { - JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE + case array_index_status::leading_zero: + JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr)); + case array_index_status::not_a_number: + JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr)); + case array_index_status::unresolved: + JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr)); + case array_index_status::exceeds_size_type: + JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE + case array_index_status::ok: + default: + break; } return idx; @@ -20133,7 +20126,7 @@ class json_pointer return nullptr; } - // a malformed index still throws parse_error.106/109; an + // a malformed index throws parse_error.106/109; an // index that is syntactically valid but cannot be // represented (out_of_range.404/410) is treated like an // out-of-range index below