Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
#include <memory> // shared_ptr, make_shared, addressof
#include <numeric> // accumulate
#include <span> //span
#include <streambuf> // streambuf
#include <string> // string, char_traits
#include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
Expand Down Expand Up @@ -197,6 +198,17 @@ class iterator_input_adapter
return count * sizeof(T);
}

#ifdef __cpp_lib_span
inline constexpr std::span<char_type> as_span()
{
return std::span<char_type>(current, end);
}
inline constexpr std::span<const char_type> as_span() const
{
return std::span<const char_type>(current, end);
}
#endif

private:
IteratorType current;
IteratorType end;
Expand Down
6 changes: 6 additions & 0 deletions include/nlohmann/detail/input/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename SAX>
JSON_HEDLEY_NON_NULL(2)
Expand Down
53 changes: 53 additions & 0 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#endif // JSON_NO_IO
#include <iterator> // random_access_iterator_tag
#include <memory> // unique_ptr
#include <span> // span
#include <string> // string, stoi, to_string
#include <utility> // declval, forward, move, pair, swap
#include <vector> // vector
Expand Down Expand Up @@ -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<typename InputType>
JSON_HEDLEY_WARN_UNUSED_RESULT
static std::pair<basic_json, detail::position_t> 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<InputType>(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<typename IteratorType>
Expand All @@ -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<typename IteratorType>
JSON_HEDLEY_WARN_UNUSED_RESULT
static std::pair<basic_json, IteratorType> 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,
Expand All @@ -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<basic_json, std::span<char>> 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<char> 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<typename InputType>
Expand Down