2022-07-20 18:38:07 +08:00
|
|
|
// __ _____ _____ _____
|
|
|
|
// __| | __| | | | JSON for Modern C++
|
2022-08-02 05:27:58 +08:00
|
|
|
// | | |__ | | | | | | version 3.11.1
|
2022-07-20 18:38:07 +08:00
|
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
|
|
//
|
|
|
|
// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2018-02-27 03:08:12 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
2019-03-15 21:55:13 +08:00
|
|
|
#include <string> // string
|
|
|
|
#include <utility> // move
|
|
|
|
#include <vector> // vector
|
2018-02-27 03:08:12 +08:00
|
|
|
|
2018-03-12 05:47:25 +08:00
|
|
|
#include <nlohmann/detail/exceptions.hpp>
|
2019-01-13 22:31:22 +08:00
|
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
2022-04-12 20:18:16 +08:00
|
|
|
#include <nlohmann/detail/string_concat.hpp>
|
2018-03-12 05:47:25 +08:00
|
|
|
|
2022-07-31 03:59:13 +08:00
|
|
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
2018-02-27 03:08:12 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief SAX interface
|
2018-08-17 03:53:47 +08:00
|
|
|
|
|
|
|
This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
|
|
|
|
Each function is called in different situations while the input is parsed. The
|
|
|
|
boolean return value informs the parser whether to continue processing the
|
|
|
|
input.
|
2018-02-27 03:08:12 +08:00
|
|
|
*/
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
struct json_sax
|
|
|
|
{
|
|
|
|
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;
|
2018-03-14 14:18:42 +08:00
|
|
|
using string_t = typename BasicJsonType::string_t;
|
2020-05-18 04:50:27 +08:00
|
|
|
using binary_t = typename BasicJsonType::binary_t;
|
2018-02-27 03:08:12 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief a null value was read
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
|
|
|
virtual bool null() = 0;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief a boolean value was read
|
|
|
|
@param[in] val boolean value
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
|
|
|
virtual bool boolean(bool val) = 0;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief an integer number was read
|
|
|
|
@param[in] val integer value
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
|
|
|
virtual bool number_integer(number_integer_t val) = 0;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief an unsigned integer number was read
|
|
|
|
@param[in] val unsigned integer value
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
|
|
|
virtual bool number_unsigned(number_unsigned_t val) = 0;
|
|
|
|
|
|
|
|
/*!
|
2021-12-29 20:41:01 +08:00
|
|
|
@brief a floating-point number was read
|
2018-02-27 03:08:12 +08:00
|
|
|
@param[in] val floating-point value
|
|
|
|
@param[in] s raw token value
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
2018-03-14 14:18:42 +08:00
|
|
|
virtual bool number_float(number_float_t val, const string_t& s) = 0;
|
2018-02-27 03:08:12 +08:00
|
|
|
|
|
|
|
/*!
|
2021-12-29 20:41:01 +08:00
|
|
|
@brief a string value was read
|
2018-02-27 03:08:12 +08:00
|
|
|
@param[in] val string value
|
|
|
|
@return whether parsing should proceed
|
2021-12-29 20:41:01 +08:00
|
|
|
@note It is safe to move the passed string value.
|
2018-02-27 03:08:12 +08:00
|
|
|
*/
|
2018-03-22 03:12:06 +08:00
|
|
|
virtual bool string(string_t& val) = 0;
|
2018-02-27 03:08:12 +08:00
|
|
|
|
2019-07-05 12:13:25 +08:00
|
|
|
/*!
|
2021-12-29 20:41:01 +08:00
|
|
|
@brief a binary value was read
|
2019-07-05 12:13:25 +08:00
|
|
|
@param[in] val binary value
|
|
|
|
@return whether parsing should proceed
|
2021-12-29 20:41:01 +08:00
|
|
|
@note It is safe to move the passed binary value.
|
2019-07-05 12:13:25 +08:00
|
|
|
*/
|
2020-05-18 04:50:27 +08:00
|
|
|
virtual bool binary(binary_t& val) = 0;
|
2019-07-05 12:13:25 +08:00
|
|
|
|
2018-02-27 03:08:12 +08:00
|
|
|
/*!
|
|
|
|
@brief the beginning of an object was read
|
2018-07-24 19:09:08 +08:00
|
|
|
@param[in] elements number of object elements or -1 if unknown
|
2018-02-27 03:08:12 +08:00
|
|
|
@return whether parsing should proceed
|
|
|
|
@note binary formats may report the number of elements
|
|
|
|
*/
|
2018-07-24 19:09:08 +08:00
|
|
|
virtual bool start_object(std::size_t elements) = 0;
|
2018-02-27 03:08:12 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief an object key was read
|
|
|
|
@param[in] val object key
|
|
|
|
@return whether parsing should proceed
|
2018-08-17 03:53:47 +08:00
|
|
|
@note It is safe to move the passed string.
|
2018-02-27 03:08:12 +08:00
|
|
|
*/
|
2018-03-22 03:12:06 +08:00
|
|
|
virtual bool key(string_t& val) = 0;
|
2018-02-27 03:08:12 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief the end of an object was read
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
|
|
|
virtual bool end_object() = 0;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief the beginning of an array was read
|
2018-07-24 19:09:08 +08:00
|
|
|
@param[in] elements number of array elements or -1 if unknown
|
2018-02-27 03:08:12 +08:00
|
|
|
@return whether parsing should proceed
|
|
|
|
@note binary formats may report the number of elements
|
|
|
|
*/
|
2018-07-24 19:09:08 +08:00
|
|
|
virtual bool start_array(std::size_t elements) = 0;
|
2018-02-27 03:08:12 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief the end of an array was read
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
|
|
|
virtual bool end_array() = 0;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief a parse error occurred
|
|
|
|
@param[in] position the position in the input where the error occurs
|
|
|
|
@param[in] last_token the last read token
|
2018-10-08 03:30:58 +08:00
|
|
|
@param[in] ex an exception object describing the error
|
2018-03-21 05:39:08 +08:00
|
|
|
@return whether parsing should proceed (must return false)
|
2018-02-27 03:08:12 +08:00
|
|
|
*/
|
2018-03-05 23:46:35 +08:00
|
|
|
virtual bool parse_error(std::size_t position,
|
|
|
|
const std::string& last_token,
|
2018-03-12 05:47:25 +08:00
|
|
|
const detail::exception& ex) = 0;
|
2018-02-27 03:08:12 +08:00
|
|
|
|
2021-03-24 14:15:18 +08:00
|
|
|
json_sax() = default;
|
|
|
|
json_sax(const json_sax&) = default;
|
|
|
|
json_sax(json_sax&&) noexcept = default;
|
|
|
|
json_sax& operator=(const json_sax&) = default;
|
|
|
|
json_sax& operator=(json_sax&&) noexcept = default;
|
2018-02-27 03:08:12 +08:00
|
|
|
virtual ~json_sax() = default;
|
|
|
|
};
|
2018-03-06 04:06:00 +08:00
|
|
|
|
|
|
|
|
2018-03-12 01:47:38 +08:00
|
|
|
namespace detail
|
|
|
|
{
|
2018-05-28 00:07:53 +08:00
|
|
|
/*!
|
|
|
|
@brief SAX implementation to create a JSON value from SAX events
|
|
|
|
|
|
|
|
This class implements the @ref json_sax interface and processes the SAX events
|
|
|
|
to create a JSON value which makes it basically a DOM parser. The structure or
|
|
|
|
hierarchy of the JSON value is managed by the stack `ref_stack` which contains
|
|
|
|
a pointer to the respective array or object for each recursion depth.
|
|
|
|
|
|
|
|
After successful parsing, the value that is passed by reference to the
|
|
|
|
constructor contains the parsed value.
|
|
|
|
|
|
|
|
@tparam BasicJsonType the JSON type
|
|
|
|
*/
|
2018-03-06 04:06:00 +08:00
|
|
|
template<typename BasicJsonType>
|
2018-07-02 16:14:37 +08:00
|
|
|
class json_sax_dom_parser
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
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;
|
2018-03-14 14:18:42 +08:00
|
|
|
using string_t = typename BasicJsonType::string_t;
|
2020-05-18 04:50:27 +08:00
|
|
|
using binary_t = typename BasicJsonType::binary_t;
|
2018-03-06 04:06:00 +08:00
|
|
|
|
2018-05-28 00:07:53 +08:00
|
|
|
/*!
|
2021-03-24 14:15:18 +08:00
|
|
|
@param[in,out] r reference to a JSON value that is manipulated while
|
2018-05-28 00:07:53 +08:00
|
|
|
parsing
|
|
|
|
@param[in] allow_exceptions_ whether parse errors yield exceptions
|
|
|
|
*/
|
2018-08-17 03:53:47 +08:00
|
|
|
explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
|
2018-03-07 01:17:07 +08:00
|
|
|
: root(r), allow_exceptions(allow_exceptions_)
|
|
|
|
{}
|
|
|
|
|
2019-03-17 19:01:49 +08:00
|
|
|
// make class move-only
|
|
|
|
json_sax_dom_parser(const json_sax_dom_parser&) = delete;
|
2021-03-24 14:15:18 +08:00
|
|
|
json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
|
2019-03-17 19:01:49 +08:00
|
|
|
json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
|
2021-03-24 14:15:18 +08:00
|
|
|
json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
|
2019-03-17 19:01:49 +08:00
|
|
|
~json_sax_dom_parser() = default;
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool null()
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
|
|
|
handle_value(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool boolean(bool val)
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool number_integer(number_integer_t val)
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool number_unsigned(number_unsigned_t val)
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 00:39:18 +08:00
|
|
|
bool number_float(number_float_t val, const string_t& /*unused*/)
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool string(string_t& val)
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:50:27 +08:00
|
|
|
bool binary(binary_t& val)
|
2019-07-05 12:13:25 +08:00
|
|
|
{
|
2020-05-17 19:51:59 +08:00
|
|
|
handle_value(std::move(val));
|
2019-07-05 12:13:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:09:08 +08:00
|
|
|
bool start_object(std::size_t len)
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
|
|
|
ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
|
2018-03-12 01:47:38 +08:00
|
|
|
|
2021-12-29 16:47:05 +08:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
|
2018-03-12 01:47:38 +08:00
|
|
|
{
|
2022-04-12 20:18:16 +08:00
|
|
|
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
|
2018-03-12 01:47:38 +08:00
|
|
|
}
|
|
|
|
|
2018-03-06 04:06:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool key(string_t& val)
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
2022-05-20 01:56:33 +08:00
|
|
|
JSON_ASSERT(!ref_stack.empty());
|
|
|
|
JSON_ASSERT(ref_stack.back()->is_object());
|
|
|
|
|
2018-03-06 14:19:05 +08:00
|
|
|
// add null at given key and store the reference for later
|
|
|
|
object_element = &(ref_stack.back()->m_value.object->operator[](val));
|
2018-03-06 04:06:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool end_object()
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
2022-05-20 01:56:33 +08:00
|
|
|
JSON_ASSERT(!ref_stack.empty());
|
|
|
|
JSON_ASSERT(ref_stack.back()->is_object());
|
|
|
|
|
2021-01-15 04:55:49 +08:00
|
|
|
ref_stack.back()->set_parents();
|
2018-03-06 04:06:00 +08:00
|
|
|
ref_stack.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:09:08 +08:00
|
|
|
bool start_array(std::size_t len)
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
|
|
|
ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
|
2018-03-12 01:47:38 +08:00
|
|
|
|
2021-12-29 16:47:05 +08:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
|
2018-03-12 01:47:38 +08:00
|
|
|
{
|
2022-04-12 20:18:16 +08:00
|
|
|
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
|
2018-03-12 01:47:38 +08:00
|
|
|
}
|
|
|
|
|
2018-03-06 04:06:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool end_array()
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
2022-05-20 01:56:33 +08:00
|
|
|
JSON_ASSERT(!ref_stack.empty());
|
|
|
|
JSON_ASSERT(ref_stack.back()->is_array());
|
|
|
|
|
2021-01-15 04:55:49 +08:00
|
|
|
ref_stack.back()->set_parents();
|
2018-03-06 04:06:00 +08:00
|
|
|
ref_stack.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-16 20:45:39 +08:00
|
|
|
template<class Exception>
|
2018-10-08 00:39:18 +08:00
|
|
|
bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
|
2020-07-16 20:45:39 +08:00
|
|
|
const Exception& ex)
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
2018-03-07 01:17:07 +08:00
|
|
|
errored = true;
|
2020-07-25 04:21:30 +08:00
|
|
|
static_cast<void>(ex);
|
2018-03-07 01:17:07 +08:00
|
|
|
if (allow_exceptions)
|
|
|
|
{
|
2020-07-16 20:45:39 +08:00
|
|
|
JSON_THROW(ex);
|
2018-03-07 01:17:07 +08:00
|
|
|
}
|
2018-03-06 04:06:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-29 05:39:39 +08:00
|
|
|
constexpr bool is_errored() const
|
2018-03-06 04:06:00 +08:00
|
|
|
{
|
2018-03-07 01:17:07 +08:00
|
|
|
return errored;
|
2018-03-06 04:06:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/*!
|
|
|
|
@invariant If the ref stack is empty, then the passed value will be the new
|
|
|
|
root.
|
|
|
|
@invariant If the ref stack contains a value, then it is an array or an
|
|
|
|
object to which we can add elements
|
|
|
|
*/
|
|
|
|
template<typename Value>
|
2019-07-02 04:37:30 +08:00
|
|
|
JSON_HEDLEY_RETURNS_NON_NULL
|
2018-03-06 04:06:00 +08:00
|
|
|
BasicJsonType* handle_value(Value&& v)
|
|
|
|
{
|
|
|
|
if (ref_stack.empty())
|
|
|
|
{
|
|
|
|
root = BasicJsonType(std::forward<Value>(v));
|
|
|
|
return &root;
|
|
|
|
}
|
2018-10-08 01:07:58 +08:00
|
|
|
|
2020-07-11 20:04:40 +08:00
|
|
|
JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
|
2018-10-08 01:07:58 +08:00
|
|
|
|
|
|
|
if (ref_stack.back()->is_array())
|
|
|
|
{
|
|
|
|
ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
|
|
|
|
return &(ref_stack.back()->m_value.array->back());
|
|
|
|
}
|
2019-03-18 20:53:48 +08:00
|
|
|
|
2020-07-06 18:22:31 +08:00
|
|
|
JSON_ASSERT(ref_stack.back()->is_object());
|
|
|
|
JSON_ASSERT(object_element);
|
2019-03-18 20:53:48 +08:00
|
|
|
*object_element = BasicJsonType(std::forward<Value>(v));
|
|
|
|
return object_element;
|
2018-03-06 04:06:00 +08:00
|
|
|
}
|
2018-03-07 01:17:07 +08:00
|
|
|
|
|
|
|
/// the parsed JSON value
|
|
|
|
BasicJsonType& root;
|
|
|
|
/// stack to model hierarchy of values
|
2019-03-17 19:01:49 +08:00
|
|
|
std::vector<BasicJsonType*> ref_stack {};
|
2018-03-07 01:17:07 +08:00
|
|
|
/// helper to hold the reference for the next object element
|
|
|
|
BasicJsonType* object_element = nullptr;
|
|
|
|
/// whether a syntax error occurred
|
|
|
|
bool errored = false;
|
|
|
|
/// whether to throw exceptions in case of errors
|
|
|
|
const bool allow_exceptions = true;
|
2018-03-06 04:06:00 +08:00
|
|
|
};
|
|
|
|
|
2018-03-18 22:13:53 +08:00
|
|
|
template<typename BasicJsonType>
|
2018-07-02 16:14:37 +08:00
|
|
|
class json_sax_dom_callback_parser
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
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;
|
2020-05-18 04:50:27 +08:00
|
|
|
using binary_t = typename BasicJsonType::binary_t;
|
2018-03-18 22:13:53 +08:00
|
|
|
using parser_callback_t = typename BasicJsonType::parser_callback_t;
|
|
|
|
using parse_event_t = typename BasicJsonType::parse_event_t;
|
|
|
|
|
|
|
|
json_sax_dom_callback_parser(BasicJsonType& r,
|
2018-03-29 05:39:39 +08:00
|
|
|
const parser_callback_t cb,
|
2018-03-18 22:13:53 +08:00
|
|
|
const bool allow_exceptions_ = true)
|
|
|
|
: root(r), callback(cb), allow_exceptions(allow_exceptions_)
|
|
|
|
{
|
|
|
|
keep_stack.push_back(true);
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:01:49 +08:00
|
|
|
// make class move-only
|
|
|
|
json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
|
2021-03-24 14:15:18 +08:00
|
|
|
json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
|
2019-03-17 19:01:49 +08:00
|
|
|
json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
|
2021-03-24 14:15:18 +08:00
|
|
|
json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
|
2019-03-17 19:01:49 +08:00
|
|
|
~json_sax_dom_callback_parser() = default;
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool null()
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
|
|
|
handle_value(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool boolean(bool val)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool number_integer(number_integer_t val)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool number_unsigned(number_unsigned_t val)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 00:39:18 +08:00
|
|
|
bool number_float(number_float_t val, const string_t& /*unused*/)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool string(string_t& val)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:50:27 +08:00
|
|
|
bool binary(binary_t& val)
|
2019-07-05 12:13:25 +08:00
|
|
|
{
|
2020-05-17 19:51:59 +08:00
|
|
|
handle_value(std::move(val));
|
2019-07-05 12:13:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:09:08 +08:00
|
|
|
bool start_object(std::size_t len)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2018-03-29 05:39:39 +08:00
|
|
|
// check callback for object start
|
2018-05-07 01:00:49 +08:00
|
|
|
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
|
2018-03-18 22:13:53 +08:00
|
|
|
keep_stack.push_back(keep);
|
|
|
|
|
2018-05-07 01:00:49 +08:00
|
|
|
auto val = handle_value(BasicJsonType::value_t::object, true);
|
2018-03-29 05:39:39 +08:00
|
|
|
ref_stack.push_back(val.second);
|
2018-03-18 22:13:53 +08:00
|
|
|
|
2018-03-29 05:39:39 +08:00
|
|
|
// check object limit
|
2021-12-29 16:47:05 +08:00
|
|
|
if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2022-04-12 20:18:16 +08:00
|
|
|
JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
|
2018-03-18 22:13:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool key(string_t& val)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2018-03-22 03:12:06 +08:00
|
|
|
BasicJsonType k = BasicJsonType(val);
|
2018-03-18 22:13:53 +08:00
|
|
|
|
2018-03-29 05:39:39 +08:00
|
|
|
// check callback for key
|
|
|
|
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
|
|
|
|
key_keep_stack.push_back(keep);
|
|
|
|
|
|
|
|
// add discarded value at given key and store the reference for later
|
2020-06-03 20:20:36 +08:00
|
|
|
if (keep && ref_stack.back())
|
2018-03-29 05:39:39 +08:00
|
|
|
{
|
|
|
|
object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
|
|
|
|
}
|
|
|
|
|
2018-03-18 22:13:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool end_object()
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2021-01-11 05:40:50 +08:00
|
|
|
if (ref_stack.back())
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2021-01-11 05:40:50 +08:00
|
|
|
if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
|
|
|
|
{
|
|
|
|
// discard object
|
|
|
|
*ref_stack.back() = discarded;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-15 04:55:49 +08:00
|
|
|
ref_stack.back()->set_parents();
|
2021-01-11 05:40:50 +08:00
|
|
|
}
|
2018-03-18 22:13:53 +08:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:04:40 +08:00
|
|
|
JSON_ASSERT(!ref_stack.empty());
|
|
|
|
JSON_ASSERT(!keep_stack.empty());
|
2018-03-18 22:13:53 +08:00
|
|
|
ref_stack.pop_back();
|
|
|
|
keep_stack.pop_back();
|
2018-03-29 05:39:39 +08:00
|
|
|
|
2020-06-03 20:20:36 +08:00
|
|
|
if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
|
2018-03-29 05:39:39 +08:00
|
|
|
{
|
|
|
|
// remove discarded value
|
2019-03-18 20:53:48 +08:00
|
|
|
for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
|
2018-03-29 05:39:39 +08:00
|
|
|
{
|
2019-03-18 20:53:48 +08:00
|
|
|
if (it->is_discarded())
|
2018-03-29 05:39:39 +08:00
|
|
|
{
|
2019-03-18 20:53:48 +08:00
|
|
|
ref_stack.back()->erase(it);
|
|
|
|
break;
|
2018-03-29 05:39:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 22:13:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:09:08 +08:00
|
|
|
bool start_array(std::size_t len)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2018-05-07 01:00:49 +08:00
|
|
|
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
|
2018-03-18 22:13:53 +08:00
|
|
|
keep_stack.push_back(keep);
|
|
|
|
|
2018-05-07 01:00:49 +08:00
|
|
|
auto val = handle_value(BasicJsonType::value_t::array, true);
|
2018-03-29 05:39:39 +08:00
|
|
|
ref_stack.push_back(val.second);
|
2018-03-18 22:13:53 +08:00
|
|
|
|
2018-03-29 05:39:39 +08:00
|
|
|
// check array limit
|
2021-12-29 16:47:05 +08:00
|
|
|
if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2022-04-12 20:18:16 +08:00
|
|
|
JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
|
2018-03-18 22:13:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool end_array()
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2018-03-29 05:39:39 +08:00
|
|
|
bool keep = true;
|
|
|
|
|
|
|
|
if (ref_stack.back())
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2018-03-29 05:39:39 +08:00
|
|
|
keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
|
2021-01-11 05:40:50 +08:00
|
|
|
if (keep)
|
|
|
|
{
|
2021-01-15 04:55:49 +08:00
|
|
|
ref_stack.back()->set_parents();
|
2021-01-11 05:40:50 +08:00
|
|
|
}
|
|
|
|
else
|
2018-03-29 05:39:39 +08:00
|
|
|
{
|
|
|
|
// discard array
|
|
|
|
*ref_stack.back() = discarded;
|
|
|
|
}
|
2018-03-18 22:13:53 +08:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:04:40 +08:00
|
|
|
JSON_ASSERT(!ref_stack.empty());
|
|
|
|
JSON_ASSERT(!keep_stack.empty());
|
2018-03-18 22:13:53 +08:00
|
|
|
ref_stack.pop_back();
|
|
|
|
keep_stack.pop_back();
|
2018-03-29 05:39:39 +08:00
|
|
|
|
|
|
|
// remove discarded value
|
2020-06-03 20:20:36 +08:00
|
|
|
if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
|
2018-03-29 05:39:39 +08:00
|
|
|
{
|
2019-03-18 20:53:48 +08:00
|
|
|
ref_stack.back()->m_value.array->pop_back();
|
2018-03-29 05:39:39 +08:00
|
|
|
}
|
|
|
|
|
2018-03-18 22:13:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-16 20:45:39 +08:00
|
|
|
template<class Exception>
|
2018-10-08 00:39:18 +08:00
|
|
|
bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
|
2020-07-16 20:45:39 +08:00
|
|
|
const Exception& ex)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
|
|
|
errored = true;
|
2020-07-25 04:21:30 +08:00
|
|
|
static_cast<void>(ex);
|
2018-03-18 22:13:53 +08:00
|
|
|
if (allow_exceptions)
|
|
|
|
{
|
2020-07-16 20:45:39 +08:00
|
|
|
JSON_THROW(ex);
|
2018-03-18 22:13:53 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-29 05:39:39 +08:00
|
|
|
constexpr bool is_errored() const
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
|
|
|
return errored;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/*!
|
2018-05-07 01:00:49 +08:00
|
|
|
@param[in] v value to add to the JSON value we build during parsing
|
|
|
|
@param[in] skip_callback whether we should skip calling the callback
|
|
|
|
function; this is required after start_array() and
|
|
|
|
start_object() SAX events, because otherwise we would call the
|
|
|
|
callback function with an empty array or object, respectively.
|
|
|
|
|
2018-03-18 22:13:53 +08:00
|
|
|
@invariant If the ref stack is empty, then the passed value will be the new
|
|
|
|
root.
|
|
|
|
@invariant If the ref stack contains a value, then it is an array or an
|
|
|
|
object to which we can add elements
|
2018-05-07 01:00:49 +08:00
|
|
|
|
2018-03-29 05:39:39 +08:00
|
|
|
@return pair of boolean (whether value should be kept) and pointer (to the
|
|
|
|
passed value in the ref_stack hierarchy; nullptr if not kept)
|
2018-03-18 22:13:53 +08:00
|
|
|
*/
|
|
|
|
template<typename Value>
|
2018-05-07 01:00:49 +08:00
|
|
|
std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
|
2018-03-18 22:13:53 +08:00
|
|
|
{
|
2020-07-11 20:04:40 +08:00
|
|
|
JSON_ASSERT(!keep_stack.empty());
|
2018-03-29 05:39:39 +08:00
|
|
|
|
|
|
|
// do not handle this value if we know it would be added to a discarded
|
|
|
|
// container
|
2020-06-03 20:20:36 +08:00
|
|
|
if (!keep_stack.back())
|
2018-03-29 05:39:39 +08:00
|
|
|
{
|
|
|
|
return {false, nullptr};
|
|
|
|
}
|
|
|
|
|
2018-05-07 01:00:49 +08:00
|
|
|
// create value
|
2018-03-29 05:39:39 +08:00
|
|
|
auto value = BasicJsonType(std::forward<Value>(v));
|
2018-05-07 01:00:49 +08:00
|
|
|
|
|
|
|
// check callback
|
2020-06-03 20:20:36 +08:00
|
|
|
const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
|
2018-03-29 05:39:39 +08:00
|
|
|
|
|
|
|
// do not handle this value if we just learnt it shall be discarded
|
2020-06-03 20:20:36 +08:00
|
|
|
if (!keep)
|
2018-03-29 05:39:39 +08:00
|
|
|
{
|
|
|
|
return {false, nullptr};
|
|
|
|
}
|
|
|
|
|
2018-03-18 22:13:53 +08:00
|
|
|
if (ref_stack.empty())
|
|
|
|
{
|
2018-03-29 05:39:39 +08:00
|
|
|
root = std::move(value);
|
|
|
|
return {true, &root};
|
2018-03-18 22:13:53 +08:00
|
|
|
}
|
2018-10-08 01:07:58 +08:00
|
|
|
|
|
|
|
// skip this value if we already decided to skip the parent
|
|
|
|
// (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
|
2020-06-03 20:20:36 +08:00
|
|
|
if (!ref_stack.back())
|
2018-10-08 01:07:58 +08:00
|
|
|
{
|
|
|
|
return {false, nullptr};
|
|
|
|
}
|
|
|
|
|
|
|
|
// we now only expect arrays and objects
|
2020-07-11 20:04:40 +08:00
|
|
|
JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
|
2018-10-08 01:07:58 +08:00
|
|
|
|
2019-03-18 20:53:48 +08:00
|
|
|
// array
|
2018-10-08 01:07:58 +08:00
|
|
|
if (ref_stack.back()->is_array())
|
|
|
|
{
|
2021-01-03 04:36:11 +08:00
|
|
|
ref_stack.back()->m_value.array->emplace_back(std::move(value));
|
2018-10-08 01:07:58 +08:00
|
|
|
return {true, &(ref_stack.back()->m_value.array->back())};
|
|
|
|
}
|
2018-08-18 05:09:45 +08:00
|
|
|
|
2019-03-18 20:53:48 +08:00
|
|
|
// object
|
2020-07-06 18:22:31 +08:00
|
|
|
JSON_ASSERT(ref_stack.back()->is_object());
|
2019-03-18 20:53:48 +08:00
|
|
|
// check if we should store an element for the current key
|
2020-07-11 20:04:40 +08:00
|
|
|
JSON_ASSERT(!key_keep_stack.empty());
|
2019-03-18 20:53:48 +08:00
|
|
|
const bool store_element = key_keep_stack.back();
|
|
|
|
key_keep_stack.pop_back();
|
2018-03-29 05:39:39 +08:00
|
|
|
|
2020-06-03 20:20:36 +08:00
|
|
|
if (!store_element)
|
2019-03-18 20:53:48 +08:00
|
|
|
{
|
|
|
|
return {false, nullptr};
|
2018-03-18 22:13:53 +08:00
|
|
|
}
|
2019-03-18 20:53:48 +08:00
|
|
|
|
2020-07-06 18:22:31 +08:00
|
|
|
JSON_ASSERT(object_element);
|
2019-03-18 20:53:48 +08:00
|
|
|
*object_element = std::move(value);
|
|
|
|
return {true, object_element};
|
2018-03-18 22:13:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// the parsed JSON value
|
|
|
|
BasicJsonType& root;
|
|
|
|
/// stack to model hierarchy of values
|
2019-03-17 19:01:49 +08:00
|
|
|
std::vector<BasicJsonType*> ref_stack {};
|
2018-03-18 22:13:53 +08:00
|
|
|
/// stack to manage which values to keep
|
2019-03-17 19:01:49 +08:00
|
|
|
std::vector<bool> keep_stack {};
|
2018-03-29 05:39:39 +08:00
|
|
|
/// stack to manage which object keys to keep
|
2019-03-17 19:01:49 +08:00
|
|
|
std::vector<bool> key_keep_stack {};
|
2018-03-18 22:13:53 +08:00
|
|
|
/// helper to hold the reference for the next object element
|
|
|
|
BasicJsonType* object_element = nullptr;
|
|
|
|
/// whether a syntax error occurred
|
|
|
|
bool errored = false;
|
|
|
|
/// callback function
|
|
|
|
const parser_callback_t callback = nullptr;
|
|
|
|
/// whether to throw exceptions in case of errors
|
|
|
|
const bool allow_exceptions = true;
|
|
|
|
/// a discarded value for the callback
|
|
|
|
BasicJsonType discarded = BasicJsonType::value_t::discarded;
|
|
|
|
};
|
|
|
|
|
2018-03-08 05:40:48 +08:00
|
|
|
template<typename BasicJsonType>
|
2018-07-02 16:14:37 +08:00
|
|
|
class json_sax_acceptor
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
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;
|
2018-03-14 14:18:42 +08:00
|
|
|
using string_t = typename BasicJsonType::string_t;
|
2020-05-18 04:50:27 +08:00
|
|
|
using binary_t = typename BasicJsonType::binary_t;
|
2018-03-08 05:40:48 +08:00
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool null()
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 00:39:18 +08:00
|
|
|
bool boolean(bool /*unused*/)
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 00:39:18 +08:00
|
|
|
bool number_integer(number_integer_t /*unused*/)
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 00:39:18 +08:00
|
|
|
bool number_unsigned(number_unsigned_t /*unused*/)
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 00:39:18 +08:00
|
|
|
bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 00:39:18 +08:00
|
|
|
bool string(string_t& /*unused*/)
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:50:27 +08:00
|
|
|
bool binary(binary_t& /*unused*/)
|
2019-07-05 12:13:25 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-29 16:47:05 +08:00
|
|
|
bool start_object(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 00:39:18 +08:00
|
|
|
bool key(string_t& /*unused*/)
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool end_object()
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-29 16:47:05 +08:00
|
|
|
bool start_array(std::size_t /*unused*/ = static_cast<std::size_t>(-1))
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-02 16:14:37 +08:00
|
|
|
bool end_array()
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-08 00:39:18 +08:00
|
|
|
bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
|
2018-03-08 05:40:48 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-31 03:59:13 +08:00
|
|
|
} // namespace detail
|
|
|
|
NLOHMANN_JSON_NAMESPACE_END
|