From 617b3cf42ea3b1f333f1babcba0d81ffbee7234d Mon Sep 17 00:00:00 2001 From: Francois Chabot Date: Wed, 19 Feb 2020 10:32:49 -0500 Subject: [PATCH 1/9] templated input adapters --- .../nlohmann/detail/input/binary_reader.hpp | 7 +- .../nlohmann/detail/input/input_adapters.hpp | 205 +++++--- include/nlohmann/detail/input/lexer.hpp | 44 +- include/nlohmann/detail/input/parser.hpp | 50 +- include/nlohmann/json.hpp | 158 ++++-- single_include/nlohmann/json.hpp | 464 ++++++++++++------ test/src/unit-class_lexer.cpp | 5 +- 7 files changed, 623 insertions(+), 310 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 25c51fc00..fe61896dc 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -31,9 +31,10 @@ namespace detail /*! @brief deserialization of CBOR, MessagePack, and UBJSON values */ -template> +template, typename InputAdapterType = input_adapter_protocol> class binary_reader { + using input_adapter_ptr_t = std::shared_ptr; using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; @@ -46,7 +47,7 @@ class binary_reader @param[in] adapter input adapter to read from */ - explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter)) + explicit binary_reader(input_adapter_ptr_t adapter) : ia(std::move(adapter)) { (void)detail::is_sax_static_asserts {}; assert(ia); @@ -1965,7 +1966,7 @@ class binary_reader private: /// input adapter - input_adapter_t ia = nullptr; + input_adapter_ptr_t ia = nullptr; /// the current character int current = std::char_traits::eof(); diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 92f0608e9..9709a4507 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -45,14 +45,11 @@ struct input_adapter_protocol virtual ~input_adapter_protocol() = default; }; -/// a type to simplify interfaces -using input_adapter_t = std::shared_ptr; - /*! 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 : public input_adapter_protocol +class file_input_adapter final : public input_adapter_protocol { public: JSON_HEDLEY_NON_NULL(2) @@ -87,7 +84,7 @@ 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 : public input_adapter_protocol +class input_stream_adapter final : public input_adapter_protocol { public: ~input_stream_adapter() override @@ -128,7 +125,7 @@ class input_stream_adapter : public input_adapter_protocol }; /// input adapter for buffer input -class input_buffer_adapter : public input_adapter_protocol +class input_buffer_adapter final : public input_adapter_protocol { public: input_buffer_adapter(const char* b, const std::size_t l) noexcept @@ -285,7 +282,7 @@ struct wide_string_input_helper }; template -class wide_string_input_adapter : public input_adapter_protocol +class wide_string_input_adapter final : public input_adapter_protocol { public: explicit wide_string_input_adapter(const WideStringType& w) noexcept @@ -331,112 +328,164 @@ class wide_string_input_adapter : public input_adapter_protocol std::size_t utf8_bytes_filled = 0; }; -class input_adapter +inline std::shared_ptr input_adapter(std::FILE* file) +{ + return std::make_shared(file); +} + +inline std::shared_ptr input_adapter(std::istream& stream) +{ + return std::make_shared(stream); +} + +inline std::shared_ptr input_adapter(std::istream&& stream) +{ + return std::make_shared(stream); +} + +template::value and + std::is_integral::type>::value and + sizeof(typename std::remove_pointer::type) == 1, + int>::type = 0> +std::shared_ptr input_adapter(CharT b, std::size_t l) +{ + return std::make_shared(reinterpret_cast(b), l); +} + +template::value and + std::is_integral::type>::value and + sizeof(typename std::remove_pointer::type) == 1, + int>::type = 0> +std::shared_ptr input_adapter(CharT b) +{ + return input_adapter(reinterpret_cast(b), + std::strlen(reinterpret_cast(b))); +} + +template::iterator_category, std::random_access_iterator_tag>::value, + int>::type = 0> +std::shared_ptr input_adapter(IteratorType first, IteratorType last) +{ +#ifndef NDEBUG + // assertion to check that the iterator range is indeed contiguous, + // see https://stackoverflow.com/a/35008842/266378 for more discussion + const auto is_contiguous = std::accumulate( + first, last, std::pair(true, 0), + [&first](std::pair res, decltype(*first) val) + { + res.first &= (val == *(std::next(std::addressof(*first), res.second++))); + return res; + }).first; + assert(is_contiguous); +#endif + + // assertion to check that each element is 1 byte long + static_assert( + sizeof(typename iterator_traits::value_type) == 1, + "each element in the iterator range must have the size of 1 byte"); + + const auto len = static_cast(std::distance(first, last)); + if (JSON_HEDLEY_LIKELY(len > 0)) + { + // there is at least one element: use the address of first + return std::make_shared(reinterpret_cast(&(*first)), len); + } + else + { + // the address of first cannot be used: use nullptr + return std::make_shared(nullptr, len); + } +} + +inline std::shared_ptr> input_adapter(const std::wstring& ws) +{ + return std::make_shared>(ws); +} + + +inline std::shared_ptr> input_adapter(const std::u16string& ws) +{ + return std::make_shared>(ws); +} + +inline std::shared_ptr> input_adapter(const std::u32string& ws) +{ + return std::make_shared>(ws); +} + +template::value and + std::is_base_of()))>::iterator_category>::value, + int>::type = 0> +std::shared_ptr input_adapter(const ContiguousContainer& c) +{ + return input_adapter(std::begin(c), std::end(c)); +} + + +template +std::shared_ptr input_adapter(T (&array)[N]) +{ + return input_adapter(std::begin(array), std::end(array)); +} + +// This class only handles inputs of input_buffer_adapter type. +// It's required so that expressions like {ptr, len} can be implicitely casted +// to the correct adapter. +class span_input_adapter { public: - // native support - JSON_HEDLEY_NON_NULL(2) - input_adapter(std::FILE* file) - : ia(std::make_shared(file)) {} - /// input adapter for input stream - input_adapter(std::istream& i) - : ia(std::make_shared(i)) {} - - /// input adapter for input stream - input_adapter(std::istream&& i) - : ia(std::make_shared(i)) {} - - input_adapter(const std::wstring& ws) - : ia(std::make_shared>(ws)) {} - - input_adapter(const std::u16string& ws) - : ia(std::make_shared>(ws)) {} - - input_adapter(const std::u32string& ws) - : ia(std::make_shared>(ws)) {} - - /// input adapter for buffer template::value and std::is_integral::type>::value and sizeof(typename std::remove_pointer::type) == 1, int>::type = 0> - input_adapter(CharT b, std::size_t l) + span_input_adapter(CharT b, std::size_t l) : ia(std::make_shared(reinterpret_cast(b), l)) {} - // derived support - - /// input adapter for string literal template::value and std::is_integral::type>::value and sizeof(typename std::remove_pointer::type) == 1, int>::type = 0> - input_adapter(CharT b) - : input_adapter(reinterpret_cast(b), - std::strlen(reinterpret_cast(b))) {} + span_input_adapter(CharT b) + : span_input_adapter(reinterpret_cast(b), + std::strlen(reinterpret_cast(b))) {} - /// input adapter for iterator range with contiguous storage template::iterator_category, std::random_access_iterator_tag>::value, int>::type = 0> - input_adapter(IteratorType first, IteratorType last) - { -#ifndef NDEBUG - // assertion to check that the iterator range is indeed contiguous, - // see https://stackoverflow.com/a/35008842/266378 for more discussion - const auto is_contiguous = std::accumulate( - first, last, std::pair(true, 0), - [&first](std::pair res, decltype(*first) val) - { - res.first &= (val == *(std::next(std::addressof(*first), res.second++))); - return res; - }).first; - assert(is_contiguous); -#endif + span_input_adapter(IteratorType first, IteratorType last) + : ia(input_adapter(first, last)) {} - // assertion to check that each element is 1 byte long - static_assert( - sizeof(typename iterator_traits::value_type) == 1, - "each element in the iterator range must have the size of 1 byte"); - - const auto len = static_cast(std::distance(first, last)); - if (JSON_HEDLEY_LIKELY(len > 0)) - { - // there is at least one element: use the address of first - ia = std::make_shared(reinterpret_cast(&(*first)), len); - } - else - { - // the address of first cannot be used: use nullptr - ia = std::make_shared(nullptr, len); - } - } - - /// input adapter for array template - input_adapter(T (&array)[N]) - : input_adapter(std::begin(array), std::end(array)) {} + span_input_adapter(T (&array)[N]) + : span_input_adapter(std::begin(array), std::end(array)) {} /// input adapter for contiguous container template::value and std::is_base_of()))>::iterator_category>::value, int>::type = 0> - input_adapter(const ContiguousContainer& c) - : input_adapter(std::begin(c), std::end(c)) {} + span_input_adapter(const ContiguousContainer& c) + : span_input_adapter(std::begin(c), std::end(c)) {} - operator input_adapter_t() + std::shared_ptr get() { return ia; } private: - /// the actual adapter - input_adapter_t ia = nullptr; + std::shared_ptr ia = nullptr; }; } // namespace detail } // namespace nlohmann diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 1b49d69b0..b2774d764 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -22,19 +22,9 @@ namespace detail // lexer // /////////// -/*! -@brief lexical analysis - -This class organizes the lexical analysis during JSON deserialization. -*/ template -class lexer +class lexer_base { - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - public: /// token types for the parser enum class token_type @@ -75,9 +65,9 @@ class lexer return "null literal"; case token_type::value_string: return "string literal"; - case lexer::token_type::value_unsigned: - case lexer::token_type::value_integer: - case lexer::token_type::value_float: + case token_type::value_unsigned: + case token_type::value_integer: + case token_type::value_float: return "number literal"; case token_type::begin_array: return "'['"; @@ -103,15 +93,33 @@ class lexer // LCOV_EXCL_STOP } } +}; +/*! +@brief lexical analysis - explicit lexer(detail::input_adapter_t&& adapter) +This class organizes the lexical analysis during JSON deserialization. +*/ +template +class lexer : public lexer_base +{ + using input_adapter_ptr_t = std::shared_ptr; + + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + using string_t = typename BasicJsonType::string_t; + + public: + using token_type = typename lexer_base::token_type; + + explicit lexer(input_adapter_ptr_t&& adapter) : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {} // delete because of pointer members lexer(const lexer&) = delete; - lexer(lexer&&) = delete; + lexer(lexer&&) = default; lexer& operator=(lexer&) = delete; - lexer& operator=(lexer&&) = delete; + lexer& operator=(lexer&&) = default; ~lexer() = default; private: @@ -1480,7 +1488,7 @@ scan_number_done: private: /// input adapter - detail::input_adapter_t ia = nullptr; + input_adapter_ptr_t ia = nullptr; /// the current character std::char_traits::int_type current = std::char_traits::eof(); diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index 8ff7ca4be..b7628cefa 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -24,44 +24,46 @@ namespace detail // parser // //////////// +enum class parse_event_t : uint8_t +{ + /// the parser read `{` and started to process a JSON object + object_start, + /// the parser read `}` and finished processing a JSON object + object_end, + /// the parser read `[` and started to process a JSON array + array_start, + /// the parser read `]` and finished processing a JSON array + array_end, + /// the parser read a key of a value in an object + key, + /// the parser finished reading a JSON value + value +}; + +template +using parser_callback_t = + std::function; + /*! @brief syntax analysis This class implements a recursive descent parser. */ -template +template class parser { + using input_adapter_ptr_t = std::shared_ptr; using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; - using lexer_t = lexer; + using lexer_t = lexer; using token_type = typename lexer_t::token_type; public: - enum class parse_event_t : uint8_t - { - /// the parser read `{` and started to process a JSON object - object_start, - /// the parser read `}` and finished processing a JSON object - object_end, - /// the parser read `[` and started to process a JSON array - array_start, - /// the parser read `]` and finished processing a JSON array - array_end, - /// the parser read a key of a value in an object - key, - /// the parser finished reading a JSON value - value - }; - - using parser_callback_t = - std::function; - /// a parser reading from an input adapter - explicit parser(detail::input_adapter_t&& adapter, - const parser_callback_t cb = nullptr, + explicit parser(input_adapter_ptr_t&& adapter, + const parser_callback_t cb = nullptr, const bool allow_exceptions_ = true) : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_) { @@ -486,7 +488,7 @@ class parser private: /// callback function - const parser_callback_t callback = nullptr; + const parser_callback_t callback = nullptr; /// the type of the last read token token_type last_token = token_type::uninitialized; /// the lexer diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b72410d71..94598d7b8 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -167,7 +167,9 @@ class basic_json private: template friend struct detail::external_constructor; friend ::nlohmann::json_pointer; - friend ::nlohmann::detail::parser; + + template + friend class ::nlohmann::detail::parser; friend ::nlohmann::detail::serializer; template friend class ::nlohmann::detail::iter_impl; @@ -184,8 +186,17 @@ class basic_json using basic_json_t = NLOHMANN_BASIC_JSON_TPL; // convenience aliases for types residing in namespace detail; - using lexer = ::nlohmann::detail::lexer; - using parser = ::nlohmann::detail::parser; + using lexer = ::nlohmann::detail::lexer_base; + + template + static ::nlohmann::detail::parser parser( + std::shared_ptr adapter, + detail::parser_callback_tcb = nullptr, + bool allow_exceptions = true + ) + { + return ::nlohmann::detail::parser(std::move(adapter), std::move(cb), allow_exceptions); + } using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t; template @@ -1115,7 +1126,7 @@ class basic_json @sa @ref parser_callback_t for more information and examples */ - using parse_event_t = typename parser::parse_event_t; + using parse_event_t = detail::parse_event_t; /*! @brief per-element parser callback type @@ -1166,7 +1177,7 @@ class basic_json @since version 1.0.0 */ - using parser_callback_t = typename parser::parser_callback_t; + using parser_callback_t = detail::parser_callback_t; ////////////////// // constructors // @@ -6198,21 +6209,39 @@ class basic_json @since version 2.0.3 (contiguous containers) */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json parse(detail::input_adapter&& i, + static basic_json parse(InputType&& i, const parser_callback_t cb = nullptr, const bool allow_exceptions = true) { basic_json result; - parser(i, cb, allow_exceptions).parse(true, result); + parser(detail::input_adapter(std::forward(i)), cb, allow_exceptions).parse(true, result); return result; } - static bool accept(detail::input_adapter&& i) + + + JSON_HEDLEY_WARN_UNUSED_RESULT + static basic_json parse(detail::span_input_adapter&& i, + const parser_callback_t cb = nullptr, + const bool allow_exceptions = true) { - return parser(i).accept(true); + basic_json result; + parser(i.get(), cb, allow_exceptions).parse(true, result); + return result; } + template + static bool accept(InputType&& i) + { + return parser(detail::input_adapter(std::forward(i))).accept(true); + } + + static bool accept(detail::span_input_adapter&& i) + { + return parser(i.get()).accept(true); + } /*! @brief generate SAX events @@ -6266,18 +6295,37 @@ class basic_json @since version 3.2.0 */ - template + template JSON_HEDLEY_NON_NULL(2) - static bool sax_parse(detail::input_adapter&& i, SAX* sax, + static bool sax_parse(InputType&& i, SAX* sax, input_format_t format = input_format_t::json, const bool strict = true) { assert(sax); + + auto input_adapter = detail::input_adapter(std::forward(i)); + using adapter_type = typename decltype(input_adapter)::element_type; return format == input_format_t::json - ? parser(std::move(i)).sax_parse(sax, strict) - : detail::binary_reader(std::move(i)).sax_parse(format, sax, strict); + ? parser(std::move(input_adapter)).sax_parse(sax, strict) + : detail::binary_reader(std::move(input_adapter)).sax_parse(format, sax, strict); } + template + JSON_HEDLEY_NON_NULL(2) + static bool sax_parse(detail::span_input_adapter&& i, SAX* sax, + input_format_t format = input_format_t::json, + const bool strict = true) + { + assert(sax); + + auto input_adapter = i.get(); + using adapter_type = typename decltype(input_adapter)::element_type; + return format == input_format_t::json + ? parser(std::move(input_adapter)).sax_parse(sax, strict) + : detail::binary_reader(std::move(input_adapter)).sax_parse(format, sax, strict); + } + + /*! @brief deserialize from an iterator range with contiguous storage @@ -6970,14 +7018,15 @@ class basic_json @a strict parameter since 3.0.0; added @a allow_exceptions parameter since 3.2.0 */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_cbor(detail::input_adapter&& i, + static basic_json from_cbor(InputType&& i, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::cbor, &sdp, strict); + const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -6985,7 +7034,7 @@ class basic_json @copydoc from_cbor(detail::input_adapter&&, const bool, const bool) */ template::value, int> = 0> + detail::enable_if_t::value, int> = 0> JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_cbor(A1 && a1, A2 && a2, const bool strict = true, @@ -6993,7 +7042,18 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(a1), std::forward(a2))).sax_parse(input_format_t::cbor, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict); + return res ? result : basic_json(value_t::discarded); + } + + JSON_HEDLEY_WARN_UNUSED_RESULT + static basic_json from_cbor(detail::span_input_adapter&& i, + const bool strict = true, + const bool allow_exceptions = true) + { + basic_json result; + detail::json_sax_dom_parser sdp(result, allow_exceptions); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7079,14 +7139,15 @@ class basic_json @a strict parameter since 3.0.0; added @a allow_exceptions parameter since 3.2.0 */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_msgpack(detail::input_adapter&& i, + static basic_json from_msgpack(InputType&& i, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::msgpack, &sdp, strict); + const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7094,7 +7155,7 @@ class basic_json @copydoc from_msgpack(detail::input_adapter&&, const bool, const bool) */ template::value, int> = 0> + detail::enable_if_t::value, int> = 0> JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_msgpack(A1 && a1, A2 && a2, const bool strict = true, @@ -7102,10 +7163,23 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(a1), std::forward(a2))).sax_parse(input_format_t::msgpack, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } + + JSON_HEDLEY_WARN_UNUSED_RESULT + static basic_json from_msgpack(detail::span_input_adapter&& i, + const bool strict = true, + const bool allow_exceptions = true) + { + basic_json result; + detail::json_sax_dom_parser sdp(result, allow_exceptions); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict); + return res ? result : basic_json(value_t::discarded); + } + + /*! @brief create a JSON value from an input in UBJSON format @@ -7167,14 +7241,15 @@ class basic_json @since version 3.1.0; added @a allow_exceptions parameter since 3.2.0 */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_ubjson(detail::input_adapter&& i, + static basic_json from_ubjson(InputType&& i, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::ubjson, &sdp, strict); + const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7182,7 +7257,7 @@ class basic_json @copydoc from_ubjson(detail::input_adapter&&, const bool, const bool) */ template::value, int> = 0> + detail::enable_if_t::value, int> = 0> JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_ubjson(A1 && a1, A2 && a2, const bool strict = true, @@ -7190,10 +7265,22 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(a1), std::forward(a2))).sax_parse(input_format_t::ubjson, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } + + static basic_json from_ubjson(detail::span_input_adapter&& i, + const bool strict = true, + const bool allow_exceptions = true) + { + basic_json result; + detail::json_sax_dom_parser sdp(result, allow_exceptions); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict); + return res ? result : basic_json(value_t::discarded); + } + + /*! @brief Create a JSON value from an input in BSON format @@ -7254,14 +7341,15 @@ class basic_json @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the related UBJSON format */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_bson(detail::input_adapter&& i, + static basic_json from_bson(InputType&& i, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::bson, &sdp, strict); + const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7269,7 +7357,7 @@ class basic_json @copydoc from_bson(detail::input_adapter&&, const bool, const bool) */ template::value, int> = 0> + detail::enable_if_t::value, int> = 0> JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_bson(A1 && a1, A2 && a2, const bool strict = true, @@ -7277,12 +7365,20 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(a1), std::forward(a2))).sax_parse(input_format_t::bson, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } - - + JSON_HEDLEY_WARN_UNUSED_RESULT + static basic_json from_bson(detail::span_input_adapter&& i, + const bool strict = true, + const bool allow_exceptions = true) + { + basic_json result; + detail::json_sax_dom_parser sdp(result, allow_exceptions); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::bson, &sdp, strict); + return res ? result : basic_json(value_t::discarded); + } /// @} ////////////////////////// diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 77bd1739c..26c4cd1ba 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3908,14 +3908,11 @@ struct input_adapter_protocol virtual ~input_adapter_protocol() = default; }; -/// a type to simplify interfaces -using input_adapter_t = std::shared_ptr; - /*! 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 : public input_adapter_protocol +class file_input_adapter final : public input_adapter_protocol { public: JSON_HEDLEY_NON_NULL(2) @@ -3950,7 +3947,7 @@ 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 : public input_adapter_protocol +class input_stream_adapter final : public input_adapter_protocol { public: ~input_stream_adapter() override @@ -3991,7 +3988,7 @@ class input_stream_adapter : public input_adapter_protocol }; /// input adapter for buffer input -class input_buffer_adapter : public input_adapter_protocol +class input_buffer_adapter final : public input_adapter_protocol { public: input_buffer_adapter(const char* b, const std::size_t l) noexcept @@ -4148,7 +4145,7 @@ struct wide_string_input_helper }; template -class wide_string_input_adapter : public input_adapter_protocol +class wide_string_input_adapter final : public input_adapter_protocol { public: explicit wide_string_input_adapter(const WideStringType& w) noexcept @@ -4194,112 +4191,164 @@ class wide_string_input_adapter : public input_adapter_protocol std::size_t utf8_bytes_filled = 0; }; -class input_adapter +inline std::shared_ptr input_adapter(std::FILE* file) +{ + return std::make_shared(file); +} + +inline std::shared_ptr input_adapter(std::istream& stream) +{ + return std::make_shared(stream); +} + +inline std::shared_ptr input_adapter(std::istream&& stream) +{ + return std::make_shared(stream); +} + +template::value and + std::is_integral::type>::value and + sizeof(typename std::remove_pointer::type) == 1, + int>::type = 0> +std::shared_ptr input_adapter(CharT b, std::size_t l) +{ + return std::make_shared(reinterpret_cast(b), l); +} + +template::value and + std::is_integral::type>::value and + sizeof(typename std::remove_pointer::type) == 1, + int>::type = 0> +std::shared_ptr input_adapter(CharT b) +{ + return input_adapter(reinterpret_cast(b), + std::strlen(reinterpret_cast(b))); +} + +template::iterator_category, std::random_access_iterator_tag>::value, + int>::type = 0> +std::shared_ptr input_adapter(IteratorType first, IteratorType last) +{ +#ifndef NDEBUG + // assertion to check that the iterator range is indeed contiguous, + // see https://stackoverflow.com/a/35008842/266378 for more discussion + const auto is_contiguous = std::accumulate( + first, last, std::pair(true, 0), + [&first](std::pair res, decltype(*first) val) + { + res.first &= (val == *(std::next(std::addressof(*first), res.second++))); + return res; + }).first; + assert(is_contiguous); +#endif + + // assertion to check that each element is 1 byte long + static_assert( + sizeof(typename iterator_traits::value_type) == 1, + "each element in the iterator range must have the size of 1 byte"); + + const auto len = static_cast(std::distance(first, last)); + if (JSON_HEDLEY_LIKELY(len > 0)) + { + // there is at least one element: use the address of first + return std::make_shared(reinterpret_cast(&(*first)), len); + } + else + { + // the address of first cannot be used: use nullptr + return std::make_shared(nullptr, len); + } +} + +inline std::shared_ptr> input_adapter(const std::wstring& ws) +{ + return std::make_shared>(ws); +} + + +inline std::shared_ptr> input_adapter(const std::u16string& ws) +{ + return std::make_shared>(ws); +} + +inline std::shared_ptr> input_adapter(const std::u32string& ws) +{ + return std::make_shared>(ws); +} + +template::value and + std::is_base_of()))>::iterator_category>::value, + int>::type = 0> +std::shared_ptr input_adapter(const ContiguousContainer& c) +{ + return input_adapter(std::begin(c), std::end(c)); +} + + +template +std::shared_ptr input_adapter(T (&array)[N]) +{ + return input_adapter(std::begin(array), std::end(array)); +} + +// This class only handles inputs of input_buffer_adapter type. +// It's required so that expressions like {ptr, len} can be implicitely casted +// to the correct adapter. +class span_input_adapter { public: - // native support - JSON_HEDLEY_NON_NULL(2) - input_adapter(std::FILE* file) - : ia(std::make_shared(file)) {} - /// input adapter for input stream - input_adapter(std::istream& i) - : ia(std::make_shared(i)) {} - - /// input adapter for input stream - input_adapter(std::istream&& i) - : ia(std::make_shared(i)) {} - - input_adapter(const std::wstring& ws) - : ia(std::make_shared>(ws)) {} - - input_adapter(const std::u16string& ws) - : ia(std::make_shared>(ws)) {} - - input_adapter(const std::u32string& ws) - : ia(std::make_shared>(ws)) {} - - /// input adapter for buffer template::value and std::is_integral::type>::value and sizeof(typename std::remove_pointer::type) == 1, int>::type = 0> - input_adapter(CharT b, std::size_t l) + span_input_adapter(CharT b, std::size_t l) : ia(std::make_shared(reinterpret_cast(b), l)) {} - // derived support - - /// input adapter for string literal template::value and std::is_integral::type>::value and sizeof(typename std::remove_pointer::type) == 1, int>::type = 0> - input_adapter(CharT b) - : input_adapter(reinterpret_cast(b), - std::strlen(reinterpret_cast(b))) {} + span_input_adapter(CharT b) + : span_input_adapter(reinterpret_cast(b), + std::strlen(reinterpret_cast(b))) {} - /// input adapter for iterator range with contiguous storage template::iterator_category, std::random_access_iterator_tag>::value, int>::type = 0> - input_adapter(IteratorType first, IteratorType last) - { -#ifndef NDEBUG - // assertion to check that the iterator range is indeed contiguous, - // see https://stackoverflow.com/a/35008842/266378 for more discussion - const auto is_contiguous = std::accumulate( - first, last, std::pair(true, 0), - [&first](std::pair res, decltype(*first) val) - { - res.first &= (val == *(std::next(std::addressof(*first), res.second++))); - return res; - }).first; - assert(is_contiguous); -#endif + span_input_adapter(IteratorType first, IteratorType last) + : ia(input_adapter(first, last)) {} - // assertion to check that each element is 1 byte long - static_assert( - sizeof(typename iterator_traits::value_type) == 1, - "each element in the iterator range must have the size of 1 byte"); - - const auto len = static_cast(std::distance(first, last)); - if (JSON_HEDLEY_LIKELY(len > 0)) - { - // there is at least one element: use the address of first - ia = std::make_shared(reinterpret_cast(&(*first)), len); - } - else - { - // the address of first cannot be used: use nullptr - ia = std::make_shared(nullptr, len); - } - } - - /// input adapter for array template - input_adapter(T (&array)[N]) - : input_adapter(std::begin(array), std::end(array)) {} + span_input_adapter(T (&array)[N]) + : span_input_adapter(std::begin(array), std::end(array)) {} /// input adapter for contiguous container template::value and std::is_base_of()))>::iterator_category>::value, int>::type = 0> - input_adapter(const ContiguousContainer& c) - : input_adapter(std::begin(c), std::end(c)) {} + span_input_adapter(const ContiguousContainer& c) + : span_input_adapter(std::begin(c), std::end(c)) {} - operator input_adapter_t() + std::shared_ptr get() { return ia; } private: - /// the actual adapter - input_adapter_t ia = nullptr; + std::shared_ptr ia = nullptr; }; } // namespace detail } // namespace nlohmann @@ -5171,9 +5220,10 @@ namespace detail /*! @brief deserialization of CBOR, MessagePack, and UBJSON values */ -template> +template, typename InputAdapterType = input_adapter_protocol> class binary_reader { + using input_adapter_ptr_t = std::shared_ptr; using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; @@ -5186,7 +5236,7 @@ class binary_reader @param[in] adapter input adapter to read from */ - explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter)) + explicit binary_reader(input_adapter_ptr_t adapter) : ia(std::move(adapter)) { (void)detail::is_sax_static_asserts {}; assert(ia); @@ -7105,7 +7155,7 @@ class binary_reader private: /// input adapter - input_adapter_t ia = nullptr; + input_adapter_ptr_t ia = nullptr; /// the current character int current = std::char_traits::eof(); @@ -7152,19 +7202,9 @@ namespace detail // lexer // /////////// -/*! -@brief lexical analysis - -This class organizes the lexical analysis during JSON deserialization. -*/ template -class lexer +class lexer_base { - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - public: /// token types for the parser enum class token_type @@ -7205,9 +7245,9 @@ class lexer return "null literal"; case token_type::value_string: return "string literal"; - case lexer::token_type::value_unsigned: - case lexer::token_type::value_integer: - case lexer::token_type::value_float: + case token_type::value_unsigned: + case token_type::value_integer: + case token_type::value_float: return "number literal"; case token_type::begin_array: return "'['"; @@ -7233,15 +7273,33 @@ class lexer // LCOV_EXCL_STOP } } +}; +/*! +@brief lexical analysis - explicit lexer(detail::input_adapter_t&& adapter) +This class organizes the lexical analysis during JSON deserialization. +*/ +template +class lexer : public lexer_base +{ + using input_adapter_ptr_t = std::shared_ptr; + + using number_integer_t = typename BasicJsonType::number_integer_t; + using number_unsigned_t = typename BasicJsonType::number_unsigned_t; + using number_float_t = typename BasicJsonType::number_float_t; + using string_t = typename BasicJsonType::string_t; + + public: + using token_type = typename lexer_base::token_type; + + explicit lexer(input_adapter_ptr_t&& adapter) : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {} // delete because of pointer members lexer(const lexer&) = delete; - lexer(lexer&&) = delete; + lexer(lexer&&) = default; lexer& operator=(lexer&) = delete; - lexer& operator=(lexer&&) = delete; + lexer& operator=(lexer&&) = default; ~lexer() = default; private: @@ -8610,7 +8668,7 @@ scan_number_done: private: /// input adapter - detail::input_adapter_t ia = nullptr; + input_adapter_ptr_t ia = nullptr; /// the current character std::char_traits::int_type current = std::char_traits::eof(); @@ -8675,44 +8733,46 @@ namespace detail // parser // //////////// +enum class parse_event_t : uint8_t +{ + /// the parser read `{` and started to process a JSON object + object_start, + /// the parser read `}` and finished processing a JSON object + object_end, + /// the parser read `[` and started to process a JSON array + array_start, + /// the parser read `]` and finished processing a JSON array + array_end, + /// the parser read a key of a value in an object + key, + /// the parser finished reading a JSON value + value +}; + +template +using parser_callback_t = + std::function; + /*! @brief syntax analysis This class implements a recursive descent parser. */ -template +template class parser { + using input_adapter_ptr_t = std::shared_ptr; using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; - using lexer_t = lexer; + using lexer_t = lexer; using token_type = typename lexer_t::token_type; public: - enum class parse_event_t : uint8_t - { - /// the parser read `{` and started to process a JSON object - object_start, - /// the parser read `}` and finished processing a JSON object - object_end, - /// the parser read `[` and started to process a JSON array - array_start, - /// the parser read `]` and finished processing a JSON array - array_end, - /// the parser read a key of a value in an object - key, - /// the parser finished reading a JSON value - value - }; - - using parser_callback_t = - std::function; - /// a parser reading from an input adapter - explicit parser(detail::input_adapter_t&& adapter, - const parser_callback_t cb = nullptr, + explicit parser(input_adapter_ptr_t&& adapter, + const parser_callback_t cb = nullptr, const bool allow_exceptions_ = true) : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_) { @@ -9137,7 +9197,7 @@ class parser private: /// callback function - const parser_callback_t callback = nullptr; + const parser_callback_t callback = nullptr; /// the type of the last read token token_type last_token = token_type::uninitialized; /// the lexer @@ -14710,7 +14770,9 @@ class basic_json private: template friend struct detail::external_constructor; friend ::nlohmann::json_pointer; - friend ::nlohmann::detail::parser; + + template + friend class ::nlohmann::detail::parser; friend ::nlohmann::detail::serializer; template friend class ::nlohmann::detail::iter_impl; @@ -14727,8 +14789,17 @@ class basic_json using basic_json_t = NLOHMANN_BASIC_JSON_TPL; // convenience aliases for types residing in namespace detail; - using lexer = ::nlohmann::detail::lexer; - using parser = ::nlohmann::detail::parser; + using lexer = ::nlohmann::detail::lexer_base; + + template + static ::nlohmann::detail::parser parser( + std::shared_ptr adapter, + detail::parser_callback_tcb = nullptr, + bool allow_exceptions = true + ) + { + return ::nlohmann::detail::parser(std::move(adapter), std::move(cb), allow_exceptions); + } using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t; template @@ -15658,7 +15729,7 @@ class basic_json @sa @ref parser_callback_t for more information and examples */ - using parse_event_t = typename parser::parse_event_t; + using parse_event_t = detail::parse_event_t; /*! @brief per-element parser callback type @@ -15709,7 +15780,7 @@ class basic_json @since version 1.0.0 */ - using parser_callback_t = typename parser::parser_callback_t; + using parser_callback_t = detail::parser_callback_t; ////////////////// // constructors // @@ -20741,21 +20812,39 @@ class basic_json @since version 2.0.3 (contiguous containers) */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json parse(detail::input_adapter&& i, + static basic_json parse(InputType&& i, const parser_callback_t cb = nullptr, const bool allow_exceptions = true) { basic_json result; - parser(i, cb, allow_exceptions).parse(true, result); + parser(detail::input_adapter(std::forward(i)), cb, allow_exceptions).parse(true, result); return result; } - static bool accept(detail::input_adapter&& i) + + + JSON_HEDLEY_WARN_UNUSED_RESULT + static basic_json parse(detail::span_input_adapter&& i, + const parser_callback_t cb = nullptr, + const bool allow_exceptions = true) { - return parser(i).accept(true); + basic_json result; + parser(i.get(), cb, allow_exceptions).parse(true, result); + return result; } + template + static bool accept(InputType&& i) + { + return parser(detail::input_adapter(std::forward(i))).accept(true); + } + + static bool accept(detail::span_input_adapter&& i) + { + return parser(i.get()).accept(true); + } /*! @brief generate SAX events @@ -20809,18 +20898,37 @@ class basic_json @since version 3.2.0 */ - template + template JSON_HEDLEY_NON_NULL(2) - static bool sax_parse(detail::input_adapter&& i, SAX* sax, + static bool sax_parse(InputType&& i, SAX* sax, input_format_t format = input_format_t::json, const bool strict = true) { assert(sax); + + auto input_adapter = detail::input_adapter(std::forward(i)); + using adapter_type = typename decltype(input_adapter)::element_type; return format == input_format_t::json - ? parser(std::move(i)).sax_parse(sax, strict) - : detail::binary_reader(std::move(i)).sax_parse(format, sax, strict); + ? parser(std::move(input_adapter)).sax_parse(sax, strict) + : detail::binary_reader(std::move(input_adapter)).sax_parse(format, sax, strict); } + template + JSON_HEDLEY_NON_NULL(2) + static bool sax_parse(detail::span_input_adapter&& i, SAX* sax, + input_format_t format = input_format_t::json, + const bool strict = true) + { + assert(sax); + + auto input_adapter = i.get(); + using adapter_type = typename decltype(input_adapter)::element_type; + return format == input_format_t::json + ? parser(std::move(input_adapter)).sax_parse(sax, strict) + : detail::binary_reader(std::move(input_adapter)).sax_parse(format, sax, strict); + } + + /*! @brief deserialize from an iterator range with contiguous storage @@ -21513,14 +21621,15 @@ class basic_json @a strict parameter since 3.0.0; added @a allow_exceptions parameter since 3.2.0 */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_cbor(detail::input_adapter&& i, + static basic_json from_cbor(InputType&& i, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::cbor, &sdp, strict); + const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21528,7 +21637,7 @@ class basic_json @copydoc from_cbor(detail::input_adapter&&, const bool, const bool) */ template::value, int> = 0> + detail::enable_if_t::value, int> = 0> JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_cbor(A1 && a1, A2 && a2, const bool strict = true, @@ -21536,7 +21645,18 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(a1), std::forward(a2))).sax_parse(input_format_t::cbor, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict); + return res ? result : basic_json(value_t::discarded); + } + + JSON_HEDLEY_WARN_UNUSED_RESULT + static basic_json from_cbor(detail::span_input_adapter&& i, + const bool strict = true, + const bool allow_exceptions = true) + { + basic_json result; + detail::json_sax_dom_parser sdp(result, allow_exceptions); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21622,14 +21742,15 @@ class basic_json @a strict parameter since 3.0.0; added @a allow_exceptions parameter since 3.2.0 */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_msgpack(detail::input_adapter&& i, + static basic_json from_msgpack(InputType&& i, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::msgpack, &sdp, strict); + const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21637,7 +21758,7 @@ class basic_json @copydoc from_msgpack(detail::input_adapter&&, const bool, const bool) */ template::value, int> = 0> + detail::enable_if_t::value, int> = 0> JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_msgpack(A1 && a1, A2 && a2, const bool strict = true, @@ -21645,10 +21766,23 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(a1), std::forward(a2))).sax_parse(input_format_t::msgpack, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } + + JSON_HEDLEY_WARN_UNUSED_RESULT + static basic_json from_msgpack(detail::span_input_adapter&& i, + const bool strict = true, + const bool allow_exceptions = true) + { + basic_json result; + detail::json_sax_dom_parser sdp(result, allow_exceptions); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict); + return res ? result : basic_json(value_t::discarded); + } + + /*! @brief create a JSON value from an input in UBJSON format @@ -21710,14 +21844,15 @@ class basic_json @since version 3.1.0; added @a allow_exceptions parameter since 3.2.0 */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_ubjson(detail::input_adapter&& i, + static basic_json from_ubjson(InputType&& i, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::ubjson, &sdp, strict); + const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21725,7 +21860,7 @@ class basic_json @copydoc from_ubjson(detail::input_adapter&&, const bool, const bool) */ template::value, int> = 0> + detail::enable_if_t::value, int> = 0> JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_ubjson(A1 && a1, A2 && a2, const bool strict = true, @@ -21733,10 +21868,22 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(a1), std::forward(a2))).sax_parse(input_format_t::ubjson, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } + + static basic_json from_ubjson(detail::span_input_adapter&& i, + const bool strict = true, + const bool allow_exceptions = true) + { + basic_json result; + detail::json_sax_dom_parser sdp(result, allow_exceptions); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict); + return res ? result : basic_json(value_t::discarded); + } + + /*! @brief Create a JSON value from an input in BSON format @@ -21797,14 +21944,15 @@ class basic_json @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the related UBJSON format */ + template JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_bson(detail::input_adapter&& i, + static basic_json from_bson(InputType&& i, const bool strict = true, const bool allow_exceptions = true) { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::bson, &sdp, strict); + const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21812,7 +21960,7 @@ class basic_json @copydoc from_bson(detail::input_adapter&&, const bool, const bool) */ template::value, int> = 0> + detail::enable_if_t::value, int> = 0> JSON_HEDLEY_WARN_UNUSED_RESULT static basic_json from_bson(A1 && a1, A2 && a2, const bool strict = true, @@ -21820,12 +21968,20 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(a1), std::forward(a2))).sax_parse(input_format_t::bson, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } - - + JSON_HEDLEY_WARN_UNUSED_RESULT + static basic_json from_bson(detail::span_input_adapter&& i, + const bool strict = true, + const bool allow_exceptions = true) + { + basic_json result; + detail::json_sax_dom_parser sdp(result, allow_exceptions); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::bson, &sdp, strict); + return res ? result : basic_json(value_t::discarded); + } /// @} ////////////////////////// diff --git a/test/src/unit-class_lexer.cpp b/test/src/unit-class_lexer.cpp index 4aad6d784..7ea4eedf0 100644 --- a/test/src/unit-class_lexer.cpp +++ b/test/src/unit-class_lexer.cpp @@ -37,10 +37,11 @@ using nlohmann::json; namespace { // shortcut to scan a string literal -json::lexer::token_type scan_string(const char* s); json::lexer::token_type scan_string(const char* s) { - return json::lexer(nlohmann::detail::input_adapter(s)).scan(); + auto input_adapter = nlohmann::detail::input_adapter(s); + using input_adapter_type = typename decltype(input_adapter)::element_type; + return nlohmann::detail::lexer(std::move(input_adapter)).scan(); } } From a0c4fc945ae21ce8f30fe453a20e605cf639fee0 Mon Sep 17 00:00:00 2001 From: Francois Chabot Date: Wed, 19 Feb 2020 11:23:02 -0500 Subject: [PATCH 2/9] fixed bad friend class declaration --- include/nlohmann/json.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 94598d7b8..5dfd2cb30 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -175,7 +175,7 @@ class basic_json friend class ::nlohmann::detail::iter_impl; template friend class ::nlohmann::detail::binary_writer; - template + template friend class ::nlohmann::detail::binary_reader; template friend class ::nlohmann::detail::json_sax_dom_parser; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 26c4cd1ba..952381f1d 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -14778,7 +14778,7 @@ class basic_json friend class ::nlohmann::detail::iter_impl; template friend class ::nlohmann::detail::binary_writer; - template + template friend class ::nlohmann::detail::binary_reader; template friend class ::nlohmann::detail::json_sax_dom_parser; From 2e2cf02cfdba55433e858342ee182fabc1a43c1d Mon Sep 17 00:00:00 2001 From: Francois Chabot Date: Wed, 19 Feb 2020 14:59:31 -0500 Subject: [PATCH 3/9] duck-typed object input adapters --- .../nlohmann/detail/input/binary_reader.hpp | 10 +- .../nlohmann/detail/input/input_adapters.hpp | 126 +++++------ include/nlohmann/detail/input/lexer.hpp | 10 +- include/nlohmann/detail/input/parser.hpp | 5 +- .../nlohmann/detail/output/binary_writer.hpp | 2 +- include/nlohmann/json.hpp | 49 +++-- single_include/nlohmann/json.hpp | 202 +++++++++--------- test/src/unit-class_lexer.cpp | 5 +- 8 files changed, 202 insertions(+), 207 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index fe61896dc..d0068c4fd 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -31,10 +31,9 @@ namespace detail /*! @brief deserialization of CBOR, MessagePack, and UBJSON values */ -template, typename InputAdapterType = input_adapter_protocol> +template> class binary_reader { - using input_adapter_ptr_t = std::shared_ptr; using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_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 */ - 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 {}; - assert(ia); } // make class move-only @@ -1810,7 +1808,7 @@ class binary_reader int get() { ++chars_read; - return current = ia->get_character(); + return current = ia.get_character(); } /*! @@ -1966,7 +1964,7 @@ class binary_reader private: /// input adapter - input_adapter_ptr_t ia = nullptr; + InputAdapterType ia; /// the current character int current = std::char_traits::eof(); diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 9709a4507..d60af25c7 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -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::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::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::eof(). - virtual std::char_traits::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::int_type get_character() noexcept override + std::char_traits::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::to_int_type, to // ensure that std::char_traits::eof() and the character 0xFF do not // end up as the same value, eg. 0xFFFFFFFF. - std::char_traits::int_type get_character() override + std::char_traits::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::int_type get_character() noexcept override + std::char_traits::int_type get_character() noexcept { if (JSON_HEDLEY_LIKELY(cursor < limit)) { @@ -282,14 +282,14 @@ struct wide_string_input_helper }; template -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::int_type get_character() noexcept override + std::char_traits::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 input_adapter(std::FILE* file) +inline file_input_adapter input_adapter(std::FILE* file) { - return std::make_shared(file); + return file_input_adapter(file); } -inline std::shared_ptr input_adapter(std::istream& stream) +inline input_stream_adapter input_adapter(std::istream& stream) { - return std::make_shared(stream); + return input_stream_adapter(stream); } -inline std::shared_ptr input_adapter(std::istream&& stream) +inline input_stream_adapter input_adapter(std::istream&& stream) { - return std::make_shared(stream); + return input_stream_adapter(stream); } template::type>::value and sizeof(typename std::remove_pointer::type) == 1, int>::type = 0> -std::shared_ptr input_adapter(CharT b, std::size_t l) +input_buffer_adapter input_adapter(CharT b, std::size_t l) { - return std::make_shared(reinterpret_cast(b), l); + return input_buffer_adapter(reinterpret_cast(b), l); } template::type>::value and sizeof(typename std::remove_pointer::type) == 1, int>::type = 0> -std::shared_ptr input_adapter(CharT b) +input_buffer_adapter input_adapter(CharT b) { return input_adapter(reinterpret_cast(b), std::strlen(reinterpret_cast(b))); @@ -370,7 +370,7 @@ template::iterator_category, std::random_access_iterator_tag>::value, int>::type = 0> -std::shared_ptr 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_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(reinterpret_cast(&(*first)), len); + return input_buffer_adapter(reinterpret_cast(&(*first)), len); } else { // the address of first cannot be used: use nullptr - return std::make_shared(nullptr, len); + return input_buffer_adapter(nullptr, len); } } -inline std::shared_ptr> input_adapter(const std::wstring& ws) +inline wide_string_input_adapter input_adapter(const std::wstring& ws) { - return std::make_shared>(ws); + return wide_string_input_adapter(ws); } -inline std::shared_ptr> input_adapter(const std::u16string& ws) +inline wide_string_input_adapter input_adapter(const std::u16string& ws) { - return std::make_shared>(ws); + return wide_string_input_adapter(ws); } -inline std::shared_ptr> input_adapter(const std::u32string& ws) +inline wide_string_input_adapter input_adapter(const std::u32string& ws) { - return std::make_shared>(ws); + return wide_string_input_adapter(ws); } template::value and std::is_base_of()))>::iterator_category>::value, int>::type = 0> -std::shared_ptr input_adapter(const ContiguousContainer& c) +input_buffer_adapter input_adapter(const ContiguousContainer& c) { return input_adapter(std::begin(c), std::end(c)); } template -std::shared_ptr 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::type) == 1, int>::type = 0> span_input_adapter(CharT b, std::size_t l) - : ia(std::make_shared(reinterpret_cast(b), l)) {} + : ia(reinterpret_cast(b), l) {} template get() + input_buffer_adapter&& get() { - return ia; + return std::move(ia); } private: - std::shared_ptr ia = nullptr; + input_buffer_adapter ia; }; } // namespace detail } // namespace nlohmann diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index b2774d764..73a11c1bd 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -99,11 +99,9 @@ class lexer_base This class organizes the lexical analysis during JSON deserialization. */ -template +template class lexer : public lexer_base { - using input_adapter_ptr_t = std::shared_ptr; - using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; @@ -112,7 +110,7 @@ class lexer : public lexer_base public: using token_type = typename lexer_base::token_type; - explicit lexer(input_adapter_ptr_t&& adapter) + explicit lexer(InputAdapterType&& adapter) : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {} // delete because of pointer members @@ -1264,7 +1262,7 @@ scan_number_done: } else { - current = ia->get_character(); + current = ia.get_character(); } if (JSON_HEDLEY_LIKELY(current != std::char_traits::eof())) @@ -1488,7 +1486,7 @@ scan_number_done: private: /// input adapter - input_adapter_ptr_t ia = nullptr; + InputAdapterType ia; /// the current character std::char_traits::int_type current = std::char_traits::eof(); diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index b7628cefa..0546b88cb 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -49,10 +49,9 @@ using parser_callback_t = This class implements a recursive descent parser. */ -template +template class parser { - using input_adapter_ptr_t = std::shared_ptr; using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; @@ -62,7 +61,7 @@ class parser public: /// a parser reading from an input adapter - explicit parser(input_adapter_ptr_t&& adapter, + explicit parser(InputAdapterType&& adapter, const parser_callback_t cb = nullptr, const bool allow_exceptions_ = true) : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 0a80d26b0..d506e7e2f 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1326,7 +1326,7 @@ class binary_writer private: /// whether we can assume little endianess - const bool is_little_endian = binary_reader::little_endianess(); + const bool is_little_endian = binary_reader::little_endianess(); /// the output output_adapter_t oa = nullptr; diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 5dfd2cb30..9db95510c 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -175,7 +175,7 @@ class basic_json friend class ::nlohmann::detail::iter_impl; template friend class ::nlohmann::detail::binary_writer; - template + template friend class ::nlohmann::detail::binary_reader; template friend class ::nlohmann::detail::json_sax_dom_parser; @@ -190,7 +190,7 @@ class basic_json template static ::nlohmann::detail::parser parser( - std::shared_ptr adapter, + InputAdapterType adapter, detail::parser_callback_tcb = nullptr, bool allow_exceptions = true ) @@ -210,7 +210,8 @@ class basic_json template using output_adapter_t = ::nlohmann::detail::output_adapter_t; - using binary_reader = ::nlohmann::detail::binary_reader; + template + using binary_reader = ::nlohmann::detail::binary_reader; template using binary_writer = ::nlohmann::detail::binary_writer; using serializer = ::nlohmann::detail::serializer; @@ -6303,11 +6304,10 @@ class basic_json { assert(sax); - auto input_adapter = detail::input_adapter(std::forward(i)); - using adapter_type = typename decltype(input_adapter)::element_type; + auto ia = detail::input_adapter(std::forward(i)); return format == input_format_t::json - ? parser(std::move(input_adapter)).sax_parse(sax, strict) - : detail::binary_reader(std::move(input_adapter)).sax_parse(format, sax, strict); + ? parser(std::move(ia)).sax_parse(sax, strict) + : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } template @@ -6318,11 +6318,10 @@ class basic_json { assert(sax); - auto input_adapter = i.get(); - using adapter_type = typename decltype(input_adapter)::element_type; + auto ia = i.get(); return format == input_format_t::json - ? parser(std::move(input_adapter)).sax_parse(sax, strict) - : detail::binary_reader(std::move(input_adapter)).sax_parse(format, sax, strict); + ? parser(std::move(ia)).sax_parse(sax, strict) + : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } @@ -7026,7 +7025,8 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::cbor, &sdp, strict); + auto ia = detail::input_adapter(std::forward(i)); + const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7042,7 +7042,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7053,7 +7053,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(i.get()).sax_parse(input_format_t::cbor, &sdp, strict); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7147,7 +7147,8 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::msgpack, &sdp, strict); + auto ia = detail::input_adapter(std::forward(i)); + const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7163,7 +7164,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7175,7 +7176,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7249,7 +7250,8 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::ubjson, &sdp, strict); + auto ia = detail::input_adapter(std::forward(i)); + const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7265,7 +7267,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7276,7 +7278,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7349,7 +7351,8 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::bson, &sdp, strict); + auto ia = detail::input_adapter(std::forward(i)); + const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7365,7 +7368,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -7376,7 +7379,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(i.get()).sax_parse(input_format_t::bson, &sdp, strict); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } /// @} diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 952381f1d..0bd26e872 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3890,29 +3890,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::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::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::eof(). - virtual std::char_traits::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) @@ -3925,9 +3907,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::int_type get_character() noexcept override + std::char_traits::int_type get_character() noexcept { 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 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::to_int_type, to // ensure that std::char_traits::eof() and the character 0xFF do not // end up as the same value, eg. 0xFFFFFFFF. - std::char_traits::int_type get_character() override + std::char_traits::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 @@ -3998,11 +3999,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::int_type get_character() noexcept override + std::char_traits::int_type get_character() noexcept { if (JSON_HEDLEY_LIKELY(cursor < limit)) { @@ -4145,14 +4145,14 @@ struct wide_string_input_helper }; template -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::int_type get_character() noexcept override + std::char_traits::int_type get_character() noexcept { // check if buffer needs to be 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; }; -inline std::shared_ptr input_adapter(std::FILE* file) +inline file_input_adapter input_adapter(std::FILE* file) { - return std::make_shared(file); + return file_input_adapter(file); } -inline std::shared_ptr input_adapter(std::istream& stream) +inline input_stream_adapter input_adapter(std::istream& stream) { - return std::make_shared(stream); + return input_stream_adapter(stream); } -inline std::shared_ptr input_adapter(std::istream&& stream) +inline input_stream_adapter input_adapter(std::istream&& stream) { - return std::make_shared(stream); + return input_stream_adapter(stream); } template::type>::value and sizeof(typename std::remove_pointer::type) == 1, int>::type = 0> -std::shared_ptr input_adapter(CharT b, std::size_t l) +input_buffer_adapter input_adapter(CharT b, std::size_t l) { - return std::make_shared(reinterpret_cast(b), l); + return input_buffer_adapter(reinterpret_cast(b), l); } template::type>::value and sizeof(typename std::remove_pointer::type) == 1, int>::type = 0> -std::shared_ptr input_adapter(CharT b) +input_buffer_adapter input_adapter(CharT b) { return input_adapter(reinterpret_cast(b), std::strlen(reinterpret_cast(b))); @@ -4233,7 +4233,7 @@ template::iterator_category, std::random_access_iterator_tag>::value, int>::type = 0> -std::shared_ptr 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, @@ -4257,43 +4257,43 @@ std::shared_ptr 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(reinterpret_cast(&(*first)), len); + return input_buffer_adapter(reinterpret_cast(&(*first)), len); } else { // the address of first cannot be used: use nullptr - return std::make_shared(nullptr, len); + return input_buffer_adapter(nullptr, len); } } -inline std::shared_ptr> input_adapter(const std::wstring& ws) +inline wide_string_input_adapter input_adapter(const std::wstring& ws) { - return std::make_shared>(ws); + return wide_string_input_adapter(ws); } -inline std::shared_ptr> input_adapter(const std::u16string& ws) +inline wide_string_input_adapter input_adapter(const std::u16string& ws) { - return std::make_shared>(ws); + return wide_string_input_adapter(ws); } -inline std::shared_ptr> input_adapter(const std::u32string& ws) +inline wide_string_input_adapter input_adapter(const std::u32string& ws) { - return std::make_shared>(ws); + return wide_string_input_adapter(ws); } template::value and std::is_base_of()))>::iterator_category>::value, int>::type = 0> -std::shared_ptr input_adapter(const ContiguousContainer& c) +input_buffer_adapter input_adapter(const ContiguousContainer& c) { return input_adapter(std::begin(c), std::end(c)); } template -std::shared_ptr input_adapter(T (&array)[N]) +input_buffer_adapter input_adapter(T (&array)[N]) { return input_adapter(std::begin(array), std::end(array)); } @@ -4311,7 +4311,7 @@ class span_input_adapter sizeof(typename std::remove_pointer::type) == 1, int>::type = 0> span_input_adapter(CharT b, std::size_t l) - : ia(std::make_shared(reinterpret_cast(b), l)) {} + : ia(reinterpret_cast(b), l) {} template get() + input_buffer_adapter&& get() { - return ia; + return std::move(ia); } private: - std::shared_ptr ia = nullptr; + input_buffer_adapter ia; }; } // namespace detail } // namespace nlohmann @@ -5220,10 +5220,9 @@ namespace detail /*! @brief deserialization of CBOR, MessagePack, and UBJSON values */ -template, typename InputAdapterType = input_adapter_protocol> +template> class binary_reader { - using input_adapter_ptr_t = std::shared_ptr; using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_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 */ - 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 {}; - assert(ia); } // make class move-only @@ -6999,7 +6997,7 @@ class binary_reader int get() { ++chars_read; - return current = ia->get_character(); + return current = ia.get_character(); } /*! @@ -7155,7 +7153,7 @@ class binary_reader private: /// input adapter - input_adapter_ptr_t ia = nullptr; + InputAdapterType ia; /// the current character int current = std::char_traits::eof(); @@ -7279,11 +7277,9 @@ class lexer_base This class organizes the lexical analysis during JSON deserialization. */ -template +template class lexer : public lexer_base { - using input_adapter_ptr_t = std::shared_ptr; - using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; @@ -7292,7 +7288,7 @@ class lexer : public lexer_base public: using token_type = typename lexer_base::token_type; - explicit lexer(input_adapter_ptr_t&& adapter) + explicit lexer(InputAdapterType&& adapter) : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {} // delete because of pointer members @@ -8444,7 +8440,7 @@ scan_number_done: } else { - current = ia->get_character(); + current = ia.get_character(); } if (JSON_HEDLEY_LIKELY(current != std::char_traits::eof())) @@ -8668,7 +8664,7 @@ scan_number_done: private: /// input adapter - input_adapter_ptr_t ia = nullptr; + InputAdapterType ia; /// the current character std::char_traits::int_type current = std::char_traits::eof(); @@ -8758,10 +8754,9 @@ using parser_callback_t = This class implements a recursive descent parser. */ -template +template class parser { - using input_adapter_ptr_t = std::shared_ptr; using number_integer_t = typename BasicJsonType::number_integer_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_float_t = typename BasicJsonType::number_float_t; @@ -8771,7 +8766,7 @@ class parser public: /// a parser reading from an input adapter - explicit parser(input_adapter_ptr_t&& adapter, + explicit parser(InputAdapterType&& adapter, const parser_callback_t cb = nullptr, const bool allow_exceptions_ = true) : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_) @@ -12678,7 +12673,7 @@ class binary_writer private: /// whether we can assume little endianess - const bool is_little_endian = binary_reader::little_endianess(); + const bool is_little_endian = binary_reader::little_endianess(); /// the output output_adapter_t oa = nullptr; @@ -14778,7 +14773,7 @@ class basic_json friend class ::nlohmann::detail::iter_impl; template friend class ::nlohmann::detail::binary_writer; - template + template friend class ::nlohmann::detail::binary_reader; template friend class ::nlohmann::detail::json_sax_dom_parser; @@ -14793,7 +14788,7 @@ class basic_json template static ::nlohmann::detail::parser parser( - std::shared_ptr adapter, + InputAdapterType adapter, detail::parser_callback_tcb = nullptr, bool allow_exceptions = true ) @@ -14813,7 +14808,8 @@ class basic_json template using output_adapter_t = ::nlohmann::detail::output_adapter_t; - using binary_reader = ::nlohmann::detail::binary_reader; + template + using binary_reader = ::nlohmann::detail::binary_reader; template using binary_writer = ::nlohmann::detail::binary_writer; using serializer = ::nlohmann::detail::serializer; @@ -20906,11 +20902,10 @@ class basic_json { assert(sax); - auto input_adapter = detail::input_adapter(std::forward(i)); - using adapter_type = typename decltype(input_adapter)::element_type; + auto ia = detail::input_adapter(std::forward(i)); return format == input_format_t::json - ? parser(std::move(input_adapter)).sax_parse(sax, strict) - : detail::binary_reader(std::move(input_adapter)).sax_parse(format, sax, strict); + ? parser(std::move(ia)).sax_parse(sax, strict) + : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } template @@ -20921,11 +20916,10 @@ class basic_json { assert(sax); - auto input_adapter = i.get(); - using adapter_type = typename decltype(input_adapter)::element_type; + auto ia = i.get(); return format == input_format_t::json - ? parser(std::move(input_adapter)).sax_parse(sax, strict) - : detail::binary_reader(std::move(input_adapter)).sax_parse(format, sax, strict); + ? parser(std::move(ia)).sax_parse(sax, strict) + : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } @@ -21629,7 +21623,8 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::cbor, &sdp, strict); + auto ia = detail::input_adapter(std::forward(i)); + const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21645,7 +21640,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21656,7 +21651,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(i.get()).sax_parse(input_format_t::cbor, &sdp, strict); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::cbor, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21750,7 +21745,8 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::msgpack, &sdp, strict); + auto ia = detail::input_adapter(std::forward(i)); + const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21766,7 +21762,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21778,7 +21774,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::msgpack, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21852,7 +21848,8 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::ubjson, &sdp, strict); + auto ia = detail::input_adapter(std::forward(i)); + const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21868,7 +21865,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21879,7 +21876,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::ubjson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21952,7 +21949,8 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::input_adapter(std::forward(i))).sax_parse(input_format_t::bson, &sdp, strict); + auto ia = detail::input_adapter(std::forward(i)); + const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21968,7 +21966,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict); + const bool res = binary_reader(detail::span_input_adapter(std::forward(a1), std::forward(a2)).get()).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } @@ -21979,7 +21977,7 @@ class basic_json { basic_json result; detail::json_sax_dom_parser sdp(result, allow_exceptions); - const bool res = binary_reader(i.get()).sax_parse(input_format_t::bson, &sdp, strict); + const bool res = binary_reader(i.get()).sax_parse(input_format_t::bson, &sdp, strict); return res ? result : basic_json(value_t::discarded); } /// @} diff --git a/test/src/unit-class_lexer.cpp b/test/src/unit-class_lexer.cpp index 7ea4eedf0..a08b1dbbc 100644 --- a/test/src/unit-class_lexer.cpp +++ b/test/src/unit-class_lexer.cpp @@ -39,9 +39,8 @@ namespace // shortcut to scan a string literal json::lexer::token_type scan_string(const char* s) { - auto input_adapter = nlohmann::detail::input_adapter(s); - using input_adapter_type = typename decltype(input_adapter)::element_type; - return nlohmann::detail::lexer(std::move(input_adapter)).scan(); + auto ia = nlohmann::detail::input_adapter(s); + return nlohmann::detail::lexer(std::move(ia)).scan(); } } From de35fad88ee3172f867407661cac8b7e33903ba7 Mon Sep 17 00:00:00 2001 From: Francois Chabot Date: Wed, 19 Feb 2020 15:23:49 -0500 Subject: [PATCH 4/9] fixed missing return arg of operator=() --- include/nlohmann/detail/input/input_adapters.hpp | 1 + single_include/nlohmann/json.hpp | 1 + 2 files changed, 2 insertions(+) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index d60af25c7..142aba285 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -103,6 +103,7 @@ class input_stream_adapter sb = rhs.sb; rhs.is = nullptr; rhs.sb = nullptr; + return *this; } // std::istream/std::streambuf use std::char_traits::to_int_type, to diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 0bd26e872..286e0a293 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3966,6 +3966,7 @@ class input_stream_adapter sb = rhs.sb; rhs.is = nullptr; rhs.sb = nullptr; + return *this; } // std::istream/std::streambuf use std::char_traits::to_int_type, to From 770ae6e9da68a60f5d20409212113d1eff97ea09 Mon Sep 17 00:00:00 2001 From: Francois Chabot Date: Wed, 19 Feb 2020 16:54:09 -0500 Subject: [PATCH 5/9] accomodate older gcc --- include/nlohmann/detail/output/binary_writer.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index d506e7e2f..01cd104d8 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1326,7 +1326,7 @@ class binary_writer private: /// whether we can assume little endianess - const bool is_little_endian = binary_reader::little_endianess(); + const bool is_little_endian = binary_reader::little_endianess(); /// the output output_adapter_t oa = nullptr; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 286e0a293..be12b9651 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12674,7 +12674,7 @@ class binary_writer private: /// whether we can assume little endianess - const bool is_little_endian = binary_reader::little_endianess(); + const bool is_little_endian = binary_reader::little_endianess(); /// the output output_adapter_t oa = nullptr; From c7282d5b1e8d30bfc0dcbca05bf8d89cdc004e63 Mon Sep 17 00:00:00 2001 From: Francois Chabot Date: Thu, 20 Feb 2020 10:19:29 -0500 Subject: [PATCH 6/9] simpler endian detection for older gcc versions --- .../nlohmann/detail/input/binary_reader.hpp | 26 +++++++++-------- .../nlohmann/detail/output/binary_writer.hpp | 2 +- single_include/nlohmann/json.hpp | 28 ++++++++++--------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index d0068c4fd..0fa609650 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -24,6 +24,20 @@ namespace nlohmann { namespace detail { + +/*! +@brief determine system byte order + +@return true if and only if system's byte order is little endian + +@note from https://stackoverflow.com/a/1001328/266378 +*/ +static bool little_endianess(int num = 1) noexcept +{ + return *reinterpret_cast(&num) == 1; +} + + /////////////////// // binary reader // /////////////////// @@ -117,18 +131,6 @@ class binary_reader return result; } - /*! - @brief determine system byte order - - @return true if and only if system's byte order is little endian - - @note from https://stackoverflow.com/a/1001328/266378 - */ - static constexpr bool little_endianess(int num = 1) noexcept - { - return *reinterpret_cast(&num) == 1; - } - private: ////////// // BSON // diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 01cd104d8..76a2b2181 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1326,7 +1326,7 @@ class binary_writer private: /// whether we can assume little endianess - const bool is_little_endian = binary_reader::little_endianess(); + const bool is_little_endian = little_endianess(); /// the output output_adapter_t oa = nullptr; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index be12b9651..a587f954f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5214,6 +5214,20 @@ namespace nlohmann { namespace detail { + +/*! +@brief determine system byte order + +@return true if and only if system's byte order is little endian + +@note from https://stackoverflow.com/a/1001328/266378 +*/ +static bool little_endianess(int num = 1) noexcept +{ + return *reinterpret_cast(&num) == 1; +} + + /////////////////// // binary reader // /////////////////// @@ -5307,18 +5321,6 @@ class binary_reader return result; } - /*! - @brief determine system byte order - - @return true if and only if system's byte order is little endian - - @note from https://stackoverflow.com/a/1001328/266378 - */ - static constexpr bool little_endianess(int num = 1) noexcept - { - return *reinterpret_cast(&num) == 1; - } - private: ////////// // BSON // @@ -12674,7 +12676,7 @@ class binary_writer private: /// whether we can assume little endianess - const bool is_little_endian = binary_reader::little_endianess(); + const bool is_little_endian = little_endianess(); /// the output output_adapter_t oa = nullptr; From 7eadd6daef28615ddf65d724666fc0e11716e6bf Mon Sep 17 00:00:00 2001 From: Francois Chabot Date: Fri, 28 Feb 2020 18:15:11 -0500 Subject: [PATCH 7/9] added back forward declaration to address warnings --- test/src/unit-class_lexer.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/src/unit-class_lexer.cpp b/test/src/unit-class_lexer.cpp index a08b1dbbc..1ac5068bd 100644 --- a/test/src/unit-class_lexer.cpp +++ b/test/src/unit-class_lexer.cpp @@ -37,6 +37,7 @@ using nlohmann::json; namespace { // 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 ia = nlohmann::detail::input_adapter(s); From 41b0704ce1266af31bf15584d672b065b1581612 Mon Sep 17 00:00:00 2001 From: Francois Chabot Date: Mon, 2 Mar 2020 23:50:01 -0500 Subject: [PATCH 8/9] removed move assignments from input adapters --- .../nlohmann/detail/input/input_adapters.hpp | 19 +++---------------- single_include/nlohmann/json.hpp | 19 +++---------------- 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 142aba285..7ad26d00c 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -43,7 +43,7 @@ class file_input_adapter file_input_adapter(const file_input_adapter&) = delete; 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& operator=(file_input_adapter&&) = delete; std::char_traits::int_type get_character() noexcept { @@ -85,6 +85,7 @@ class input_stream_adapter // delete because of pointer members input_stream_adapter(const input_stream_adapter&) = delete; input_stream_adapter& operator=(input_stream_adapter&) = delete; + input_stream_adapter& operator=(input_stream_adapter&& rhs) = delete; input_stream_adapter(input_stream_adapter&& rhs) : is(rhs.is), sb(rhs.sb) { @@ -92,20 +93,6 @@ class input_stream_adapter 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; - return *this; - } - // std::istream/std::streambuf use std::char_traits::to_int_type, to // ensure that std::char_traits::eof() and the character 0xFF do not // end up as the same value, eg. 0xFFFFFFFF. @@ -138,7 +125,7 @@ class input_buffer_adapter input_buffer_adapter(const input_buffer_adapter&) = delete; input_buffer_adapter& operator=(input_buffer_adapter&) = delete; input_buffer_adapter(input_buffer_adapter&&) = default; - input_buffer_adapter& operator=(input_buffer_adapter&&) = default; + input_buffer_adapter& operator=(input_buffer_adapter&&) = delete; std::char_traits::int_type get_character() noexcept { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index a587f954f..831163550 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3906,7 +3906,7 @@ class file_input_adapter file_input_adapter(const file_input_adapter&) = delete; 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& operator=(file_input_adapter&&) = delete; std::char_traits::int_type get_character() noexcept { @@ -3948,6 +3948,7 @@ class input_stream_adapter // delete because of pointer members input_stream_adapter(const input_stream_adapter&) = delete; input_stream_adapter& operator=(input_stream_adapter&) = delete; + input_stream_adapter& operator=(input_stream_adapter&& rhs) = delete; input_stream_adapter(input_stream_adapter&& rhs) : is(rhs.is), sb(rhs.sb) { @@ -3955,20 +3956,6 @@ class input_stream_adapter 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; - return *this; - } - // std::istream/std::streambuf use std::char_traits::to_int_type, to // ensure that std::char_traits::eof() and the character 0xFF do not // end up as the same value, eg. 0xFFFFFFFF. @@ -4001,7 +3988,7 @@ class input_buffer_adapter input_buffer_adapter(const input_buffer_adapter&) = delete; input_buffer_adapter& operator=(input_buffer_adapter&) = delete; input_buffer_adapter(input_buffer_adapter&&) = default; - input_buffer_adapter& operator=(input_buffer_adapter&&) = default; + input_buffer_adapter& operator=(input_buffer_adapter&&) = delete; std::char_traits::int_type get_character() noexcept { From d7b032f565701aff9c05ed8c93a8de80b8323b3c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 13 May 2020 21:28:43 +0200 Subject: [PATCH 9/9] :white_check_mark: add tests to improve coverage --- include/nlohmann/detail/input/binary_reader.hpp | 4 ++-- include/nlohmann/detail/output/binary_writer.hpp | 4 +--- single_include/nlohmann/json.hpp | 8 +++----- test/src/unit-json_pointer.cpp | 3 +++ test/src/unit-regression.cpp | 5 +++++ 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 25de9bdab..a5dc00b18 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -1577,8 +1577,8 @@ class binary_reader return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 16, result); } - default: // LCOV_EXCL_LINE - assert(false); // LCOV_EXCL_LINE + default: // LCOV_EXCL_LINE + return false; // LCOV_EXCL_LINE } } diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index f1f901ee1..d9688096b 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1092,9 +1092,7 @@ class binary_writer } write_number(subtype); - oa->write_characters( - reinterpret_cast(value.data()), - value.size()); + oa->write_characters(reinterpret_cast(value.data()), value.size()); } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index abec8d3f1..f5abd465c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -7104,8 +7104,8 @@ class binary_reader return get_number(input_format_t::msgpack, result.subtype) and get_binary(input_format_t::msgpack, 16, result); } - default: // LCOV_EXCL_LINE - assert(false); // LCOV_EXCL_LINE + default: // LCOV_EXCL_LINE + return false; // LCOV_EXCL_LINE } } @@ -13041,9 +13041,7 @@ class binary_writer } write_number(subtype); - oa->write_characters( - reinterpret_cast(value.data()), - value.size()); + oa->write_characters(reinterpret_cast(value.data()), value.size()); } /*! diff --git a/test/src/unit-json_pointer.cpp b/test/src/unit-json_pointer.cpp index 2cc9dac6a..1e68bc83e 100644 --- a/test/src/unit-json_pointer.cpp +++ b/test/src/unit-json_pointer.cpp @@ -101,6 +101,9 @@ TEST_CASE("JSON pointers") CHECK(j["/foo/1"_json_pointer] == j["foo"][1]); CHECK(j.contains(json::json_pointer("/foo/0"))); CHECK(j.contains(json::json_pointer("/foo/1"))); + CHECK(not j.contains(json::json_pointer("/foo/3"))); + CHECK(not j.contains(json::json_pointer("/foo/+"))); + CHECK(not j.contains(json::json_pointer("/foo/1+2"))); CHECK(not j.contains(json::json_pointer("/foo/-"))); // checked array access diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 1fb372f55..c191628ab 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -464,6 +464,11 @@ TEST_CASE("regression tests") s2 = o["name"]; CHECK(s2 == "value"); + + // improve coverage + o["int"] = 1; + CHECK_THROWS_AS(s2 = o["int"], json::type_error); + CHECK_THROWS_WITH(s2 = o["int"], "[json.exception.type_error.302] type must be string, but is number"); } SECTION("issue #146 - character following a surrogate pair is skipped")