🔨 added a SAX-DOM-Parser

This commit is contained in:
Niels Lohmann 2018-02-26 23:39:23 +01:00
parent 21352c4d8e
commit 3ff9455332
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
7 changed files with 177 additions and 44 deletions

View file

@ -20,6 +20,9 @@ struct json_sax
/// type for floating-point numbers
using number_float_t = typename BasicJsonType::number_float_t;
/// constant to indicate that no size limit is given for array or object
static constexpr auto no_limit = std::size_t(-1);
/*!
@brief a null value was read
@return whether parsing should proceed
@ -60,22 +63,22 @@ struct json_sax
@param[in] val string value
@return whether parsing should proceed
*/
virtual bool string(const std::string& val) = 0;
virtual bool string(std::string&& val) = 0;
/*!
@brief the beginning of an object was read
@param[in] elements number of object elements or -1 if unknown
@param[in] elements number of object elements or no_limit if unknown
@return whether parsing should proceed
@note binary formats may report the number of elements
*/
virtual bool start_object(std::size_t elements) = 0;
virtual bool start_object(std::size_t elements = no_limit) = 0;
/*!
@brief an object key was read
@param[in] val object key
@return whether parsing should proceed
*/
virtual bool key(const std::string& val) = 0;
virtual bool key(std::string&& val) = 0;
/*!
@brief the end of an object was read
@ -85,11 +88,11 @@ struct json_sax
/*!
@brief the beginning of an array was read
@param[in] elements number of array elements or -1 if unknown
@param[in] elements number of array elements or no_limit if unknown
@return whether parsing should proceed
@note binary formats may report the number of elements
*/
virtual bool start_array(std::size_t elements) = 0;
virtual bool start_array(std::size_t elements = no_limit) = 0;
/*!
@brief the end of an array was read

View file

@ -1130,7 +1130,7 @@ scan_number_done:
}
/// return current string value (implicitly resets the token; useful only once)
std::string move_string()
std::string&& move_string()
{
return std::move(token_buffer);
}

View file

@ -53,7 +53,7 @@ class parser
value
};
using json_sax = json_sax<BasicJsonType>;
using json_sax_t = json_sax<BasicJsonType>;
using parser_callback_t =
std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>;
@ -65,7 +65,7 @@ class parser
: callback(cb), m_lexer(adapter), allow_exceptions(allow_exceptions_)
{}
parser(detail::input_adapter_t adapter, json_sax* s)
parser(detail::input_adapter_t adapter, json_sax_t* s)
: m_lexer(adapter), sax(s)
{}
@ -541,7 +541,7 @@ class parser
{
case token_type::begin_object:
{
if (not sax->start_object(std::size_t(-1)))
if (not sax->start_object())
{
return false;
}
@ -610,7 +610,7 @@ class parser
case token_type::begin_array:
{
if (not sax->start_array(std::size_t(-1)))
if (not sax->start_array())
{
return false;
}
@ -772,7 +772,7 @@ class parser
/// whether to throw exceptions in case of errors
const bool allow_exceptions = true;
/// associated SAX parse event receiver
json_sax* sax = nullptr;
json_sax_t* sax = nullptr;
};
}
}