|
|
|
@@ -27,29 +27,11 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson };
|
|
|
|
|
// 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
|
|
|
|
|
buffer. This adapter is a very low level adapter.
|
|
|
|
|
*/
|
|
|
|
|
class file_input_adapter final : public input_adapter_protocol
|
|
|
|
|
class file_input_adapter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
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& operator=(const file_input_adapter&) = delete;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
@@ -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
|
|
|
|
|
subsequent call for input from the std::istream.
|
|
|
|
|
*/
|
|
|
|
|
class input_stream_adapter final : public input_adapter_protocol
|
|
|
|
|
class input_stream_adapter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
~input_stream_adapter() override
|
|
|
|
|
~input_stream_adapter()
|
|
|
|
|
{
|
|
|
|
|
// clear stream flags; we use underlying streambuf I/O, do not
|
|
|
|
|
// 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)
|
|
|
|
|
: is(i), sb(*i.rdbuf())
|
|
|
|
|
: is(&i), sb(i.rdbuf())
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
// delete because of pointer members
|
|
|
|
|
input_stream_adapter(const 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
|
|
|
|
|
// ensure that std::char_traits<char>::eof() and the character 0xFF do not
|
|
|
|
|
// 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.
|
|
|
|
|
if (res == EOF)
|
|
|
|
|
{
|
|
|
|
|
is.clear(is.rdstate() | std::ios::eofbit);
|
|
|
|
|
is->clear(is->rdstate() | std::ios::eofbit);
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
/// the associated input stream
|
|
|
|
|
std::istream& is;
|
|
|
|
|
std::streambuf& sb;
|
|
|
|
|
std::istream* is = nullptr;
|
|
|
|
|
std::streambuf* sb = nullptr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// input adapter for buffer input
|
|
|
|
|
class input_buffer_adapter final : public input_adapter_protocol
|
|
|
|
|
class input_buffer_adapter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
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
|
|
|
|
|
input_buffer_adapter(const input_buffer_adapter&) = delete;
|
|
|
|
|
input_buffer_adapter& operator=(input_buffer_adapter&) = delete;
|
|
|
|
|
input_buffer_adapter(input_buffer_adapter&&) = delete;
|
|
|
|
|
input_buffer_adapter& operator=(input_buffer_adapter&&) = delete;
|
|
|
|
|
~input_buffer_adapter() override = default;
|
|
|
|
|
input_buffer_adapter(input_buffer_adapter&&) = default;
|
|
|
|
|
input_buffer_adapter& operator=(input_buffer_adapter&&) = 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))
|
|
|
|
|
{
|
|
|
|
@@ -282,14 +282,14 @@ struct wide_string_input_helper<WideStringType, 2>
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename WideStringType>
|
|
|
|
|
class wide_string_input_adapter final : public input_adapter_protocol
|
|
|
|
|
class wide_string_input_adapter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit wide_string_input_adapter(const WideStringType& w) noexcept
|
|
|
|
|
: 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
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
@@ -349,9 +349,9 @@ template<typename CharT,
|
|
|
|
|
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
|
|
|
|
|
sizeof(typename std::remove_pointer<CharT>::type) == 1,
|
|
|
|
|
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,
|
|
|
|
@@ -360,7 +360,7 @@ template<typename CharT,
|
|
|
|
|
std::is_integral<typename std::remove_pointer<CharT>::type>::value and
|
|
|
|
|
sizeof(typename std::remove_pointer<CharT>::type) == 1,
|
|
|
|
|
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),
|
|
|
|
|
std::strlen(reinterpret_cast<const char*>(b)));
|
|
|
|
@@ -370,7 +370,7 @@ template<class IteratorType,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
|
std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
|
|
|
|
|
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
|
|
|
|
|
// 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))
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
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,
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
@@ -448,7 +448,7 @@ class span_input_adapter
|
|
|
|
|
sizeof(typename std::remove_pointer<CharT>::type) == 1,
|
|
|
|
|
int>::type = 0>
|
|
|
|
|
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,
|
|
|
|
|
typename std::enable_if<
|
|
|
|
@@ -479,13 +479,13 @@ class span_input_adapter
|
|
|
|
|
span_input_adapter(const ContiguousContainer& 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:
|
|
|
|
|
std::shared_ptr<input_buffer_adapter> ia = nullptr;
|
|
|
|
|
input_buffer_adapter ia;
|
|
|
|
|
};
|
|
|
|
|
} // namespace detail
|
|
|
|
|
} // namespace nlohmann
|
|
|
|
|