2018-02-26 20:08:12 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-03-18 15:13:53 +01:00
|
|
|
#include <nlohmann/detail/input/parser.hpp>
|
2018-03-11 22:47:25 +01:00
|
|
|
#include <nlohmann/detail/exceptions.hpp>
|
|
|
|
|
2018-02-26 20:08:12 +01:00
|
|
|
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-03-14 07:18:42 +01:00
|
|
|
/// type for strings
|
|
|
|
using string_t = typename BasicJsonType::string_t;
|
2018-02-26 20:08:12 +01:00
|
|
|
|
2018-02-26 23:39:23 +01: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 20:08:12 +01: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
|
|
|
|
*/
|
2018-03-14 07:18:42 +01:00
|
|
|
virtual bool number_float(number_float_t val, const string_t& s) = 0;
|
2018-02-26 20:08:12 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief a string was read
|
|
|
|
@param[in] val string value
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
2018-03-21 20:12:06 +01:00
|
|
|
virtual bool string(string_t& val) = 0;
|
2018-02-26 20:08:12 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief the beginning of an object was read
|
2018-02-26 23:39:23 +01:00
|
|
|
@param[in] elements number of object elements or no_limit if unknown
|
2018-02-26 20:08:12 +01:00
|
|
|
@return whether parsing should proceed
|
|
|
|
@note binary formats may report the number of elements
|
|
|
|
*/
|
2018-02-26 23:39:23 +01:00
|
|
|
virtual bool start_object(std::size_t elements = no_limit) = 0;
|
2018-02-26 20:08:12 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief an object key was read
|
|
|
|
@param[in] val object key
|
|
|
|
@return whether parsing should proceed
|
|
|
|
*/
|
2018-03-21 20:12:06 +01:00
|
|
|
virtual bool key(string_t& val) = 0;
|
2018-02-26 20:08:12 +01: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 23:39:23 +01:00
|
|
|
@param[in] elements number of array elements or no_limit if unknown
|
2018-02-26 20:08:12 +01:00
|
|
|
@return whether parsing should proceed
|
|
|
|
@note binary formats may report the number of elements
|
|
|
|
*/
|
2018-02-26 23:39:23 +01:00
|
|
|
virtual bool start_array(std::size_t elements = no_limit) = 0;
|
2018-02-26 20:08:12 +01: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-03-05 16:46:35 +01:00
|
|
|
@param[in] error_msg a detailed error message
|
2018-03-20 22:39:08 +01:00
|
|
|
@return whether parsing should proceed (must return false)
|
2018-02-26 20:08:12 +01:00
|
|
|
*/
|
2018-03-05 16:46:35 +01:00
|
|
|
virtual bool parse_error(std::size_t position,
|
|
|
|
const std::string& last_token,
|
2018-03-11 22:47:25 +01:00
|
|
|
const detail::exception& ex) = 0;
|
2018-02-26 20:08:12 +01:00
|
|
|
|
|
|
|
virtual ~json_sax() = default;
|
|
|
|
};
|
2018-03-05 21:06:00 +01:00
|
|
|
|
|
|
|
|
2018-03-11 18:47:38 +01:00
|
|
|
namespace detail
|
|
|
|
{
|
2018-03-05 21:06:00 +01: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-14 07:18:42 +01:00
|
|
|
using string_t = typename BasicJsonType::string_t;
|
2018-03-05 21:06:00 +01:00
|
|
|
|
2018-03-06 18:17:07 +01:00
|
|
|
json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
|
|
|
|
: root(r), allow_exceptions(allow_exceptions_)
|
|
|
|
{}
|
|
|
|
|
2018-03-05 21:06:00 +01: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;
|
|
|
|
}
|
|
|
|
|
2018-03-14 07:18:42 +01:00
|
|
|
bool number_float(number_float_t val, const string_t&) override
|
2018-03-05 21:06:00 +01:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:12:06 +01:00
|
|
|
bool string(string_t& val) override
|
2018-03-05 21:06:00 +01:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:47:38 +01:00
|
|
|
bool start_object(std::size_t len) override
|
2018-03-05 21:06:00 +01:00
|
|
|
{
|
|
|
|
ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
|
2018-03-11 18:47:38 +01:00
|
|
|
|
|
|
|
if (JSON_UNLIKELY(len != json_sax<BasicJsonType>::no_limit and len > ref_stack.back()->max_size()))
|
|
|
|
{
|
|
|
|
JSON_THROW(out_of_range::create(408,
|
|
|
|
"excessive object size: " + std::to_string(len)));
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:06:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:12:06 +01:00
|
|
|
bool key(string_t& val) override
|
2018-03-05 21:06:00 +01:00
|
|
|
{
|
2018-03-06 07:19:05 +01: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 21:06:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool end_object() override
|
|
|
|
{
|
|
|
|
ref_stack.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:47:38 +01:00
|
|
|
bool start_array(std::size_t len) override
|
2018-03-05 21:06:00 +01:00
|
|
|
{
|
|
|
|
ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
|
2018-03-11 18:47:38 +01:00
|
|
|
|
|
|
|
if (JSON_UNLIKELY(len != json_sax<BasicJsonType>::no_limit and len > ref_stack.back()->max_size()))
|
|
|
|
{
|
|
|
|
JSON_THROW(out_of_range::create(408,
|
|
|
|
"excessive array size: " + std::to_string(len)));
|
|
|
|
}
|
|
|
|
|
2018-03-05 21:06:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool end_array() override
|
|
|
|
{
|
|
|
|
ref_stack.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-11 22:47:25 +01:00
|
|
|
bool parse_error(std::size_t, const std::string&,
|
|
|
|
const detail::exception& ex) override
|
2018-03-05 21:06:00 +01:00
|
|
|
{
|
2018-03-06 18:17:07 +01:00
|
|
|
errored = true;
|
|
|
|
if (allow_exceptions)
|
|
|
|
{
|
2018-03-11 22:47:25 +01:00
|
|
|
// determine the proper exception type from the id
|
|
|
|
switch ((ex.id / 100) % 100)
|
2018-03-06 18:17:07 +01:00
|
|
|
{
|
2018-03-11 22:47:25 +01:00
|
|
|
case 1:
|
|
|
|
JSON_THROW(*reinterpret_cast<const detail::parse_error*>(&ex));
|
|
|
|
case 2:
|
2018-03-17 19:15:59 +01:00
|
|
|
JSON_THROW(*reinterpret_cast<const detail::invalid_iterator*>(&ex)); // LCOV_EXCL_LINE
|
2018-03-11 22:47:25 +01:00
|
|
|
case 3:
|
2018-03-17 19:15:59 +01:00
|
|
|
JSON_THROW(*reinterpret_cast<const detail::type_error*>(&ex)); // LCOV_EXCL_LINE
|
2018-03-11 22:47:25 +01:00
|
|
|
case 4:
|
|
|
|
JSON_THROW(*reinterpret_cast<const detail::out_of_range*>(&ex));
|
|
|
|
case 5:
|
2018-03-17 19:15:59 +01:00
|
|
|
JSON_THROW(*reinterpret_cast<const detail::other_error*>(&ex)); // LCOV_EXCL_LINE
|
2018-03-11 22:47:25 +01:00
|
|
|
default:
|
|
|
|
assert(false); // LCOV_EXCL_LINE
|
2018-03-06 18:17:07 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-05 21:06:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-28 23:39:39 +02:00
|
|
|
constexpr bool is_errored() const
|
2018-03-05 21:06:00 +01:00
|
|
|
{
|
2018-03-06 18:17:07 +01:00
|
|
|
return errored;
|
2018-03-05 21:06:00 +01: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
|
|
|
|
{
|
2018-03-18 16:07:21 +01:00
|
|
|
assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
|
|
|
|
if (ref_stack.back()->is_array())
|
2018-03-05 21:06:00 +01:00
|
|
|
{
|
2018-03-18 16:07:21 +01:00
|
|
|
ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
|
|
|
|
return &(ref_stack.back()->m_value.array->back());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(object_element);
|
|
|
|
*object_element = BasicJsonType(std::forward<Value>(v));
|
|
|
|
return object_element;
|
2018-03-05 21:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 18:17:07 +01: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 21:06:00 +01:00
|
|
|
};
|
|
|
|
|
2018-03-18 15:13:53 +01:00
|
|
|
template<typename BasicJsonType>
|
|
|
|
class json_sax_dom_callback_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;
|
|
|
|
using string_t = typename BasicJsonType::string_t;
|
|
|
|
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-28 23:39:39 +02:00
|
|
|
const parser_callback_t cb,
|
2018-03-18 15:13:53 +01:00
|
|
|
const bool allow_exceptions_ = true)
|
|
|
|
: root(r), callback(cb), allow_exceptions(allow_exceptions_)
|
|
|
|
{
|
|
|
|
keep_stack.push_back(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 string_t&) override
|
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:12:06 +01:00
|
|
|
bool string(string_t& val) override
|
2018-03-18 15:13:53 +01:00
|
|
|
{
|
|
|
|
handle_value(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool start_object(std::size_t len) override
|
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
// check callback for object start
|
|
|
|
const bool keep = callback(static_cast<int>(ref_stack.size()) + 1, parse_event_t::object_start, discarded);
|
2018-03-18 15:13:53 +01:00
|
|
|
keep_stack.push_back(keep);
|
|
|
|
|
2018-03-28 23:39:39 +02:00
|
|
|
auto val = handle_value(BasicJsonType::value_t::object);
|
|
|
|
ref_stack.push_back(val.second);
|
2018-03-18 15:13:53 +01:00
|
|
|
|
2018-03-28 23:39:39 +02:00
|
|
|
// check object limit
|
|
|
|
if (ref_stack.back())
|
2018-03-18 15:13:53 +01:00
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
if (JSON_UNLIKELY(len != json_sax<BasicJsonType>::no_limit and len > ref_stack.back()->max_size()))
|
|
|
|
{
|
|
|
|
JSON_THROW(out_of_range::create(408,
|
|
|
|
"excessive object size: " + std::to_string(len)));
|
|
|
|
}
|
2018-03-18 15:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:12:06 +01:00
|
|
|
bool key(string_t& val) override
|
2018-03-18 15:13:53 +01:00
|
|
|
{
|
2018-03-21 20:12:06 +01:00
|
|
|
BasicJsonType k = BasicJsonType(val);
|
2018-03-18 15:13:53 +01:00
|
|
|
|
2018-03-28 23:39:39 +02: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
|
|
|
|
if (keep and ref_stack.back())
|
|
|
|
{
|
|
|
|
object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:13:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool end_object() override
|
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
bool keep = true;
|
|
|
|
if (ref_stack.back())
|
2018-03-18 15:13:53 +01:00
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back());
|
|
|
|
if (not keep)
|
|
|
|
{
|
|
|
|
// discard object
|
|
|
|
*ref_stack.back() = discarded;
|
|
|
|
}
|
2018-03-18 15:13:53 +01:00
|
|
|
}
|
|
|
|
|
2018-03-28 23:39:39 +02:00
|
|
|
assert(not ref_stack.empty());
|
|
|
|
assert(not keep_stack.empty());
|
2018-03-18 15:13:53 +01:00
|
|
|
ref_stack.pop_back();
|
|
|
|
keep_stack.pop_back();
|
2018-03-28 23:39:39 +02:00
|
|
|
|
|
|
|
if (not ref_stack.empty() and ref_stack.back())
|
|
|
|
{
|
|
|
|
// remove discarded value
|
|
|
|
if (ref_stack.back()->is_object())
|
|
|
|
{
|
|
|
|
for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->is_discarded())
|
|
|
|
{
|
|
|
|
ref_stack.back()->erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:13:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool start_array(std::size_t len) override
|
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
const bool keep = callback(static_cast<int>(ref_stack.size()) + 1, parse_event_t::array_start, discarded);
|
2018-03-18 15:13:53 +01:00
|
|
|
keep_stack.push_back(keep);
|
|
|
|
|
2018-03-28 23:39:39 +02:00
|
|
|
auto val = handle_value(BasicJsonType::value_t::array);
|
|
|
|
ref_stack.push_back(val.second);
|
2018-03-18 15:13:53 +01:00
|
|
|
|
2018-03-28 23:39:39 +02:00
|
|
|
// check array limit
|
|
|
|
if (ref_stack.back())
|
2018-03-18 15:13:53 +01:00
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
if (JSON_UNLIKELY(len != json_sax<BasicJsonType>::no_limit and len > ref_stack.back()->max_size()))
|
|
|
|
{
|
|
|
|
JSON_THROW(out_of_range::create(408,
|
|
|
|
"excessive array size: " + std::to_string(len)));
|
|
|
|
}
|
2018-03-18 15:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool end_array() override
|
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
bool keep = true;
|
|
|
|
|
|
|
|
if (ref_stack.back())
|
2018-03-18 15:13:53 +01:00
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
|
|
|
|
if (not keep)
|
|
|
|
{
|
|
|
|
// discard array
|
|
|
|
*ref_stack.back() = discarded;
|
|
|
|
}
|
2018-03-18 15:13:53 +01:00
|
|
|
}
|
|
|
|
|
2018-03-28 23:39:39 +02:00
|
|
|
assert(not ref_stack.empty());
|
|
|
|
assert(not keep_stack.empty());
|
2018-03-18 15:13:53 +01:00
|
|
|
ref_stack.pop_back();
|
|
|
|
keep_stack.pop_back();
|
2018-03-28 23:39:39 +02:00
|
|
|
|
|
|
|
// remove discarded value
|
|
|
|
if (not keep and not ref_stack.empty())
|
|
|
|
{
|
|
|
|
if (ref_stack.back()->is_array())
|
|
|
|
{
|
|
|
|
ref_stack.back()->m_value.array->pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:13:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse_error(std::size_t, const std::string&,
|
|
|
|
const detail::exception& ex) override
|
|
|
|
{
|
|
|
|
errored = true;
|
|
|
|
if (allow_exceptions)
|
|
|
|
{
|
|
|
|
// determine the proper exception type from the id
|
|
|
|
switch ((ex.id / 100) % 100)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
JSON_THROW(*reinterpret_cast<const detail::parse_error*>(&ex));
|
|
|
|
case 2:
|
|
|
|
JSON_THROW(*reinterpret_cast<const detail::invalid_iterator*>(&ex)); // LCOV_EXCL_LINE
|
|
|
|
case 3:
|
|
|
|
JSON_THROW(*reinterpret_cast<const detail::type_error*>(&ex)); // LCOV_EXCL_LINE
|
|
|
|
case 4:
|
|
|
|
JSON_THROW(*reinterpret_cast<const detail::out_of_range*>(&ex));
|
|
|
|
case 5:
|
|
|
|
JSON_THROW(*reinterpret_cast<const detail::other_error*>(&ex)); // LCOV_EXCL_LINE
|
|
|
|
default:
|
|
|
|
assert(false); // LCOV_EXCL_LINE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-28 23:39:39 +02:00
|
|
|
constexpr bool is_errored() const
|
2018-03-18 15:13:53 +01:00
|
|
|
{
|
|
|
|
return errored;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-03-28 23:39:39 +02: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 15:13:53 +01:00
|
|
|
*/
|
|
|
|
template<typename Value>
|
2018-03-28 23:39:39 +02:00
|
|
|
std::pair<bool, BasicJsonType*> handle_value(Value&& v)
|
2018-03-18 15:13:53 +01:00
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
assert(not keep_stack.empty());
|
|
|
|
|
|
|
|
// do not handle this value if we know it would be added to a discarded
|
|
|
|
// container
|
|
|
|
if (not keep_stack.back())
|
|
|
|
{
|
|
|
|
return {false, nullptr};
|
|
|
|
}
|
|
|
|
|
|
|
|
// create value and check callback
|
|
|
|
auto value = BasicJsonType(std::forward<Value>(v));
|
|
|
|
const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
|
|
|
|
|
|
|
|
// do not handle this value if we just learnt it shall be discarded
|
|
|
|
if (not keep)
|
|
|
|
{
|
|
|
|
return {false, nullptr};
|
|
|
|
}
|
|
|
|
|
2018-03-18 15:13:53 +01:00
|
|
|
if (ref_stack.empty())
|
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
root = std::move(value);
|
|
|
|
return {true, &root};
|
2018-03-18 15:13:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-18 16:07:21 +01:00
|
|
|
assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
|
|
|
|
if (ref_stack.back()->is_array())
|
2018-03-18 15:13:53 +01:00
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
ref_stack.back()->m_value.array->push_back(std::move(value));
|
|
|
|
return {true, &(ref_stack.back()->m_value.array->back())};
|
2018-03-18 16:07:21 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-28 23:39:39 +02:00
|
|
|
// check if we should store an element for the current key
|
|
|
|
assert(not key_keep_stack.empty());
|
|
|
|
const bool store_element = key_keep_stack.back();
|
|
|
|
key_keep_stack.pop_back();
|
|
|
|
|
|
|
|
if (not store_element)
|
|
|
|
{
|
|
|
|
return {false, nullptr};
|
|
|
|
}
|
|
|
|
|
2018-03-18 16:07:21 +01:00
|
|
|
assert(object_element);
|
2018-03-28 23:39:39 +02:00
|
|
|
*object_element = std::move(value);
|
|
|
|
return {true, object_element};
|
2018-03-18 15:13:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// the parsed JSON value
|
|
|
|
BasicJsonType& root;
|
|
|
|
/// stack to model hierarchy of values
|
|
|
|
std::vector<BasicJsonType*> ref_stack;
|
|
|
|
/// stack to manage which values to keep
|
|
|
|
std::vector<bool> keep_stack;
|
2018-03-28 23:39:39 +02:00
|
|
|
/// stack to manage which object keys to keep
|
|
|
|
std::vector<bool> key_keep_stack;
|
2018-03-18 15:13:53 +01: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-07 22:40:48 +01:00
|
|
|
template<typename BasicJsonType>
|
|
|
|
class json_sax_acceptor : 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-14 07:18:42 +01:00
|
|
|
using string_t = typename BasicJsonType::string_t;
|
2018-03-07 22:40:48 +01:00
|
|
|
|
|
|
|
bool null() override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool boolean(bool) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool number_integer(number_integer_t) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool number_unsigned(number_unsigned_t) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-14 07:18:42 +01:00
|
|
|
bool number_float(number_float_t, const string_t&) override
|
2018-03-07 22:40:48 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:12:06 +01:00
|
|
|
bool string(string_t&) override
|
2018-03-07 22:40:48 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool start_object(std::size_t) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:12:06 +01:00
|
|
|
bool key(string_t&) override
|
2018-03-07 22:40:48 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool end_object() override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool start_array(std::size_t) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool end_array() override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-11 22:47:25 +01:00
|
|
|
bool parse_error(std::size_t, const std::string&, const detail::exception&) override
|
2018-03-07 22:40:48 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2018-03-11 18:47:38 +01:00
|
|
|
}
|
2018-03-07 22:40:48 +01:00
|
|
|
|
2018-02-26 20:08:12 +01:00
|
|
|
}
|