mirror of
https://github.com/nlohmann/json.git
synced 2026-05-20 13:15:23 +00:00
🔨 added a SAX-DOM-Parser
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1105,7 +1105,7 @@ class basic_json
|
||||
*/
|
||||
using parser_callback_t = typename parser::parser_callback_t;
|
||||
|
||||
using json_sax = typename parser::json_sax;
|
||||
using json_sax_t = typename parser::json_sax_t;
|
||||
|
||||
//////////////////
|
||||
// constructors //
|
||||
@@ -5926,12 +5926,12 @@ class basic_json
|
||||
return parser(i).accept(true);
|
||||
}
|
||||
|
||||
static bool sax_parse(detail::input_adapter i, json_sax* sax)
|
||||
static bool sax_parse(detail::input_adapter i, json_sax_t* sax)
|
||||
{
|
||||
return parser(i, sax).sax_parse();
|
||||
}
|
||||
|
||||
static bool sax_parse(detail::input_adapter& i, json_sax* sax)
|
||||
static bool sax_parse(detail::input_adapter& i, json_sax_t* sax)
|
||||
{
|
||||
return parser(i, sax).sax_parse();
|
||||
}
|
||||
@@ -6009,7 +6009,7 @@ class basic_json
|
||||
std::is_base_of<
|
||||
std::random_access_iterator_tag,
|
||||
typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
|
||||
static bool sax_parse(IteratorType first, IteratorType last, json_sax* sax)
|
||||
static bool sax_parse(IteratorType first, IteratorType last, json_sax_t* sax)
|
||||
{
|
||||
return parser(detail::input_adapter(first, last), sax).sax_parse();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user