From f18644c77975d202ee41b387bfe41931c39e3e4f Mon Sep 17 00:00:00 2001 From: Abit Gray Date: Fri, 9 Jan 2026 23:20:23 +0100 Subject: [PATCH] `parse_some` --- .../nlohmann/detail/input/input_adapters.hpp | 12 +++++ include/nlohmann/detail/input/parser.hpp | 6 +++ include/nlohmann/json.hpp | 53 +++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index be58b1110f..6f35cf858c 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -14,6 +14,7 @@ #include // begin, end, iterator_traits, random_access_iterator_tag, distance, next #include // shared_ptr, make_shared, addressof #include // accumulate +#include //span #include // streambuf #include // string, char_traits #include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer @@ -197,6 +198,17 @@ class iterator_input_adapter return count * sizeof(T); } +#ifdef __cpp_lib_span + inline constexpr std::span as_span() + { + return std::span(current, end); + } + inline constexpr std::span as_span() const + { + return std::span(current, end); + } +#endif + private: IteratorType current; IteratorType end; diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index 4b1963f4a7..df3ab41b3d 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -176,6 +176,12 @@ class parser return result; } + /// return position of last read token + constexpr position_t get_position() const noexcept + { + return m_lexer.get_position(); + } + private: template JSON_HEDLEY_NON_NULL(2) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 0b8f155ac5..a8187ce545 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -27,6 +27,7 @@ #endif // JSON_NO_IO #include // random_access_iterator_tag #include // unique_ptr +#include // span #include // string, stoi, to_string #include // declval, forward, move, pair, swap #include // vector @@ -4054,6 +4055,23 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return result; } + /// @brief deserialize one JSON object from a compatible input + /// @sa https://json.nlohmann.me/api/basic_json/parse/ + template + JSON_HEDLEY_WARN_UNUSED_RESULT + static std::pair parse_some(InputType&& i, + parser_callback_t cb = nullptr, + const bool allow_exceptions = true, + const bool ignore_comments = false, + const bool ignore_trailing_commas = false) + { + basic_json result; + auto p = parser(detail::input_adapter(std::forward(i)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); // cppcheck-suppress[accessMoved,accessForwarded] + p.parse(false, result); + detail::position_t& end = p.get_position(); + return {result, end}; + } + /// @brief deserialize from a pair of character iterators /// @sa https://json.nlohmann.me/api/basic_json/parse/ template @@ -4070,6 +4088,24 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return result; } + /// @brief deserialize from a pair of character iterators + /// @sa https://json.nlohmann.me/api/basic_json/parse/ + template + JSON_HEDLEY_WARN_UNUSED_RESULT + static std::pair parse_some(IteratorType first, + IteratorType last, + parser_callback_t cb = nullptr, + const bool allow_exceptions = true, + const bool ignore_comments = false, + const bool ignore_trailing_commas = false) + { + basic_json result; + auto p = parser(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); // cppcheck-suppress[accessMoved] + p.parse(false, result); + IteratorType end = first + p.get_position(); + return {result, end}; + } + JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len)) static basic_json parse(detail::span_input_adapter&& i, @@ -4083,6 +4119,23 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return result; } +#ifdef __cpp_lib_span + JSON_HEDLEY_WARN_UNUSED_RESULT + JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len)) + static std::pair> parse_some(detail::span_input_adapter&& i, + parser_callback_t cb = nullptr, + const bool allow_exceptions = true, + const bool ignore_comments = false, + const bool ignore_trailing_commas = false) + { + basic_json result; + auto p = parser(i.get(), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); // cppcheck-suppress[accessMoved] + p.parse(false, result); + ::std::span remainder = i.get().as_span().subspan(p.get_position()); + return {result, remainder}; + } +#endif + /// @brief check if the input is valid JSON /// @sa https://json.nlohmann.me/api/basic_json/accept/ template