2018-02-26 19:08:12 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief SAX interface
|
|
|
|
*/
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
struct json_sax
|
|
|
|
{
|
|
|
|
/// type for (signed) integers
|
|
|
|
using number_integer_t = typename BasicJsonType::number_integer_t;
|
|
|
|
/// type for unsigned integers
|
|
|
|
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
|
|
|
|
/// type for floating-point numbers
|
|
|
|
using number_float_t = typename BasicJsonType::number_float_t;
|
|
|
|
|
2018-02-26 22:39:23 +00:00
|
|
|
/// constant to indicate that no size limit is given for array or object
|
|
|
|
static constexpr auto no_limit = std::size_t(-1);
|
|
|
|
|
2018-02-26 19:08:12 +00: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;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief an floating-point number was read
|
|
|
|
@param[in] val floating-point value
|
|
|
|
@param[in] s raw token value
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
|
|
|
virtual bool number_float(number_float_t val, const std::string& s) = 0;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief a string was read
|
|
|
|
@param[in] val string value
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
2018-02-26 22:39:23 +00:00
|
|
|
virtual bool string(std::string&& val) = 0;
|
2018-02-26 19:08:12 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief the beginning of an object was read
|
2018-02-26 22:39:23 +00:00
|
|
|
@param[in] elements number of object elements or no_limit if unknown
|
2018-02-26 19:08:12 +00:00
|
|
|
@return whether parsing should proceed
|
|
|
|
@note binary formats may report the number of elements
|
|
|
|
*/
|
2018-02-26 22:39:23 +00:00
|
|
|
virtual bool start_object(std::size_t elements = no_limit) = 0;
|
2018-02-26 19:08:12 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief an object key was read
|
|
|
|
@param[in] val object key
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
2018-02-26 22:39:23 +00:00
|
|
|
virtual bool key(std::string&& val) = 0;
|
2018-02-26 19:08:12 +00: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-02-26 22:39:23 +00:00
|
|
|
@param[in] elements number of array elements or no_limit if unknown
|
2018-02-26 19:08:12 +00:00
|
|
|
@return whether parsing should proceed
|
|
|
|
@note binary formats may report the number of elements
|
|
|
|
*/
|
2018-02-26 22:39:23 +00:00
|
|
|
virtual bool start_array(std::size_t elements = no_limit) = 0;
|
2018-02-26 19:08:12 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief the end of an array was read
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
|
|
|
virtual bool end_array() = 0;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief a binary value was read
|
|
|
|
@param[in] val byte vector
|
|
|
|
@return whether parsing should proceed
|
|
|
|
@note examples are CBOR type 2 strings, MessagePack bin, and maybe UBJSON
|
|
|
|
array<uint8t>
|
|
|
|
*/
|
|
|
|
virtual bool binary(const std::vector<uint8_t>& val) = 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-03-05 15:46:35 +00:00
|
|
|
@param[in] error_msg a detailed error message
|
2018-02-26 19:08:12 +00:00
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
2018-03-05 15:46:35 +00:00
|
|
|
virtual bool parse_error(std::size_t position,
|
|
|
|
const std::string& last_token,
|
|
|
|
const std::string& error_msg) = 0;
|
2018-02-26 19:08:12 +00:00
|
|
|
|
|
|
|
virtual ~json_sax() = default;
|
|
|
|
};
|
2018-03-05 20:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
class json_sax_dom_parser : public json_sax<BasicJsonType>
|
|
|
|
{
|
|
|
|
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-06 17:17:07 +00:00
|
|
|
json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
|
|
|
|
: root(r), allow_exceptions(allow_exceptions_)
|
|
|
|
{}
|
|
|
|
|
2018-03-05 20:06:00 +00:00
|
|
|
bool null() override
|
|
|
|
{
|
|
|
|
handle_value(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool boolean(bool val) override
|
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool number_integer(number_integer_t val) override
|
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool number_unsigned(number_unsigned_t val) override
|
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool number_float(number_float_t val, const std::string&) override
|
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool string(std::string&& val) override
|
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool start_object(std::size_t) override
|
|
|
|
{
|
|
|
|
ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool key(std::string&& val) override
|
|
|
|
{
|
2018-03-06 06:19:05 +00:00
|
|
|
// add null at given key and store the reference for later
|
|
|
|
object_element = &(ref_stack.back()->m_value.object->operator[](val));
|
2018-03-05 20:06:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool end_object() override
|
|
|
|
{
|
|
|
|
ref_stack.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool start_array(std::size_t) override
|
|
|
|
{
|
|
|
|
ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool end_array() override
|
|
|
|
{
|
|
|
|
ref_stack.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool binary(const std::vector<uint8_t>&) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:17:07 +00:00
|
|
|
bool parse_error(std::size_t position, const std::string& token,
|
|
|
|
const std::string& error_msg) override
|
2018-03-05 20:06:00 +00:00
|
|
|
{
|
2018-03-06 17:17:07 +00:00
|
|
|
errored = true;
|
|
|
|
if (allow_exceptions)
|
|
|
|
{
|
|
|
|
if (error_msg == "number overflow")
|
|
|
|
{
|
|
|
|
JSON_THROW(BasicJsonType::out_of_range::create(406, "number overflow parsing '" + token + "'"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
JSON_THROW(BasicJsonType::parse_error::create(101, position, error_msg));
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 20:06:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:17:07 +00:00
|
|
|
bool is_errored() const
|
2018-03-05 20:06:00 +00:00
|
|
|
{
|
2018-03-06 17:17:07 +00:00
|
|
|
return errored;
|
2018-03-05 20:06:00 +00: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>
|
|
|
|
BasicJsonType* handle_value(Value&& v)
|
|
|
|
{
|
|
|
|
if (ref_stack.empty())
|
|
|
|
{
|
|
|
|
root = BasicJsonType(std::forward<Value>(v));
|
|
|
|
return &root;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
|
|
|
|
if (ref_stack.back()->is_array())
|
|
|
|
{
|
|
|
|
ref_stack.back()->m_value.array->push_back(BasicJsonType(std::forward<Value>(v)));
|
|
|
|
return &(ref_stack.back()->m_value.array->back());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-06 06:19:05 +00:00
|
|
|
assert(object_element);
|
|
|
|
*object_element = BasicJsonType(std::forward<Value>(v));
|
|
|
|
return object_element;
|
2018-03-05 20:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 17:17:07 +00:00
|
|
|
|
|
|
|
/// the parsed JSON value
|
|
|
|
BasicJsonType& root;
|
|
|
|
/// stack to model hierarchy of values
|
|
|
|
std::vector<BasicJsonType*> ref_stack;
|
|
|
|
/// 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-05 20:06:00 +00:00
|
|
|
};
|
|
|
|
|
2018-02-26 19:08:12 +00:00
|
|
|
}
|
|
|
|
|