use templates in the sax interface instead of virtuals

This commit is contained in:
Théo DELRIEU 2018-07-02 10:14:37 +02:00
parent f6febbe359
commit 442886d040
No known key found for this signature in database
GPG key ID: 7D6E00D1DF01DEAF
10 changed files with 207 additions and 209 deletions

View file

@ -30,14 +30,14 @@ namespace detail
/*! /*!
@brief deserialization of CBOR, MessagePack, and UBJSON values @brief deserialization of CBOR, MessagePack, and UBJSON values
*/ */
template<typename BasicJsonType> template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>>
class binary_reader class binary_reader
{ {
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t; using string_t = typename BasicJsonType::string_t;
using json_sax_t = json_sax<BasicJsonType>; using json_sax_t = SAX;
public: public:
/*! /*!
@ -321,7 +321,7 @@ class binary_reader
} }
case 0x9F: // array (indefinite length) case 0x9F: // array (indefinite length)
return get_cbor_array(json_sax_t::no_limit); return get_cbor_array(std::size_t(-1));
// map (0x00..0x17 pairs of data items follow) // map (0x00..0x17 pairs of data items follow)
case 0xA0: case 0xA0:
@ -375,7 +375,7 @@ class binary_reader
} }
case 0xBF: // map (indefinite length) case 0xBF: // map (indefinite length)
return get_cbor_object(json_sax_t::no_limit); return get_cbor_object(std::size_t(-1));
case 0xF4: // false case 0xF4: // false
return sax->boolean(false); return sax->boolean(false);
@ -1020,7 +1020,7 @@ class binary_reader
} }
/*! /*!
@param[in] len the length of the array or json_sax_t::no_limit for an @param[in] len the length of the array or std::size_t(-1) for an
array of indefinite size array of indefinite size
@return whether array creation completed @return whether array creation completed
*/ */
@ -1031,7 +1031,7 @@ class binary_reader
return false; return false;
} }
if (len != json_sax_t::no_limit) if (len != std::size_t(-1))
for (std::size_t i = 0; i < len; ++i) for (std::size_t i = 0; i < len; ++i)
{ {
if (JSON_UNLIKELY(not parse_cbor_internal())) if (JSON_UNLIKELY(not parse_cbor_internal()))
@ -1054,7 +1054,7 @@ class binary_reader
} }
/*! /*!
@param[in] len the length of the object or json_sax_t::no_limit for an @param[in] len the length of the object or std::size_t(-1) for an
object of indefinite size object of indefinite size
@return whether object creation completed @return whether object creation completed
*/ */
@ -1066,7 +1066,7 @@ class binary_reader
} }
string_t key; string_t key;
if (len != json_sax_t::no_limit) if (len != std::size_t(-1))
{ {
for (std::size_t i = 0; i < len; ++i) for (std::size_t i = 0; i < len; ++i)
{ {
@ -1558,7 +1558,7 @@ class binary_reader
} }
else else
{ {
if (JSON_UNLIKELY(not sax->start_array())) if (JSON_UNLIKELY(not sax->start_array(-1)))
{ {
return false; return false;
} }
@ -1628,7 +1628,7 @@ class binary_reader
} }
else else
{ {
if (JSON_UNLIKELY(not sax->start_object())) if (JSON_UNLIKELY(not sax->start_object(-1)))
{ {
return false; return false;
} }

View file

@ -136,7 +136,7 @@ constructor contains the parsed value.
@tparam BasicJsonType the JSON type @tparam BasicJsonType the JSON type
*/ */
template<typename BasicJsonType> template<typename BasicJsonType>
class json_sax_dom_parser : public json_sax<BasicJsonType> class json_sax_dom_parser
{ {
public: public:
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
@ -153,43 +153,43 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
: root(r), allow_exceptions(allow_exceptions_) : root(r), allow_exceptions(allow_exceptions_)
{} {}
bool null() override bool null()
{ {
handle_value(nullptr); handle_value(nullptr);
return true; return true;
} }
bool boolean(bool val) override bool boolean(bool val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_integer(number_integer_t val) override bool number_integer(number_integer_t val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_unsigned(number_unsigned_t val) override bool number_unsigned(number_unsigned_t val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_float(number_float_t val, const string_t&) override bool number_float(number_float_t val, const string_t&)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool string(string_t& val) override bool string(string_t& val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool start_object(std::size_t len) override bool start_object(std::size_t len = -1)
{ {
ref_stack.push_back(handle_value(BasicJsonType::value_t::object)); ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
@ -202,20 +202,20 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t& val) override bool key(string_t& val)
{ {
// add null at given key and store the reference for later // add null at given key and store the reference for later
object_element = &(ref_stack.back()->m_value.object->operator[](val)); object_element = &(ref_stack.back()->m_value.object->operator[](val));
return true; return true;
} }
bool end_object() override bool end_object()
{ {
ref_stack.pop_back(); ref_stack.pop_back();
return true; return true;
} }
bool start_array(std::size_t len) override bool start_array(std::size_t len = -1)
{ {
ref_stack.push_back(handle_value(BasicJsonType::value_t::array)); ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
@ -228,14 +228,14 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool end_array() override bool end_array()
{ {
ref_stack.pop_back(); ref_stack.pop_back();
return true; return true;
} }
bool parse_error(std::size_t, const std::string&, bool parse_error(std::size_t, const std::string&,
const detail::exception& ex) override const detail::exception& ex)
{ {
errored = true; errored = true;
if (allow_exceptions) if (allow_exceptions)
@ -310,7 +310,7 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
}; };
template<typename BasicJsonType> template<typename BasicJsonType>
class json_sax_dom_callback_parser : public json_sax<BasicJsonType> class json_sax_dom_callback_parser
{ {
public: public:
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
@ -328,43 +328,43 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
keep_stack.push_back(true); keep_stack.push_back(true);
} }
bool null() override bool null()
{ {
handle_value(nullptr); handle_value(nullptr);
return true; return true;
} }
bool boolean(bool val) override bool boolean(bool val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_integer(number_integer_t val) override bool number_integer(number_integer_t val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_unsigned(number_unsigned_t val) override bool number_unsigned(number_unsigned_t val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_float(number_float_t val, const string_t&) override bool number_float(number_float_t val, const string_t&)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool string(string_t& val) override bool string(string_t& val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool start_object(std::size_t len) override bool start_object(std::size_t len = -1)
{ {
// check callback for object start // check callback for object start
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded); const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
@ -386,7 +386,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t& val) override bool key(string_t& val)
{ {
BasicJsonType k = BasicJsonType(val); BasicJsonType k = BasicJsonType(val);
@ -403,7 +403,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool end_object() override bool end_object()
{ {
if (ref_stack.back()) if (ref_stack.back())
{ {
@ -438,7 +438,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool start_array(std::size_t len) override bool start_array(std::size_t len = -1)
{ {
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded); const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
keep_stack.push_back(keep); keep_stack.push_back(keep);
@ -459,7 +459,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool end_array() override bool end_array()
{ {
bool keep = true; bool keep = true;
@ -491,7 +491,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
} }
bool parse_error(std::size_t, const std::string&, bool parse_error(std::size_t, const std::string&,
const detail::exception& ex) override const detail::exception& ex)
{ {
errored = true; errored = true;
if (allow_exceptions) if (allow_exceptions)
@ -614,7 +614,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
}; };
template<typename BasicJsonType> template<typename BasicJsonType>
class json_sax_acceptor : public json_sax<BasicJsonType> class json_sax_acceptor
{ {
public: public:
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
@ -622,62 +622,62 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t; using string_t = typename BasicJsonType::string_t;
bool null() override bool null()
{ {
return true; return true;
} }
bool boolean(bool) override bool boolean(bool)
{ {
return true; return true;
} }
bool number_integer(number_integer_t) override bool number_integer(number_integer_t)
{ {
return true; return true;
} }
bool number_unsigned(number_unsigned_t) override bool number_unsigned(number_unsigned_t)
{ {
return true; return true;
} }
bool number_float(number_float_t, const string_t&) override bool number_float(number_float_t, const string_t&)
{ {
return true; return true;
} }
bool string(string_t&) override bool string(string_t&)
{ {
return true; return true;
} }
bool start_object(std::size_t) override bool start_object(std::size_t = -1)
{ {
return true; return true;
} }
bool key(string_t&) override bool key(string_t&)
{ {
return true; return true;
} }
bool end_object() override bool end_object()
{ {
return true; return true;
} }
bool start_array(std::size_t) override bool start_array(std::size_t = -1)
{ {
return true; return true;
} }
bool end_array() override bool end_array()
{ {
return true; return true;
} }
bool parse_error(std::size_t, const std::string&, const detail::exception&) override bool parse_error(std::size_t, const std::string&, const detail::exception&)
{ {
return false; return false;
} }

View file

@ -54,8 +54,6 @@ class parser
value value
}; };
using json_sax_t = json_sax<BasicJsonType>;
using parser_callback_t = using parser_callback_t =
std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>; std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>;
@ -144,7 +142,8 @@ class parser
return sax_parse(&sax_acceptor, strict); return sax_parse(&sax_acceptor, strict);
} }
bool sax_parse(json_sax_t* sax, const bool strict = true) template <typename SAX>
bool sax_parse(SAX* sax, const bool strict = true)
{ {
const bool result = sax_parse_internal(sax); const bool result = sax_parse_internal(sax);
@ -160,7 +159,8 @@ class parser
} }
private: private:
bool sax_parse_internal(json_sax_t* sax) template <typename SAX>
bool sax_parse_internal(SAX* sax)
{ {
// stack to remember the hieararchy of structured values we are parsing // stack to remember the hieararchy of structured values we are parsing
// true = array; false = object // true = array; false = object
@ -177,7 +177,7 @@ class parser
{ {
case token_type::begin_object: case token_type::begin_object:
{ {
if (JSON_UNLIKELY(not sax->start_object())) if (JSON_UNLIKELY(not sax->start_object(-1)))
{ {
return false; return false;
} }
@ -225,7 +225,7 @@ class parser
case token_type::begin_array: case token_type::begin_array:
{ {
if (JSON_UNLIKELY(not sax->start_array())) if (JSON_UNLIKELY(not sax->start_array(-1)))
{ {
return false; return false;
} }

View file

@ -171,7 +171,7 @@ class basic_json
friend class ::nlohmann::detail::iter_impl; friend class ::nlohmann::detail::iter_impl;
template<typename BasicJsonType, typename CharType> template<typename BasicJsonType, typename CharType>
friend class ::nlohmann::detail::binary_writer; friend class ::nlohmann::detail::binary_writer;
template<typename BasicJsonType> template<typename BasicJsonType, typename SAX>
friend class ::nlohmann::detail::binary_reader; friend class ::nlohmann::detail::binary_reader;
template<typename BasicJsonType> template<typename BasicJsonType>
friend class ::nlohmann::detail::json_sax_dom_parser; friend class ::nlohmann::detail::json_sax_dom_parser;
@ -1113,8 +1113,6 @@ class basic_json
*/ */
using parser_callback_t = typename parser::parser_callback_t; using parser_callback_t = typename parser::parser_callback_t;
using json_sax_t = typename parser::json_sax_t;
////////////////// //////////////////
// constructors // // constructors //
////////////////// //////////////////
@ -6014,7 +6012,8 @@ class basic_json
return parser(i).accept(true); return parser(i).accept(true);
} }
static bool sax_parse(detail::input_adapter&& i, json_sax_t* sax, template <typename SAX>
static bool sax_parse(detail::input_adapter&& i, SAX* sax,
input_format_t format = input_format_t::json, input_format_t format = input_format_t::json,
const bool strict = true) const bool strict = true)
{ {
@ -6024,7 +6023,7 @@ class basic_json
case input_format_t::json: case input_format_t::json:
return parser(std::move(i)).sax_parse(sax, strict); return parser(std::move(i)).sax_parse(sax, strict);
default: default:
return binary_reader(std::move(i)).sax_parse(format, sax, strict); return detail::binary_reader<basic_json, SAX>(std::move(i)).sax_parse(format, sax, strict);
} }
} }
@ -6097,11 +6096,11 @@ class basic_json
return parser(detail::input_adapter(first, last)).accept(true); return parser(detail::input_adapter(first, last)).accept(true);
} }
template<class IteratorType, typename std::enable_if< template<class IteratorType, class SAX, typename std::enable_if<
std::is_base_of< std::is_base_of<
std::random_access_iterator_tag, std::random_access_iterator_tag,
typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0> typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
static bool sax_parse(IteratorType first, IteratorType last, json_sax_t* sax) static bool sax_parse(IteratorType first, IteratorType last, SAX* sax)
{ {
return parser(detail::input_adapter(first, last)).sax_parse(sax); return parser(detail::input_adapter(first, last)).sax_parse(sax);
} }

View file

@ -3700,7 +3700,7 @@ constructor contains the parsed value.
@tparam BasicJsonType the JSON type @tparam BasicJsonType the JSON type
*/ */
template<typename BasicJsonType> template<typename BasicJsonType>
class json_sax_dom_parser : public json_sax<BasicJsonType> class json_sax_dom_parser
{ {
public: public:
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
@ -3717,43 +3717,43 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
: root(r), allow_exceptions(allow_exceptions_) : root(r), allow_exceptions(allow_exceptions_)
{} {}
bool null() override bool null()
{ {
handle_value(nullptr); handle_value(nullptr);
return true; return true;
} }
bool boolean(bool val) override bool boolean(bool val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_integer(number_integer_t val) override bool number_integer(number_integer_t val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_unsigned(number_unsigned_t val) override bool number_unsigned(number_unsigned_t val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_float(number_float_t val, const string_t&) override bool number_float(number_float_t val, const string_t&)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool string(string_t& val) override bool string(string_t& val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool start_object(std::size_t len) override bool start_object(std::size_t len = -1)
{ {
ref_stack.push_back(handle_value(BasicJsonType::value_t::object)); ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
@ -3766,20 +3766,20 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t& val) override bool key(string_t& val)
{ {
// add null at given key and store the reference for later // add null at given key and store the reference for later
object_element = &(ref_stack.back()->m_value.object->operator[](val)); object_element = &(ref_stack.back()->m_value.object->operator[](val));
return true; return true;
} }
bool end_object() override bool end_object()
{ {
ref_stack.pop_back(); ref_stack.pop_back();
return true; return true;
} }
bool start_array(std::size_t len) override bool start_array(std::size_t len = -1)
{ {
ref_stack.push_back(handle_value(BasicJsonType::value_t::array)); ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
@ -3792,14 +3792,14 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool end_array() override bool end_array()
{ {
ref_stack.pop_back(); ref_stack.pop_back();
return true; return true;
} }
bool parse_error(std::size_t, const std::string&, bool parse_error(std::size_t, const std::string&,
const detail::exception& ex) override const detail::exception& ex)
{ {
errored = true; errored = true;
if (allow_exceptions) if (allow_exceptions)
@ -3874,7 +3874,7 @@ class json_sax_dom_parser : public json_sax<BasicJsonType>
}; };
template<typename BasicJsonType> template<typename BasicJsonType>
class json_sax_dom_callback_parser : public json_sax<BasicJsonType> class json_sax_dom_callback_parser
{ {
public: public:
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
@ -3892,43 +3892,43 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
keep_stack.push_back(true); keep_stack.push_back(true);
} }
bool null() override bool null()
{ {
handle_value(nullptr); handle_value(nullptr);
return true; return true;
} }
bool boolean(bool val) override bool boolean(bool val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_integer(number_integer_t val) override bool number_integer(number_integer_t val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_unsigned(number_unsigned_t val) override bool number_unsigned(number_unsigned_t val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool number_float(number_float_t val, const string_t&) override bool number_float(number_float_t val, const string_t&)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool string(string_t& val) override bool string(string_t& val)
{ {
handle_value(val); handle_value(val);
return true; return true;
} }
bool start_object(std::size_t len) override bool start_object(std::size_t len = -1)
{ {
// check callback for object start // check callback for object start
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded); const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
@ -3950,7 +3950,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool key(string_t& val) override bool key(string_t& val)
{ {
BasicJsonType k = BasicJsonType(val); BasicJsonType k = BasicJsonType(val);
@ -3967,7 +3967,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool end_object() override bool end_object()
{ {
if (ref_stack.back()) if (ref_stack.back())
{ {
@ -4002,7 +4002,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool start_array(std::size_t len) override bool start_array(std::size_t len = -1)
{ {
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded); const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
keep_stack.push_back(keep); keep_stack.push_back(keep);
@ -4023,7 +4023,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
return true; return true;
} }
bool end_array() override bool end_array()
{ {
bool keep = true; bool keep = true;
@ -4055,7 +4055,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
} }
bool parse_error(std::size_t, const std::string&, bool parse_error(std::size_t, const std::string&,
const detail::exception& ex) override const detail::exception& ex)
{ {
errored = true; errored = true;
if (allow_exceptions) if (allow_exceptions)
@ -4178,7 +4178,7 @@ class json_sax_dom_callback_parser : public json_sax<BasicJsonType>
}; };
template<typename BasicJsonType> template<typename BasicJsonType>
class json_sax_acceptor : public json_sax<BasicJsonType> class json_sax_acceptor
{ {
public: public:
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
@ -4186,62 +4186,62 @@ class json_sax_acceptor : public json_sax<BasicJsonType>
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t; using string_t = typename BasicJsonType::string_t;
bool null() override bool null()
{ {
return true; return true;
} }
bool boolean(bool) override bool boolean(bool)
{ {
return true; return true;
} }
bool number_integer(number_integer_t) override bool number_integer(number_integer_t)
{ {
return true; return true;
} }
bool number_unsigned(number_unsigned_t) override bool number_unsigned(number_unsigned_t)
{ {
return true; return true;
} }
bool number_float(number_float_t, const string_t&) override bool number_float(number_float_t, const string_t&)
{ {
return true; return true;
} }
bool string(string_t&) override bool string(string_t&)
{ {
return true; return true;
} }
bool start_object(std::size_t) override bool start_object(std::size_t = -1)
{ {
return true; return true;
} }
bool key(string_t&) override bool key(string_t&)
{ {
return true; return true;
} }
bool end_object() override bool end_object()
{ {
return true; return true;
} }
bool start_array(std::size_t) override bool start_array(std::size_t = -1)
{ {
return true; return true;
} }
bool end_array() override bool end_array()
{ {
return true; return true;
} }
bool parse_error(std::size_t, const std::string&, const detail::exception&) override bool parse_error(std::size_t, const std::string&, const detail::exception&)
{ {
return false; return false;
} }
@ -4295,8 +4295,6 @@ class parser
value value
}; };
using json_sax_t = json_sax<BasicJsonType>;
using parser_callback_t = using parser_callback_t =
std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>; std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>;
@ -4385,7 +4383,8 @@ class parser
return sax_parse(&sax_acceptor, strict); return sax_parse(&sax_acceptor, strict);
} }
bool sax_parse(json_sax_t* sax, const bool strict = true) template <typename SAX>
bool sax_parse(SAX* sax, const bool strict = true)
{ {
const bool result = sax_parse_internal(sax); const bool result = sax_parse_internal(sax);
@ -4401,7 +4400,8 @@ class parser
} }
private: private:
bool sax_parse_internal(json_sax_t* sax) template <typename SAX>
bool sax_parse_internal(SAX* sax)
{ {
// stack to remember the hieararchy of structured values we are parsing // stack to remember the hieararchy of structured values we are parsing
// true = array; false = object // true = array; false = object
@ -4418,7 +4418,7 @@ class parser
{ {
case token_type::begin_object: case token_type::begin_object:
{ {
if (JSON_UNLIKELY(not sax->start_object())) if (JSON_UNLIKELY(not sax->start_object(-1)))
{ {
return false; return false;
} }
@ -4466,7 +4466,7 @@ class parser
case token_type::begin_array: case token_type::begin_array:
{ {
if (JSON_UNLIKELY(not sax->start_array())) if (JSON_UNLIKELY(not sax->start_array(-1)))
{ {
return false; return false;
} }
@ -5774,14 +5774,14 @@ namespace detail
/*! /*!
@brief deserialization of CBOR, MessagePack, and UBJSON values @brief deserialization of CBOR, MessagePack, and UBJSON values
*/ */
template<typename BasicJsonType> template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>>
class binary_reader class binary_reader
{ {
using number_integer_t = typename BasicJsonType::number_integer_t; using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t; using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t; using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t; using string_t = typename BasicJsonType::string_t;
using json_sax_t = json_sax<BasicJsonType>; using json_sax_t = SAX;
public: public:
/*! /*!
@ -6065,7 +6065,7 @@ class binary_reader
} }
case 0x9F: // array (indefinite length) case 0x9F: // array (indefinite length)
return get_cbor_array(json_sax_t::no_limit); return get_cbor_array(std::size_t(-1));
// map (0x00..0x17 pairs of data items follow) // map (0x00..0x17 pairs of data items follow)
case 0xA0: case 0xA0:
@ -6119,7 +6119,7 @@ class binary_reader
} }
case 0xBF: // map (indefinite length) case 0xBF: // map (indefinite length)
return get_cbor_object(json_sax_t::no_limit); return get_cbor_object(std::size_t(-1));
case 0xF4: // false case 0xF4: // false
return sax->boolean(false); return sax->boolean(false);
@ -6764,7 +6764,7 @@ class binary_reader
} }
/*! /*!
@param[in] len the length of the array or json_sax_t::no_limit for an @param[in] len the length of the array or std::size_t(-1) for an
array of indefinite size array of indefinite size
@return whether array creation completed @return whether array creation completed
*/ */
@ -6775,7 +6775,7 @@ class binary_reader
return false; return false;
} }
if (len != json_sax_t::no_limit) if (len != std::size_t(-1))
for (std::size_t i = 0; i < len; ++i) for (std::size_t i = 0; i < len; ++i)
{ {
if (JSON_UNLIKELY(not parse_cbor_internal())) if (JSON_UNLIKELY(not parse_cbor_internal()))
@ -6798,7 +6798,7 @@ class binary_reader
} }
/*! /*!
@param[in] len the length of the object or json_sax_t::no_limit for an @param[in] len the length of the object or std::size_t(-1) for an
object of indefinite size object of indefinite size
@return whether object creation completed @return whether object creation completed
*/ */
@ -6810,7 +6810,7 @@ class binary_reader
} }
string_t key; string_t key;
if (len != json_sax_t::no_limit) if (len != std::size_t(-1))
{ {
for (std::size_t i = 0; i < len; ++i) for (std::size_t i = 0; i < len; ++i)
{ {
@ -7302,7 +7302,7 @@ class binary_reader
} }
else else
{ {
if (JSON_UNLIKELY(not sax->start_array())) if (JSON_UNLIKELY(not sax->start_array(-1)))
{ {
return false; return false;
} }
@ -7372,7 +7372,7 @@ class binary_reader
} }
else else
{ {
if (JSON_UNLIKELY(not sax->start_object())) if (JSON_UNLIKELY(not sax->start_object(-1)))
{ {
return false; return false;
} }
@ -11040,7 +11040,7 @@ class basic_json
friend class ::nlohmann::detail::iter_impl; friend class ::nlohmann::detail::iter_impl;
template<typename BasicJsonType, typename CharType> template<typename BasicJsonType, typename CharType>
friend class ::nlohmann::detail::binary_writer; friend class ::nlohmann::detail::binary_writer;
template<typename BasicJsonType> template<typename BasicJsonType, typename SAX>
friend class ::nlohmann::detail::binary_reader; friend class ::nlohmann::detail::binary_reader;
template<typename BasicJsonType> template<typename BasicJsonType>
friend class ::nlohmann::detail::json_sax_dom_parser; friend class ::nlohmann::detail::json_sax_dom_parser;
@ -11982,8 +11982,6 @@ class basic_json
*/ */
using parser_callback_t = typename parser::parser_callback_t; using parser_callback_t = typename parser::parser_callback_t;
using json_sax_t = typename parser::json_sax_t;
////////////////// //////////////////
// constructors // // constructors //
////////////////// //////////////////
@ -16883,7 +16881,8 @@ class basic_json
return parser(i).accept(true); return parser(i).accept(true);
} }
static bool sax_parse(detail::input_adapter&& i, json_sax_t* sax, template <typename SAX>
static bool sax_parse(detail::input_adapter&& i, SAX* sax,
input_format_t format = input_format_t::json, input_format_t format = input_format_t::json,
const bool strict = true) const bool strict = true)
{ {
@ -16893,7 +16892,7 @@ class basic_json
case input_format_t::json: case input_format_t::json:
return parser(std::move(i)).sax_parse(sax, strict); return parser(std::move(i)).sax_parse(sax, strict);
default: default:
return binary_reader(std::move(i)).sax_parse(format, sax, strict); return detail::binary_reader<basic_json, SAX>(std::move(i)).sax_parse(format, sax, strict);
} }
} }
@ -16966,11 +16965,11 @@ class basic_json
return parser(detail::input_adapter(first, last)).accept(true); return parser(detail::input_adapter(first, last)).accept(true);
} }
template<class IteratorType, typename std::enable_if< template<class IteratorType, class SAX, typename std::enable_if<
std::is_base_of< std::is_base_of<
std::random_access_iterator_tag, std::random_access_iterator_tag,
typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0> typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
static bool sax_parse(IteratorType first, IteratorType last, json_sax_t* sax) static bool sax_parse(IteratorType first, IteratorType last, SAX* sax)
{ {
return parser(detail::input_adapter(first, last)).sax_parse(sax); return parser(detail::input_adapter(first, last)).sax_parse(sax);
} }

View file

@ -34,68 +34,68 @@ using nlohmann::json;
#include <fstream> #include <fstream>
class SaxCountdown : public nlohmann::json::json_sax_t class SaxCountdown
{ {
public: public:
explicit SaxCountdown(const int count) : events_left(count) explicit SaxCountdown(const int count) : events_left(count)
{} {}
bool null() override bool null()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool boolean(bool) override bool boolean(bool)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_integer(json::number_integer_t) override bool number_integer(json::number_integer_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_unsigned(json::number_unsigned_t) override bool number_unsigned(json::number_unsigned_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_float(json::number_float_t, const std::string&) override bool number_float(json::number_float_t, const std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&) override bool string(std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_object(std::size_t) override bool start_object(std::size_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&) override bool key(std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool end_object() override bool end_object()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_array(std::size_t) override bool start_array(std::size_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool end_array() override bool end_array()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t, const std::string&, const json::exception&) override bool parse_error(std::size_t, const std::string&, const json::exception&)
{ {
return false; return false;
} }

View file

@ -35,48 +35,48 @@ using nlohmann::json;
#include <valarray> #include <valarray>
class SaxEventLogger : public nlohmann::json::json_sax_t class SaxEventLogger
{ {
public: public:
bool null() override bool null()
{ {
events.push_back("null()"); events.push_back("null()");
return true; return true;
} }
bool boolean(bool val) override bool boolean(bool val)
{ {
events.push_back(val ? "boolean(true)" : "boolean(false)"); events.push_back(val ? "boolean(true)" : "boolean(false)");
return true; return true;
} }
bool number_integer(json::number_integer_t val) override bool number_integer(json::number_integer_t val)
{ {
events.push_back("number_integer(" + std::to_string(val) + ")"); events.push_back("number_integer(" + std::to_string(val) + ")");
return true; return true;
} }
bool number_unsigned(json::number_unsigned_t val) override bool number_unsigned(json::number_unsigned_t val)
{ {
events.push_back("number_unsigned(" + std::to_string(val) + ")"); events.push_back("number_unsigned(" + std::to_string(val) + ")");
return true; return true;
} }
bool number_float(json::number_float_t, const std::string& s) override bool number_float(json::number_float_t, const std::string& s)
{ {
events.push_back("number_float(" + s + ")"); events.push_back("number_float(" + s + ")");
return true; return true;
} }
bool string(std::string& val) override bool string(std::string& val)
{ {
events.push_back("string(" + val + ")"); events.push_back("string(" + val + ")");
return true; return true;
} }
bool start_object(std::size_t elements) override bool start_object(std::size_t elements = -1)
{ {
if (elements == no_limit) if (elements == std::size_t(-1))
{ {
events.push_back("start_object()"); events.push_back("start_object()");
} }
@ -87,21 +87,21 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
return true; return true;
} }
bool key(std::string& val) override bool key(std::string& val)
{ {
events.push_back("key(" + val + ")"); events.push_back("key(" + val + ")");
return true; return true;
} }
bool end_object() override bool end_object()
{ {
events.push_back("end_object()"); events.push_back("end_object()");
return true; return true;
} }
bool start_array(std::size_t elements) override bool start_array(std::size_t elements = -1)
{ {
if (elements == no_limit) if (elements == std::size_t(-1))
{ {
events.push_back("start_array()"); events.push_back("start_array()");
} }
@ -112,13 +112,13 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
return true; return true;
} }
bool end_array() override bool end_array()
{ {
events.push_back("end_array()"); events.push_back("end_array()");
return true; return true;
} }
bool parse_error(std::size_t position, const std::string&, const json::exception&) override bool parse_error(std::size_t position, const std::string&, const json::exception&)
{ {
errored = true; errored = true;
events.push_back("parse_error(" + std::to_string(position) + ")"); events.push_back("parse_error(" + std::to_string(position) + ")");
@ -129,68 +129,68 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
bool errored = false; bool errored = false;
}; };
class SaxCountdown : public nlohmann::json::json_sax_t class SaxCountdown
{ {
public: public:
explicit SaxCountdown(const int count) : events_left(count) explicit SaxCountdown(const int count) : events_left(count)
{} {}
bool null() override bool null()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool boolean(bool) override bool boolean(bool)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_integer(json::number_integer_t) override bool number_integer(json::number_integer_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_unsigned(json::number_unsigned_t) override bool number_unsigned(json::number_unsigned_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_float(json::number_float_t, const std::string&) override bool number_float(json::number_float_t, const std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&) override bool string(std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_object(std::size_t) override bool start_object(std::size_t = -1)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&) override bool key(std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool end_object() override bool end_object()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_array(std::size_t) override bool start_array(std::size_t = -1)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool end_array() override bool end_array()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t, const std::string&, const json::exception&) override bool parse_error(std::size_t, const std::string&, const json::exception&)
{ {
return false; return false;
} }

View file

@ -35,45 +35,45 @@ using nlohmann::json;
#include <iostream> #include <iostream>
#include <valarray> #include <valarray>
struct SaxEventLogger : public nlohmann::json::json_sax_t struct SaxEventLogger
{ {
bool null() override bool null()
{ {
events.push_back("null()"); events.push_back("null()");
return true; return true;
} }
bool boolean(bool val) override bool boolean(bool val)
{ {
events.push_back(val ? "boolean(true)" : "boolean(false)"); events.push_back(val ? "boolean(true)" : "boolean(false)");
return true; return true;
} }
bool number_integer(json::number_integer_t val) override bool number_integer(json::number_integer_t val)
{ {
events.push_back("number_integer(" + std::to_string(val) + ")"); events.push_back("number_integer(" + std::to_string(val) + ")");
return true; return true;
} }
bool number_unsigned(json::number_unsigned_t val) override bool number_unsigned(json::number_unsigned_t val)
{ {
events.push_back("number_unsigned(" + std::to_string(val) + ")"); events.push_back("number_unsigned(" + std::to_string(val) + ")");
return true; return true;
} }
bool number_float(json::number_float_t, const std::string& s) override bool number_float(json::number_float_t, const std::string& s)
{ {
events.push_back("number_float(" + s + ")"); events.push_back("number_float(" + s + ")");
return true; return true;
} }
bool string(std::string& val) override bool string(std::string& val)
{ {
events.push_back("string(" + val + ")"); events.push_back("string(" + val + ")");
return true; return true;
} }
bool start_object(std::size_t elements) override bool start_object(std::size_t elements)
{ {
if (elements == std::size_t(-1)) if (elements == std::size_t(-1))
{ {
@ -86,19 +86,19 @@ struct SaxEventLogger : public nlohmann::json::json_sax_t
return true; return true;
} }
bool key(std::string& val) override bool key(std::string& val)
{ {
events.push_back("key(" + val + ")"); events.push_back("key(" + val + ")");
return true; return true;
} }
bool end_object()override bool end_object()
{ {
events.push_back("end_object()"); events.push_back("end_object()");
return true; return true;
} }
bool start_array(std::size_t elements) override bool start_array(std::size_t elements)
{ {
if (elements == std::size_t(-1)) if (elements == std::size_t(-1))
{ {
@ -111,13 +111,13 @@ struct SaxEventLogger : public nlohmann::json::json_sax_t
return true; return true;
} }
bool end_array() override bool end_array()
{ {
events.push_back("end_array()"); events.push_back("end_array()");
return true; return true;
} }
bool parse_error(std::size_t position, const std::string&, const json::exception&) override bool parse_error(std::size_t position, const std::string&, const json::exception&)
{ {
events.push_back("parse_error(" + std::to_string(position) + ")"); events.push_back("parse_error(" + std::to_string(position) + ")");
return false; return false;
@ -128,9 +128,9 @@ struct SaxEventLogger : public nlohmann::json::json_sax_t
struct SaxEventLoggerExitAfterStartObject : public SaxEventLogger struct SaxEventLoggerExitAfterStartObject : public SaxEventLogger
{ {
bool start_object(std::size_t elements) override bool start_object(std::size_t elements)
{ {
if (elements == no_limit) if (elements == -1)
{ {
events.push_back("start_object()"); events.push_back("start_object()");
} }
@ -144,7 +144,7 @@ struct SaxEventLoggerExitAfterStartObject : public SaxEventLogger
struct SaxEventLoggerExitAfterKey : public SaxEventLogger struct SaxEventLoggerExitAfterKey : public SaxEventLogger
{ {
bool key(std::string& val) override bool key(std::string& val)
{ {
events.push_back("key(" + val + ")"); events.push_back("key(" + val + ")");
return false; return false;
@ -153,9 +153,9 @@ struct SaxEventLoggerExitAfterKey : public SaxEventLogger
struct SaxEventLoggerExitAfterStartArray : public SaxEventLogger struct SaxEventLoggerExitAfterStartArray : public SaxEventLogger
{ {
bool start_array(std::size_t elements) override bool start_array(std::size_t elements)
{ {
if (elements == no_limit) if (elements == -1)
{ {
events.push_back("start_array()"); events.push_back("start_array()");
} }

View file

@ -34,68 +34,68 @@ using nlohmann::json;
#include <fstream> #include <fstream>
class SaxCountdown : public nlohmann::json::json_sax_t class SaxCountdown
{ {
public: public:
explicit SaxCountdown(const int count) : events_left(count) explicit SaxCountdown(const int count) : events_left(count)
{} {}
bool null() override bool null()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool boolean(bool) override bool boolean(bool)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_integer(json::number_integer_t) override bool number_integer(json::number_integer_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_unsigned(json::number_unsigned_t) override bool number_unsigned(json::number_unsigned_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_float(json::number_float_t, const std::string&) override bool number_float(json::number_float_t, const std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&) override bool string(std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_object(std::size_t) override bool start_object(std::size_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&) override bool key(std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool end_object() override bool end_object()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_array(std::size_t) override bool start_array(std::size_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool end_array() override bool end_array()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t, const std::string&, const json::exception&) override bool parse_error(std::size_t, const std::string&, const json::exception&)
{ {
return false; return false;
} }

View file

@ -34,68 +34,68 @@ using nlohmann::json;
#include <fstream> #include <fstream>
class SaxCountdown : public nlohmann::json::json_sax_t class SaxCountdown
{ {
public: public:
explicit SaxCountdown(const int count) : events_left(count) explicit SaxCountdown(const int count) : events_left(count)
{} {}
bool null() override bool null()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool boolean(bool) override bool boolean(bool)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_integer(json::number_integer_t) override bool number_integer(json::number_integer_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_unsigned(json::number_unsigned_t) override bool number_unsigned(json::number_unsigned_t)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_float(json::number_float_t, const std::string&) override bool number_float(json::number_float_t, const std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&) override bool string(std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_object(std::size_t) override bool start_object(std::size_t = -1)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&) override bool key(std::string&)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool end_object() override bool end_object()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_array(std::size_t) override bool start_array(std::size_t = -1)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool end_array() override bool end_array()
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t, const std::string&, const json::exception&) override bool parse_error(std::size_t, const std::string&, const json::exception&)
{ {
return false; return false;
} }