duck-typed object input adapters

This commit is contained in:
Francois Chabot 2020-02-19 14:59:31 -05:00
parent a0c4fc945a
commit 2e2cf02cfd
8 changed files with 202 additions and 207 deletions

View file

@ -31,10 +31,9 @@ namespace detail
/*! /*!
@brief deserialization of CBOR, MessagePack, and UBJSON values @brief deserialization of CBOR, MessagePack, and UBJSON values
*/ */
template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>, typename InputAdapterType = input_adapter_protocol> template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
class binary_reader class binary_reader
{ {
using input_adapter_ptr_t = std::shared_ptr<InputAdapterType>;
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
@ -47,10 +46,9 @@ class binary_reader
@param[in] adapter input adapter to read from @param[in] adapter input adapter to read from
*/ */
explicit binary_reader(input_adapter_ptr_t adapter) : ia(std::move(adapter)) explicit binary_reader(InputAdapterType&& adapter) : ia(std::move(adapter))
{ {
(void)detail::is_sax_static_asserts<SAX, BasicJsonType> {}; (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
assert(ia);
} }
// make class move-only // make class move-only
@ -1810,7 +1808,7 @@ class binary_reader
int get() int get()
{ {
++chars_read; ++chars_read;
return current = ia->get_character(); return current = ia.get_character();
} }
/*! /*!
@ -1966,7 +1964,7 @@ class binary_reader
private: private:
/// input adapter /// input adapter
input_adapter_ptr_t ia = nullptr; InputAdapterType ia;
/// the current character /// the current character
int current = std::char_traits<char>::eof(); int current = std::char_traits<char>::eof();

View file

@ -27,29 +27,11 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson };
// input adapters // // input adapters //
//////////////////// ////////////////////
/*!
@brief abstract input adapter interface
Produces a stream of std::char_traits<char>::int_type characters from a
std::istream, a buffer, or some other input type. Accepts the return of
exactly one non-EOF character for future input. The int_type characters
returned consist of all valid char values as positive values (typically
unsigned char), plus an EOF value outside that range, specified by the value
of the function std::char_traits<char>::eof(). This value is typically -1, but
could be any arbitrary value which is not a valid char value.
*/
struct input_adapter_protocol
{
/// get a character [0,255] or std::char_traits<char>::eof().
virtual std::char_traits<char>::int_type get_character() = 0;
virtual ~input_adapter_protocol() = default;
};
/*! /*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter. buffer. This adapter is a very low level adapter.
*/ */
class file_input_adapter final : public input_adapter_protocol class file_input_adapter
{ {
public: public:
JSON_HEDLEY_NON_NULL(2) JSON_HEDLEY_NON_NULL(2)
@ -62,9 +44,8 @@ class file_input_adapter final : public input_adapter_protocol
file_input_adapter(file_input_adapter&&) = default; file_input_adapter(file_input_adapter&&) = default;
file_input_adapter& operator=(const file_input_adapter&) = delete; file_input_adapter& operator=(const file_input_adapter&) = delete;
file_input_adapter& operator=(file_input_adapter&&) = default; file_input_adapter& operator=(file_input_adapter&&) = default;
~file_input_adapter() override = default;
std::char_traits<char>::int_type get_character() noexcept override std::char_traits<char>::int_type get_character() noexcept
{ {
return std::fgetc(m_file); return std::fgetc(m_file);
} }
@ -84,48 +65,68 @@ characters following those used in parsing the JSON input. Clears the
std::istream flags; any input errors (e.g., EOF) will be detected by the first std::istream flags; any input errors (e.g., EOF) will be detected by the first
subsequent call for input from the std::istream. subsequent call for input from the std::istream.
*/ */
class input_stream_adapter final : public input_adapter_protocol class input_stream_adapter
{ {
public: public:
~input_stream_adapter() override ~input_stream_adapter()
{ {
// clear stream flags; we use underlying streambuf I/O, do not // clear stream flags; we use underlying streambuf I/O, do not
// maintain ifstream flags, except eof // maintain ifstream flags, except eof
is.clear(is.rdstate() & std::ios::eofbit); if (is)
{
is->clear(is->rdstate() & std::ios::eofbit);
}
} }
explicit input_stream_adapter(std::istream& i) explicit input_stream_adapter(std::istream& i)
: is(i), sb(*i.rdbuf()) : is(&i), sb(i.rdbuf())
{} {}
// delete because of pointer members // delete because of pointer members
input_stream_adapter(const input_stream_adapter&) = delete; input_stream_adapter(const input_stream_adapter&) = delete;
input_stream_adapter& operator=(input_stream_adapter&) = delete; input_stream_adapter& operator=(input_stream_adapter&) = delete;
input_stream_adapter(input_stream_adapter&&) = delete;
input_stream_adapter& operator=(input_stream_adapter&&) = delete; input_stream_adapter(input_stream_adapter&& rhs) : is(rhs.is), sb(rhs.sb)
{
rhs.is = nullptr;
rhs.sb = nullptr;
}
input_stream_adapter& operator=(input_stream_adapter&& rhs)
{
if (is)
{
is->clear(is->rdstate() & std::ios::eofbit);
}
is = rhs.is;
sb = rhs.sb;
rhs.is = nullptr;
rhs.sb = nullptr;
}
// std::istream/std::streambuf use std::char_traits<char>::to_int_type, to // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
// ensure that std::char_traits<char>::eof() and the character 0xFF do not // ensure that std::char_traits<char>::eof() and the character 0xFF do not
// end up as the same value, eg. 0xFFFFFFFF. // end up as the same value, eg. 0xFFFFFFFF.
std::char_traits<char>::int_type get_character() override std::char_traits<char>::int_type get_character()
{ {
auto res = sb.sbumpc(); auto res = sb->sbumpc();
// set eof manually, as we don't use the istream interface. // set eof manually, as we don't use the istream interface.
if (res == EOF) if (res == EOF)
{ {
is.clear(is.rdstate() | std::ios::eofbit); is->clear(is->rdstate() | std::ios::eofbit);
} }
return res; return res;
} }
private: private:
/// the associated input stream /// the associated input stream
std::istream& is; std::istream* is = nullptr;
std::streambuf& sb; std::streambuf* sb = nullptr;
}; };
/// input adapter for buffer input /// input adapter for buffer input
class input_buffer_adapter final : public input_adapter_protocol class input_buffer_adapter
{ {
public: public:
input_buffer_adapter(const char* b, const std::size_t l) noexcept input_buffer_adapter(const char* b, const std::size_t l) noexcept
@ -135,11 +136,10 @@ class input_buffer_adapter final : public input_adapter_protocol
// delete because of pointer members // delete because of pointer members
input_buffer_adapter(const input_buffer_adapter&) = delete; input_buffer_adapter(const input_buffer_adapter&) = delete;
input_buffer_adapter& operator=(input_buffer_adapter&) = delete; input_buffer_adapter& operator=(input_buffer_adapter&) = delete;
input_buffer_adapter(input_buffer_adapter&&) = delete; input_buffer_adapter(input_buffer_adapter&&) = default;
input_buffer_adapter& operator=(input_buffer_adapter&&) = delete; input_buffer_adapter& operator=(input_buffer_adapter&&) = default;
~input_buffer_adapter() override = default;
std::char_traits<char>::int_type get_character() noexcept override std::char_traits<char>::int_type get_character() noexcept
{ {
if (JSON_HEDLEY_LIKELY(cursor < limit)) if (JSON_HEDLEY_LIKELY(cursor < limit))
{ {
@ -282,14 +282,14 @@ struct wide_string_input_helper<WideStringType, 2>
}; };
template<typename WideStringType> template<typename WideStringType>
class wide_string_input_adapter final : public input_adapter_protocol class wide_string_input_adapter
{ {
public: public:
explicit wide_string_input_adapter(const WideStringType& w) noexcept explicit wide_string_input_adapter(const WideStringType& w) noexcept
: str(w) : str(w)
{} {}
std::char_traits<char>::int_type get_character() noexcept override std::char_traits<char>::int_type get_character() noexcept
{ {
// check if buffer needs to be filled // check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled) if (utf8_bytes_index == utf8_bytes_filled)
@ -328,19 +328,19 @@ class wide_string_input_adapter final : public input_adapter_protocol
std::size_t utf8_bytes_filled = 0; std::size_t utf8_bytes_filled = 0;
}; };
inline std::shared_ptr<file_input_adapter> input_adapter(std::FILE* file) inline file_input_adapter input_adapter(std::FILE* file)
{ {
return std::make_shared<file_input_adapter>(file); return file_input_adapter(file);
} }
inline std::shared_ptr<input_stream_adapter> input_adapter(std::istream& stream) inline input_stream_adapter input_adapter(std::istream& stream)
{ {
return std::make_shared<input_stream_adapter>(stream); return input_stream_adapter(stream);
} }
inline std::shared_ptr<input_stream_adapter> input_adapter(std::istream&& stream) inline input_stream_adapter input_adapter(std::istream&& stream)
{ {
return std::make_shared<input_stream_adapter>(stream); return input_stream_adapter(stream);
} }
template<typename CharT, template<typename CharT,
@ -349,9 +349,9 @@ template<typename CharT,
std::is_integral<typename std::remove_pointer<CharT>::type>::value and std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1, sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0> int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(CharT b, std::size_t l) input_buffer_adapter input_adapter(CharT b, std::size_t l)
{ {
return std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l); return input_buffer_adapter(reinterpret_cast<const char*>(b), l);
} }
template<typename CharT, template<typename CharT,
@ -360,7 +360,7 @@ template<typename CharT,
std::is_integral<typename std::remove_pointer<CharT>::type>::value and std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1, sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0> int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(CharT b) input_buffer_adapter input_adapter(CharT b)
{ {
return input_adapter(reinterpret_cast<const char*>(b), return input_adapter(reinterpret_cast<const char*>(b),
std::strlen(reinterpret_cast<const char*>(b))); std::strlen(reinterpret_cast<const char*>(b)));
@ -370,7 +370,7 @@ template<class IteratorType,
typename std::enable_if< typename std::enable_if<
std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value, std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
int>::type = 0> int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(IteratorType first, IteratorType last) input_buffer_adapter input_adapter(IteratorType first, IteratorType last)
{ {
#ifndef NDEBUG #ifndef NDEBUG
// assertion to check that the iterator range is indeed contiguous, // assertion to check that the iterator range is indeed contiguous,
@ -394,43 +394,43 @@ std::shared_ptr<input_buffer_adapter> input_adapter(IteratorType first, Iterator
if (JSON_HEDLEY_LIKELY(len > 0)) if (JSON_HEDLEY_LIKELY(len > 0))
{ {
// there is at least one element: use the address of first // there is at least one element: use the address of first
return std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(&(*first)), len); return input_buffer_adapter(reinterpret_cast<const char*>(&(*first)), len);
} }
else else
{ {
// the address of first cannot be used: use nullptr // the address of first cannot be used: use nullptr
return std::make_shared<input_buffer_adapter>(nullptr, len); return input_buffer_adapter(nullptr, len);
} }
} }
inline std::shared_ptr<wide_string_input_adapter<std::wstring>> input_adapter(const std::wstring& ws) inline wide_string_input_adapter<std::wstring> input_adapter(const std::wstring& ws)
{ {
return std::make_shared<wide_string_input_adapter<std::wstring>>(ws); return wide_string_input_adapter<std::wstring>(ws);
} }
inline std::shared_ptr<wide_string_input_adapter<std::u16string>> input_adapter(const std::u16string& ws) inline wide_string_input_adapter<std::u16string> input_adapter(const std::u16string& ws)
{ {
return std::make_shared<wide_string_input_adapter<std::u16string>>(ws); return wide_string_input_adapter<std::u16string>(ws);
} }
inline std::shared_ptr<wide_string_input_adapter<std::u32string>> input_adapter(const std::u32string& ws) inline wide_string_input_adapter<std::u32string> input_adapter(const std::u32string& ws)
{ {
return std::make_shared<wide_string_input_adapter<std::u32string>>(ws); return wide_string_input_adapter<std::u32string>(ws);
} }
template<class ContiguousContainer, typename template<class ContiguousContainer, typename
std::enable_if<not std::is_pointer<ContiguousContainer>::value and std::enable_if<not std::is_pointer<ContiguousContainer>::value and
std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value, std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value,
int>::type = 0> int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(const ContiguousContainer& c) input_buffer_adapter input_adapter(const ContiguousContainer& c)
{ {
return input_adapter(std::begin(c), std::end(c)); return input_adapter(std::begin(c), std::end(c));
} }
template<class T, std::size_t N> template<class T, std::size_t N>
std::shared_ptr<input_buffer_adapter> input_adapter(T (&array)[N]) input_buffer_adapter input_adapter(T (&array)[N])
{ {
return input_adapter(std::begin(array), std::end(array)); return input_adapter(std::begin(array), std::end(array));
} }
@ -448,7 +448,7 @@ class span_input_adapter
sizeof(typename std::remove_pointer<CharT>::type) == 1, sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0> int>::type = 0>
span_input_adapter(CharT b, std::size_t l) span_input_adapter(CharT b, std::size_t l)
: ia(std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l)) {} : ia(reinterpret_cast<const char*>(b), l) {}
template<typename CharT, template<typename CharT,
typename std::enable_if< typename std::enable_if<
@ -479,13 +479,13 @@ class span_input_adapter
span_input_adapter(const ContiguousContainer& c) span_input_adapter(const ContiguousContainer& c)
: span_input_adapter(std::begin(c), std::end(c)) {} : span_input_adapter(std::begin(c), std::end(c)) {}
std::shared_ptr<input_buffer_adapter> get() input_buffer_adapter&& get()
{ {
return ia; return std::move(ia);
} }
private: private:
std::shared_ptr<input_buffer_adapter> ia = nullptr; input_buffer_adapter ia;
}; };
} // namespace detail } // namespace detail
} // namespace nlohmann } // namespace nlohmann

View file

@ -99,11 +99,9 @@ class lexer_base
This class organizes the lexical analysis during JSON deserialization. This class organizes the lexical analysis during JSON deserialization.
*/ */
template<typename BasicJsonType, typename InputAdapterType = input_adapter_protocol> template<typename BasicJsonType, typename InputAdapterType>
class lexer : public lexer_base<BasicJsonType> class lexer : public lexer_base<BasicJsonType>
{ {
using input_adapter_ptr_t = std::shared_ptr<InputAdapterType>;
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
@ -112,7 +110,7 @@ class lexer : public lexer_base<BasicJsonType>
public: public:
using token_type = typename lexer_base<BasicJsonType>::token_type; using token_type = typename lexer_base<BasicJsonType>::token_type;
explicit lexer(input_adapter_ptr_t&& adapter) explicit lexer(InputAdapterType&& adapter)
: ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {} : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {}
// delete because of pointer members // delete because of pointer members
@ -1264,7 +1262,7 @@ scan_number_done:
} }
else else
{ {
current = ia->get_character(); current = ia.get_character();
} }
if (JSON_HEDLEY_LIKELY(current != std::char_traits<char>::eof())) if (JSON_HEDLEY_LIKELY(current != std::char_traits<char>::eof()))
@ -1488,7 +1486,7 @@ scan_number_done:
private: private:
/// input adapter /// input adapter
input_adapter_ptr_t ia = nullptr; InputAdapterType ia;
/// the current character /// the current character
std::char_traits<char>::int_type current = std::char_traits<char>::eof(); std::char_traits<char>::int_type current = std::char_traits<char>::eof();

View file

@ -49,10 +49,9 @@ using parser_callback_t =
This class implements a recursive descent parser. This class implements a recursive descent parser.
*/ */
template<typename BasicJsonType, typename InputAdapterType = input_adapter_protocol> template<typename BasicJsonType, typename InputAdapterType>
class parser class parser
{ {
using input_adapter_ptr_t = std::shared_ptr<InputAdapterType>;
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
@ -62,7 +61,7 @@ class parser
public: public:
/// a parser reading from an input adapter /// a parser reading from an input adapter
explicit parser(input_adapter_ptr_t&& adapter, explicit parser(InputAdapterType&& adapter,
const parser_callback_t<BasicJsonType> cb = nullptr, const parser_callback_t<BasicJsonType> cb = nullptr,
const bool allow_exceptions_ = true) const bool allow_exceptions_ = true)
: callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_) : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)

View file

@ -1326,7 +1326,7 @@ class binary_writer
private: private:
/// whether we can assume little endianess /// whether we can assume little endianess
const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess(); const bool is_little_endian = binary_reader<BasicJsonType, detail::input_buffer_adapter>::little_endianess();
/// the output /// the output
output_adapter_t<CharType> oa = nullptr; output_adapter_t<CharType> oa = nullptr;

View file

@ -175,7 +175,7 @@ class basic_json
friend class ::nlohmann::detail::iter_impl; friend class ::nlohmann::detail::iter_impl;
template<typename BasicJsonType, typename CharType> template<typename BasicJsonType, typename CharType>
friend class ::nlohmann::detail::binary_writer; friend class ::nlohmann::detail::binary_writer;
template<typename BasicJsonType, typename SAX, typename InputType> template<typename BasicJsonType, typename InputType, typename SAX>
friend class ::nlohmann::detail::binary_reader; friend class ::nlohmann::detail::binary_reader;
template<typename BasicJsonType> template<typename BasicJsonType>
friend class ::nlohmann::detail::json_sax_dom_parser; friend class ::nlohmann::detail::json_sax_dom_parser;
@ -190,7 +190,7 @@ class basic_json
template<typename InputAdapterType> template<typename InputAdapterType>
static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser( static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
std::shared_ptr<InputAdapterType> adapter, InputAdapterType adapter,
detail::parser_callback_t<basic_json>cb = nullptr, detail::parser_callback_t<basic_json>cb = nullptr,
bool allow_exceptions = true bool allow_exceptions = true
) )
@ -210,7 +210,8 @@ class basic_json
template<typename CharType> template<typename CharType>
using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>; using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
using binary_reader = ::nlohmann::detail::binary_reader<basic_json>; template<typename InputType>
using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>; template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
using serializer = ::nlohmann::detail::serializer<basic_json>; using serializer = ::nlohmann::detail::serializer<basic_json>;
@ -6303,11 +6304,10 @@ class basic_json
{ {
assert(sax); assert(sax);
auto input_adapter = detail::input_adapter(std::forward<InputType>(i)); auto ia = detail::input_adapter(std::forward<InputType>(i));
using adapter_type = typename decltype(input_adapter)::element_type;
return format == input_format_t::json return format == input_format_t::json
? parser(std::move(input_adapter)).sax_parse(sax, strict) ? parser(std::move(ia)).sax_parse(sax, strict)
: detail::binary_reader<basic_json, SAX, adapter_type>(std::move(input_adapter)).sax_parse(format, sax, strict); : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
} }
template <typename SAX> template <typename SAX>
@ -6318,11 +6318,10 @@ class basic_json
{ {
assert(sax); assert(sax);
auto input_adapter = i.get(); auto ia = i.get();
using adapter_type = typename decltype(input_adapter)::element_type;
return format == input_format_t::json return format == input_format_t::json
? parser(std::move(input_adapter)).sax_parse(sax, strict) ? parser(std::move(ia)).sax_parse(sax, strict)
: detail::binary_reader<basic_json, SAX, adapter_type>(std::move(input_adapter)).sax_parse(format, sax, strict); : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
} }
@ -7026,7 +7025,8 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::input_adapter(std::forward<InputType>(i))).sax_parse(input_format_t::cbor, &sdp, strict); auto ia = detail::input_adapter(std::forward<InputType>(i));
const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7042,7 +7042,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7053,7 +7053,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(i.get()).sax_parse(input_format_t::cbor, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(i.get()).sax_parse(input_format_t::cbor, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7147,7 +7147,8 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::input_adapter(std::forward<InputType>(i))).sax_parse(input_format_t::msgpack, &sdp, strict); auto ia = detail::input_adapter(std::forward<InputType>(i));
const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7163,7 +7164,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7175,7 +7176,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7249,7 +7250,8 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::input_adapter(std::forward<InputType>(i))).sax_parse(input_format_t::ubjson, &sdp, strict); auto ia = detail::input_adapter(std::forward<InputType>(i));
const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7265,7 +7267,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7276,7 +7278,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7349,7 +7351,8 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::input_adapter(std::forward<InputType>(i))).sax_parse(input_format_t::bson, &sdp, strict); auto ia = detail::input_adapter(std::forward<InputType>(i));
const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7365,7 +7368,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -7376,7 +7379,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(i.get()).sax_parse(input_format_t::bson, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(i.get()).sax_parse(input_format_t::bson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
/// @} /// @}

View file

@ -3890,29 +3890,11 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson };
// input adapters // // input adapters //
//////////////////// ////////////////////
/*!
@brief abstract input adapter interface
Produces a stream of std::char_traits<char>::int_type characters from a
std::istream, a buffer, or some other input type. Accepts the return of
exactly one non-EOF character for future input. The int_type characters
returned consist of all valid char values as positive values (typically
unsigned char), plus an EOF value outside that range, specified by the value
of the function std::char_traits<char>::eof(). This value is typically -1, but
could be any arbitrary value which is not a valid char value.
*/
struct input_adapter_protocol
{
/// get a character [0,255] or std::char_traits<char>::eof().
virtual std::char_traits<char>::int_type get_character() = 0;
virtual ~input_adapter_protocol() = default;
};
/*! /*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter. buffer. This adapter is a very low level adapter.
*/ */
class file_input_adapter final : public input_adapter_protocol class file_input_adapter
{ {
public: public:
JSON_HEDLEY_NON_NULL(2) JSON_HEDLEY_NON_NULL(2)
@ -3925,9 +3907,8 @@ class file_input_adapter final : public input_adapter_protocol
file_input_adapter(file_input_adapter&&) = default; file_input_adapter(file_input_adapter&&) = default;
file_input_adapter& operator=(const file_input_adapter&) = delete; file_input_adapter& operator=(const file_input_adapter&) = delete;
file_input_adapter& operator=(file_input_adapter&&) = default; file_input_adapter& operator=(file_input_adapter&&) = default;
~file_input_adapter() override = default;
std::char_traits<char>::int_type get_character() noexcept override std::char_traits<char>::int_type get_character() noexcept
{ {
return std::fgetc(m_file); return std::fgetc(m_file);
} }
@ -3947,48 +3928,68 @@ characters following those used in parsing the JSON input. Clears the
std::istream flags; any input errors (e.g., EOF) will be detected by the first std::istream flags; any input errors (e.g., EOF) will be detected by the first
subsequent call for input from the std::istream. subsequent call for input from the std::istream.
*/ */
class input_stream_adapter final : public input_adapter_protocol class input_stream_adapter
{ {
public: public:
~input_stream_adapter() override ~input_stream_adapter()
{ {
// clear stream flags; we use underlying streambuf I/O, do not // clear stream flags; we use underlying streambuf I/O, do not
// maintain ifstream flags, except eof // maintain ifstream flags, except eof
is.clear(is.rdstate() & std::ios::eofbit); if (is)
{
is->clear(is->rdstate() & std::ios::eofbit);
}
} }
explicit input_stream_adapter(std::istream& i) explicit input_stream_adapter(std::istream& i)
: is(i), sb(*i.rdbuf()) : is(&i), sb(i.rdbuf())
{} {}
// delete because of pointer members // delete because of pointer members
input_stream_adapter(const input_stream_adapter&) = delete; input_stream_adapter(const input_stream_adapter&) = delete;
input_stream_adapter& operator=(input_stream_adapter&) = delete; input_stream_adapter& operator=(input_stream_adapter&) = delete;
input_stream_adapter(input_stream_adapter&&) = delete;
input_stream_adapter& operator=(input_stream_adapter&&) = delete; input_stream_adapter(input_stream_adapter&& rhs) : is(rhs.is), sb(rhs.sb)
{
rhs.is = nullptr;
rhs.sb = nullptr;
}
input_stream_adapter& operator=(input_stream_adapter&& rhs)
{
if (is)
{
is->clear(is->rdstate() & std::ios::eofbit);
}
is = rhs.is;
sb = rhs.sb;
rhs.is = nullptr;
rhs.sb = nullptr;
}
// std::istream/std::streambuf use std::char_traits<char>::to_int_type, to // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
// ensure that std::char_traits<char>::eof() and the character 0xFF do not // ensure that std::char_traits<char>::eof() and the character 0xFF do not
// end up as the same value, eg. 0xFFFFFFFF. // end up as the same value, eg. 0xFFFFFFFF.
std::char_traits<char>::int_type get_character() override std::char_traits<char>::int_type get_character()
{ {
auto res = sb.sbumpc(); auto res = sb->sbumpc();
// set eof manually, as we don't use the istream interface. // set eof manually, as we don't use the istream interface.
if (res == EOF) if (res == EOF)
{ {
is.clear(is.rdstate() | std::ios::eofbit); is->clear(is->rdstate() | std::ios::eofbit);
} }
return res; return res;
} }
private: private:
/// the associated input stream /// the associated input stream
std::istream& is; std::istream* is = nullptr;
std::streambuf& sb; std::streambuf* sb = nullptr;
}; };
/// input adapter for buffer input /// input adapter for buffer input
class input_buffer_adapter final : public input_adapter_protocol class input_buffer_adapter
{ {
public: public:
input_buffer_adapter(const char* b, const std::size_t l) noexcept input_buffer_adapter(const char* b, const std::size_t l) noexcept
@ -3998,11 +3999,10 @@ class input_buffer_adapter final : public input_adapter_protocol
// delete because of pointer members // delete because of pointer members
input_buffer_adapter(const input_buffer_adapter&) = delete; input_buffer_adapter(const input_buffer_adapter&) = delete;
input_buffer_adapter& operator=(input_buffer_adapter&) = delete; input_buffer_adapter& operator=(input_buffer_adapter&) = delete;
input_buffer_adapter(input_buffer_adapter&&) = delete; input_buffer_adapter(input_buffer_adapter&&) = default;
input_buffer_adapter& operator=(input_buffer_adapter&&) = delete; input_buffer_adapter& operator=(input_buffer_adapter&&) = default;
~input_buffer_adapter() override = default;
std::char_traits<char>::int_type get_character() noexcept override std::char_traits<char>::int_type get_character() noexcept
{ {
if (JSON_HEDLEY_LIKELY(cursor < limit)) if (JSON_HEDLEY_LIKELY(cursor < limit))
{ {
@ -4145,14 +4145,14 @@ struct wide_string_input_helper<WideStringType, 2>
}; };
template<typename WideStringType> template<typename WideStringType>
class wide_string_input_adapter final : public input_adapter_protocol class wide_string_input_adapter
{ {
public: public:
explicit wide_string_input_adapter(const WideStringType& w) noexcept explicit wide_string_input_adapter(const WideStringType& w) noexcept
: str(w) : str(w)
{} {}
std::char_traits<char>::int_type get_character() noexcept override std::char_traits<char>::int_type get_character() noexcept
{ {
// check if buffer needs to be filled // check if buffer needs to be filled
if (utf8_bytes_index == utf8_bytes_filled) if (utf8_bytes_index == utf8_bytes_filled)
@ -4191,19 +4191,19 @@ class wide_string_input_adapter final : public input_adapter_protocol
std::size_t utf8_bytes_filled = 0; std::size_t utf8_bytes_filled = 0;
}; };
inline std::shared_ptr<file_input_adapter> input_adapter(std::FILE* file) inline file_input_adapter input_adapter(std::FILE* file)
{ {
return std::make_shared<file_input_adapter>(file); return file_input_adapter(file);
} }
inline std::shared_ptr<input_stream_adapter> input_adapter(std::istream& stream) inline input_stream_adapter input_adapter(std::istream& stream)
{ {
return std::make_shared<input_stream_adapter>(stream); return input_stream_adapter(stream);
} }
inline std::shared_ptr<input_stream_adapter> input_adapter(std::istream&& stream) inline input_stream_adapter input_adapter(std::istream&& stream)
{ {
return std::make_shared<input_stream_adapter>(stream); return input_stream_adapter(stream);
} }
template<typename CharT, template<typename CharT,
@ -4212,9 +4212,9 @@ template<typename CharT,
std::is_integral<typename std::remove_pointer<CharT>::type>::value and std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1, sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0> int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(CharT b, std::size_t l) input_buffer_adapter input_adapter(CharT b, std::size_t l)
{ {
return std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l); return input_buffer_adapter(reinterpret_cast<const char*>(b), l);
} }
template<typename CharT, template<typename CharT,
@ -4223,7 +4223,7 @@ template<typename CharT,
std::is_integral<typename std::remove_pointer<CharT>::type>::value and std::is_integral<typename std::remove_pointer<CharT>::type>::value and
sizeof(typename std::remove_pointer<CharT>::type) == 1, sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0> int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(CharT b) input_buffer_adapter input_adapter(CharT b)
{ {
return input_adapter(reinterpret_cast<const char*>(b), return input_adapter(reinterpret_cast<const char*>(b),
std::strlen(reinterpret_cast<const char*>(b))); std::strlen(reinterpret_cast<const char*>(b)));
@ -4233,7 +4233,7 @@ template<class IteratorType,
typename std::enable_if< typename std::enable_if<
std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value, std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
int>::type = 0> int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(IteratorType first, IteratorType last) input_buffer_adapter input_adapter(IteratorType first, IteratorType last)
{ {
#ifndef NDEBUG #ifndef NDEBUG
// assertion to check that the iterator range is indeed contiguous, // assertion to check that the iterator range is indeed contiguous,
@ -4257,43 +4257,43 @@ std::shared_ptr<input_buffer_adapter> input_adapter(IteratorType first, Iterator
if (JSON_HEDLEY_LIKELY(len > 0)) if (JSON_HEDLEY_LIKELY(len > 0))
{ {
// there is at least one element: use the address of first // there is at least one element: use the address of first
return std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(&(*first)), len); return input_buffer_adapter(reinterpret_cast<const char*>(&(*first)), len);
} }
else else
{ {
// the address of first cannot be used: use nullptr // the address of first cannot be used: use nullptr
return std::make_shared<input_buffer_adapter>(nullptr, len); return input_buffer_adapter(nullptr, len);
} }
} }
inline std::shared_ptr<wide_string_input_adapter<std::wstring>> input_adapter(const std::wstring& ws) inline wide_string_input_adapter<std::wstring> input_adapter(const std::wstring& ws)
{ {
return std::make_shared<wide_string_input_adapter<std::wstring>>(ws); return wide_string_input_adapter<std::wstring>(ws);
} }
inline std::shared_ptr<wide_string_input_adapter<std::u16string>> input_adapter(const std::u16string& ws) inline wide_string_input_adapter<std::u16string> input_adapter(const std::u16string& ws)
{ {
return std::make_shared<wide_string_input_adapter<std::u16string>>(ws); return wide_string_input_adapter<std::u16string>(ws);
} }
inline std::shared_ptr<wide_string_input_adapter<std::u32string>> input_adapter(const std::u32string& ws) inline wide_string_input_adapter<std::u32string> input_adapter(const std::u32string& ws)
{ {
return std::make_shared<wide_string_input_adapter<std::u32string>>(ws); return wide_string_input_adapter<std::u32string>(ws);
} }
template<class ContiguousContainer, typename template<class ContiguousContainer, typename
std::enable_if<not std::is_pointer<ContiguousContainer>::value and std::enable_if<not std::is_pointer<ContiguousContainer>::value and
std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value, std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value,
int>::type = 0> int>::type = 0>
std::shared_ptr<input_buffer_adapter> input_adapter(const ContiguousContainer& c) input_buffer_adapter input_adapter(const ContiguousContainer& c)
{ {
return input_adapter(std::begin(c), std::end(c)); return input_adapter(std::begin(c), std::end(c));
} }
template<class T, std::size_t N> template<class T, std::size_t N>
std::shared_ptr<input_buffer_adapter> input_adapter(T (&array)[N]) input_buffer_adapter input_adapter(T (&array)[N])
{ {
return input_adapter(std::begin(array), std::end(array)); return input_adapter(std::begin(array), std::end(array));
} }
@ -4311,7 +4311,7 @@ class span_input_adapter
sizeof(typename std::remove_pointer<CharT>::type) == 1, sizeof(typename std::remove_pointer<CharT>::type) == 1,
int>::type = 0> int>::type = 0>
span_input_adapter(CharT b, std::size_t l) span_input_adapter(CharT b, std::size_t l)
: ia(std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l)) {} : ia(reinterpret_cast<const char*>(b), l) {}
template<typename CharT, template<typename CharT,
typename std::enable_if< typename std::enable_if<
@ -4342,13 +4342,13 @@ class span_input_adapter
span_input_adapter(const ContiguousContainer& c) span_input_adapter(const ContiguousContainer& c)
: span_input_adapter(std::begin(c), std::end(c)) {} : span_input_adapter(std::begin(c), std::end(c)) {}
std::shared_ptr<input_buffer_adapter> get() input_buffer_adapter&& get()
{ {
return ia; return std::move(ia);
} }
private: private:
std::shared_ptr<input_buffer_adapter> ia = nullptr; input_buffer_adapter ia;
}; };
} // namespace detail } // namespace detail
} // namespace nlohmann } // namespace nlohmann
@ -5220,10 +5220,9 @@ namespace detail
/*! /*!
@brief deserialization of CBOR, MessagePack, and UBJSON values @brief deserialization of CBOR, MessagePack, and UBJSON values
*/ */
template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>, typename InputAdapterType = input_adapter_protocol> template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
class binary_reader class binary_reader
{ {
using input_adapter_ptr_t = std::shared_ptr<InputAdapterType>;
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
@ -5236,10 +5235,9 @@ class binary_reader
@param[in] adapter input adapter to read from @param[in] adapter input adapter to read from
*/ */
explicit binary_reader(input_adapter_ptr_t adapter) : ia(std::move(adapter)) explicit binary_reader(InputAdapterType&& adapter) : ia(std::move(adapter))
{ {
(void)detail::is_sax_static_asserts<SAX, BasicJsonType> {}; (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
assert(ia);
} }
// make class move-only // make class move-only
@ -6999,7 +6997,7 @@ class binary_reader
int get() int get()
{ {
++chars_read; ++chars_read;
return current = ia->get_character(); return current = ia.get_character();
} }
/*! /*!
@ -7155,7 +7153,7 @@ class binary_reader
private: private:
/// input adapter /// input adapter
input_adapter_ptr_t ia = nullptr; InputAdapterType ia;
/// the current character /// the current character
int current = std::char_traits<char>::eof(); int current = std::char_traits<char>::eof();
@ -7279,11 +7277,9 @@ class lexer_base
This class organizes the lexical analysis during JSON deserialization. This class organizes the lexical analysis during JSON deserialization.
*/ */
template<typename BasicJsonType, typename InputAdapterType = input_adapter_protocol> template<typename BasicJsonType, typename InputAdapterType>
class lexer : public lexer_base<BasicJsonType> class lexer : public lexer_base<BasicJsonType>
{ {
using input_adapter_ptr_t = std::shared_ptr<InputAdapterType>;
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
@ -7292,7 +7288,7 @@ class lexer : public lexer_base<BasicJsonType>
public: public:
using token_type = typename lexer_base<BasicJsonType>::token_type; using token_type = typename lexer_base<BasicJsonType>::token_type;
explicit lexer(input_adapter_ptr_t&& adapter) explicit lexer(InputAdapterType&& adapter)
: ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {} : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {}
// delete because of pointer members // delete because of pointer members
@ -8444,7 +8440,7 @@ scan_number_done:
} }
else else
{ {
current = ia->get_character(); current = ia.get_character();
} }
if (JSON_HEDLEY_LIKELY(current != std::char_traits<char>::eof())) if (JSON_HEDLEY_LIKELY(current != std::char_traits<char>::eof()))
@ -8668,7 +8664,7 @@ scan_number_done:
private: private:
/// input adapter /// input adapter
input_adapter_ptr_t ia = nullptr; InputAdapterType ia;
/// the current character /// the current character
std::char_traits<char>::int_type current = std::char_traits<char>::eof(); std::char_traits<char>::int_type current = std::char_traits<char>::eof();
@ -8758,10 +8754,9 @@ using parser_callback_t =
This class implements a recursive descent parser. This class implements a recursive descent parser.
*/ */
template<typename BasicJsonType, typename InputAdapterType = input_adapter_protocol> template<typename BasicJsonType, typename InputAdapterType>
class parser class parser
{ {
using input_adapter_ptr_t = std::shared_ptr<InputAdapterType>;
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
@ -8771,7 +8766,7 @@ class parser
public: public:
/// a parser reading from an input adapter /// a parser reading from an input adapter
explicit parser(input_adapter_ptr_t&& adapter, explicit parser(InputAdapterType&& adapter,
const parser_callback_t<BasicJsonType> cb = nullptr, const parser_callback_t<BasicJsonType> cb = nullptr,
const bool allow_exceptions_ = true) const bool allow_exceptions_ = true)
: callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_) : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)
@ -12678,7 +12673,7 @@ class binary_writer
private: private:
/// whether we can assume little endianess /// whether we can assume little endianess
const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess(); const bool is_little_endian = binary_reader<BasicJsonType, detail::input_buffer_adapter>::little_endianess();
/// the output /// the output
output_adapter_t<CharType> oa = nullptr; output_adapter_t<CharType> oa = nullptr;
@ -14778,7 +14773,7 @@ class basic_json
friend class ::nlohmann::detail::iter_impl; friend class ::nlohmann::detail::iter_impl;
template<typename BasicJsonType, typename CharType> template<typename BasicJsonType, typename CharType>
friend class ::nlohmann::detail::binary_writer; friend class ::nlohmann::detail::binary_writer;
template<typename BasicJsonType, typename SAX, typename InputType> template<typename BasicJsonType, typename InputType, typename SAX>
friend class ::nlohmann::detail::binary_reader; friend class ::nlohmann::detail::binary_reader;
template<typename BasicJsonType> template<typename BasicJsonType>
friend class ::nlohmann::detail::json_sax_dom_parser; friend class ::nlohmann::detail::json_sax_dom_parser;
@ -14793,7 +14788,7 @@ class basic_json
template<typename InputAdapterType> template<typename InputAdapterType>
static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser( static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
std::shared_ptr<InputAdapterType> adapter, InputAdapterType adapter,
detail::parser_callback_t<basic_json>cb = nullptr, detail::parser_callback_t<basic_json>cb = nullptr,
bool allow_exceptions = true bool allow_exceptions = true
) )
@ -14813,7 +14808,8 @@ class basic_json
template<typename CharType> template<typename CharType>
using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>; using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
using binary_reader = ::nlohmann::detail::binary_reader<basic_json>; template<typename InputType>
using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>; template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
using serializer = ::nlohmann::detail::serializer<basic_json>; using serializer = ::nlohmann::detail::serializer<basic_json>;
@ -20906,11 +20902,10 @@ class basic_json
{ {
assert(sax); assert(sax);
auto input_adapter = detail::input_adapter(std::forward<InputType>(i)); auto ia = detail::input_adapter(std::forward<InputType>(i));
using adapter_type = typename decltype(input_adapter)::element_type;
return format == input_format_t::json return format == input_format_t::json
? parser(std::move(input_adapter)).sax_parse(sax, strict) ? parser(std::move(ia)).sax_parse(sax, strict)
: detail::binary_reader<basic_json, SAX, adapter_type>(std::move(input_adapter)).sax_parse(format, sax, strict); : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
} }
template <typename SAX> template <typename SAX>
@ -20921,11 +20916,10 @@ class basic_json
{ {
assert(sax); assert(sax);
auto input_adapter = i.get(); auto ia = i.get();
using adapter_type = typename decltype(input_adapter)::element_type;
return format == input_format_t::json return format == input_format_t::json
? parser(std::move(input_adapter)).sax_parse(sax, strict) ? parser(std::move(ia)).sax_parse(sax, strict)
: detail::binary_reader<basic_json, SAX, adapter_type>(std::move(input_adapter)).sax_parse(format, sax, strict); : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
} }
@ -21629,7 +21623,8 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::input_adapter(std::forward<InputType>(i))).sax_parse(input_format_t::cbor, &sdp, strict); auto ia = detail::input_adapter(std::forward<InputType>(i));
const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21645,7 +21640,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21656,7 +21651,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(i.get()).sax_parse(input_format_t::cbor, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(i.get()).sax_parse(input_format_t::cbor, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21750,7 +21745,8 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::input_adapter(std::forward<InputType>(i))).sax_parse(input_format_t::msgpack, &sdp, strict); auto ia = detail::input_adapter(std::forward<InputType>(i));
const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21766,7 +21762,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21778,7 +21774,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21852,7 +21848,8 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::input_adapter(std::forward<InputType>(i))).sax_parse(input_format_t::ubjson, &sdp, strict); auto ia = detail::input_adapter(std::forward<InputType>(i));
const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21868,7 +21865,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21879,7 +21876,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21952,7 +21949,8 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::input_adapter(std::forward<InputType>(i))).sax_parse(input_format_t::bson, &sdp, strict); auto ia = detail::input_adapter(std::forward<InputType>(i));
const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21968,7 +21966,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(detail::span_input_adapter(std::forward<A1>(a1), std::forward<A2>(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
@ -21979,7 +21977,7 @@ class basic_json
{ {
basic_json result; basic_json result;
detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions); detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
const bool res = binary_reader(i.get()).sax_parse(input_format_t::bson, &sdp, strict); const bool res = binary_reader<detail::input_buffer_adapter>(i.get()).sax_parse(input_format_t::bson, &sdp, strict);
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
/// @} /// @}

View file

@ -39,9 +39,8 @@ namespace
// shortcut to scan a string literal // shortcut to scan a string literal
json::lexer::token_type scan_string(const char* s) json::lexer::token_type scan_string(const char* s)
{ {
auto input_adapter = nlohmann::detail::input_adapter(s); auto ia = nlohmann::detail::input_adapter(s);
using input_adapter_type = typename decltype(input_adapter)::element_type; return nlohmann::detail::lexer<json, decltype(ia)>(std::move(ia)).scan();
return nlohmann::detail::lexer<json, input_adapter_type>(std::move(input_adapter)).scan();
} }
} }