30template<
typename BasicJsonType>
36 using string_t =
typename BasicJsonType::string_t;
37 using binary_t =
typename BasicJsonType::binary_t;
134 const std::string& last_token,
160template<
typename BasicJsonType>
176 : root(r), allow_exceptions(allow_exceptions_)
188 handle_value(
nullptr);
224 handle_value(std::move(val));
230 ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
232 if (
JSON_HEDLEY_UNLIKELY(len !=
static_cast<std::size_t
>(-1) && len > ref_stack.back()->max_size()))
234 JSON_THROW(out_of_range::create(408,
concat(
"excessive object size: ", std::to_string(len)), ref_stack.back()));
246 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
255 ref_stack.back()->set_parents();
256 ref_stack.pop_back();
262 ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
264 if (
JSON_HEDLEY_UNLIKELY(len !=
static_cast<std::size_t
>(-1) && len > ref_stack.back()->max_size()))
266 JSON_THROW(out_of_range::create(408,
concat(
"excessive array size: ", std::to_string(len)), ref_stack.back()));
277 ref_stack.back()->set_parents();
278 ref_stack.pop_back();
282 template<
class Exception>
287 static_cast<void>(ex);
288 if (allow_exceptions)
307 template<
typename Value>
309 BasicJsonType* handle_value(Value&& v)
311 if (ref_stack.empty())
313 root = BasicJsonType(std::forward<Value>(v));
317 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
319 if (ref_stack.back()->is_array())
321 ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
322 return &(ref_stack.back()->m_data.m_value.array->back());
327 *object_element = BasicJsonType(std::forward<Value>(v));
328 return object_element;
334 std::vector<BasicJsonType*> ref_stack {};
336 BasicJsonType* object_element =
nullptr;
338 bool errored =
false;
340 const bool allow_exceptions =
true;
343template<
typename BasicJsonType>
357 const bool allow_exceptions_ =
true)
358 : root(r), callback(cb), allow_exceptions(allow_exceptions_)
360 keep_stack.push_back(
true);
372 handle_value(
nullptr);
408 handle_value(std::move(val));
415 const bool keep = callback(
static_cast<int>(ref_stack.size()), parse_event_t::object_start,
discarded);
416 keep_stack.push_back(keep);
418 auto val = handle_value(BasicJsonType::value_t::object,
true);
419 ref_stack.push_back(val.second);
422 if (ref_stack.back() &&
JSON_HEDLEY_UNLIKELY(len !=
static_cast<std::size_t
>(-1) && len > ref_stack.back()->max_size()))
424 JSON_THROW(out_of_range::create(408,
concat(
"excessive object size: ", std::to_string(len)), ref_stack.back()));
432 BasicJsonType k = BasicJsonType(val);
435 const bool keep = callback(
static_cast<int>(ref_stack.size()), parse_event_t::key, k);
436 key_keep_stack.push_back(keep);
439 if (keep && ref_stack.back())
441 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) =
discarded);
449 if (ref_stack.back())
451 if (!callback(
static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
458 ref_stack.back()->set_parents();
464 ref_stack.pop_back();
465 keep_stack.pop_back();
467 if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
470 for (
auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
472 if (it->is_discarded())
474 ref_stack.back()->erase(it);
485 const bool keep = callback(
static_cast<int>(ref_stack.size()), parse_event_t::array_start,
discarded);
486 keep_stack.push_back(keep);
488 auto val = handle_value(BasicJsonType::value_t::array,
true);
489 ref_stack.push_back(val.second);
492 if (ref_stack.back() &&
JSON_HEDLEY_UNLIKELY(len !=
static_cast<std::size_t
>(-1) && len > ref_stack.back()->max_size()))
494 JSON_THROW(out_of_range::create(408,
concat(
"excessive array size: ", std::to_string(len)), ref_stack.back()));
504 if (ref_stack.back())
506 keep = callback(
static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
509 ref_stack.back()->set_parents();
520 ref_stack.pop_back();
521 keep_stack.pop_back();
524 if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
526 ref_stack.back()->m_data.m_value.array->pop_back();
532 template<
class Exception>
537 static_cast<void>(ex);
538 if (allow_exceptions)
566 template<
typename Value>
567 std::pair<bool, BasicJsonType*> handle_value(Value&& v,
const bool skip_callback =
false)
573 if (!keep_stack.back())
575 return {
false,
nullptr};
579 auto value = BasicJsonType(std::forward<Value>(v));
582 const bool keep = skip_callback || callback(
static_cast<int>(ref_stack.size()), parse_event_t::value, value);
587 return {
false,
nullptr};
590 if (ref_stack.empty())
592 root = std::move(value);
593 return {
true, & root};
598 if (!ref_stack.back())
600 return {
false,
nullptr};
604 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
607 if (ref_stack.back()->is_array())
609 ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
610 return {
true, & (ref_stack.back()->m_data.m_value.array->back())};
617 const bool store_element = key_keep_stack.back();
618 key_keep_stack.pop_back();
622 return {
false,
nullptr};
626 *object_element = std::move(value);
627 return {
true, object_element};
633 std::vector<BasicJsonType*> ref_stack {};
635 std::vector<bool> keep_stack {};
637 std::vector<bool> key_keep_stack {};
639 BasicJsonType* object_element =
nullptr;
641 bool errored =
false;
645 const bool allow_exceptions =
true;
647 BasicJsonType
discarded = BasicJsonType::value_t::discarded;
650template<
typename BasicJsonType>
#define WPI_JSON_NAMESPACE_END
Definition abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition abi_macros.h:53
general exception of the basic_json class
Definition exceptions.h:39
Definition json_sax.h:652
typename BasicJsonType::number_unsigned_t number_unsigned_t
Definition json_sax.h:655
bool end_object()
Definition json_sax.h:705
bool start_object(std::size_t=static_cast< std::size_t >(-1))
Definition json_sax.h:695
bool binary(binary_t &)
Definition json_sax.h:690
bool number_integer(number_integer_t)
Definition json_sax.h:670
bool start_array(std::size_t=static_cast< std::size_t >(-1))
Definition json_sax.h:710
bool boolean(bool)
Definition json_sax.h:665
bool null()
Definition json_sax.h:660
bool end_array()
Definition json_sax.h:715
bool number_unsigned(number_unsigned_t)
Definition json_sax.h:675
bool string(string_t &)
Definition json_sax.h:685
typename BasicJsonType::binary_t binary_t
Definition json_sax.h:658
bool number_float(number_float_t, const string_t &)
Definition json_sax.h:680
bool parse_error(std::size_t, const std::string &, const detail::exception &)
Definition json_sax.h:720
bool key(string_t &)
Definition json_sax.h:700
typename BasicJsonType::number_integer_t number_integer_t
Definition json_sax.h:654
typename BasicJsonType::number_float_t number_float_t
Definition json_sax.h:656
typename BasicJsonType::string_t string_t
Definition json_sax.h:657
Definition json_sax.h:345
bool boolean(bool val)
Definition json_sax.h:376
bool parse_error(std::size_t, const std::string &, const Exception &ex)
Definition json_sax.h:533
typename BasicJsonType::string_t string_t
Definition json_sax.h:350
bool number_float(number_float_t val, const string_t &)
Definition json_sax.h:394
constexpr bool is_errored() const
Definition json_sax.h:545
bool string(string_t &val)
Definition json_sax.h:400
json_sax_dom_callback_parser & operator=(const json_sax_dom_callback_parser &)=delete
json_sax_dom_callback_parser(const json_sax_dom_callback_parser &)=delete
typename BasicJsonType::number_unsigned_t number_unsigned_t
Definition json_sax.h:348
typename BasicJsonType::binary_t binary_t
Definition json_sax.h:351
bool start_object(std::size_t len)
Definition json_sax.h:412
bool start_array(std::size_t len)
Definition json_sax.h:483
typename BasicJsonType::number_integer_t number_integer_t
Definition json_sax.h:347
bool end_array()
Definition json_sax.h:500
json_sax_dom_callback_parser(json_sax_dom_callback_parser &&)=default
bool key(string_t &val)
Definition json_sax.h:430
bool end_object()
Definition json_sax.h:447
~json_sax_dom_callback_parser()=default
typename BasicJsonType::parse_event_t parse_event_t
Definition json_sax.h:353
typename BasicJsonType::parser_callback_t parser_callback_t
Definition json_sax.h:352
bool null()
Definition json_sax.h:370
bool number_unsigned(number_unsigned_t val)
Definition json_sax.h:388
json_sax_dom_callback_parser & operator=(json_sax_dom_callback_parser &&)=default
json_sax_dom_callback_parser(BasicJsonType &r, const parser_callback_t cb, const bool allow_exceptions_=true)
Definition json_sax.h:355
typename BasicJsonType::number_float_t number_float_t
Definition json_sax.h:349
bool number_integer(number_integer_t val)
Definition json_sax.h:382
bool binary(binary_t &val)
Definition json_sax.h:406
SAX implementation to create a JSON value from SAX events.
Definition json_sax.h:162
json_sax_dom_parser(const json_sax_dom_parser &)=delete
json_sax_dom_parser & operator=(json_sax_dom_parser &&)=default
bool string(string_t &val)
Definition json_sax.h:216
json_sax_dom_parser(BasicJsonType &r, const bool allow_exceptions_=true)
Definition json_sax.h:175
bool parse_error(std::size_t, const std::string &, const Exception &ex)
Definition json_sax.h:283
bool null()
Definition json_sax.h:186
~json_sax_dom_parser()=default
typename BasicJsonType::string_t string_t
Definition json_sax.h:167
bool start_object(std::size_t len)
Definition json_sax.h:228
typename BasicJsonType::number_unsigned_t number_unsigned_t
Definition json_sax.h:165
bool number_unsigned(number_unsigned_t val)
Definition json_sax.h:204
bool number_integer(number_integer_t val)
Definition json_sax.h:198
typename BasicJsonType::number_integer_t number_integer_t
Definition json_sax.h:164
json_sax_dom_parser & operator=(const json_sax_dom_parser &)=delete
bool binary(binary_t &val)
Definition json_sax.h:222
json_sax_dom_parser(json_sax_dom_parser &&)=default
bool boolean(bool val)
Definition json_sax.h:192
bool end_array()
Definition json_sax.h:272
bool number_float(number_float_t val, const string_t &)
Definition json_sax.h:210
bool end_object()
Definition json_sax.h:250
constexpr bool is_errored() const
Definition json_sax.h:295
typename BasicJsonType::binary_t binary_t
Definition json_sax.h:168
typename BasicJsonType::number_float_t number_float_t
Definition json_sax.h:166
bool start_array(std::size_t len)
Definition json_sax.h:260
bool key(string_t &val)
Definition json_sax.h:240
#define JSON_HEDLEY_RETURNS_NON_NULL
Definition hedley.h:1729
#define JSON_HEDLEY_UNLIKELY(expr)
Definition hedley.h:1396
#define JSON_ASSERT(x)
Definition macro_scope.h:200
#define JSON_THROW(exception)
Definition macro_scope.h:171
detail namespace with internal helper functions
Definition input_adapters.h:32
OutStringType concat(Args &&... args)
Definition string_concat.h:137
@ value
the parser finished reading a JSON value
@ discarded
discarded by the parser callback function
std::function< bool(int, parse_event_t, BasicJsonType &)> parser_callback_t
Definition parser.h:51
SAX interface.
Definition json_sax.h:32
virtual bool binary(binary_t &val)=0
a binary value was read
virtual bool number_float(number_float_t val, const string_t &s)=0
a floating-point number was read
virtual bool number_unsigned(number_unsigned_t val)=0
an unsigned integer number was read
virtual bool key(string_t &val)=0
an object key was read
virtual bool string(string_t &val)=0
a string value was read
virtual bool number_integer(number_integer_t val)=0
an integer number was read
virtual bool start_object(std::size_t elements)=0
the beginning of an object was read
typename BasicJsonType::number_integer_t number_integer_t
Definition json_sax.h:33
typename BasicJsonType::string_t string_t
Definition json_sax.h:36
virtual bool end_array()=0
the end of an array was read
json_sax(const json_sax &)=default
virtual bool boolean(bool val)=0
a boolean value was read
virtual bool end_object()=0
the end of an object was read
typename BasicJsonType::number_float_t number_float_t
Definition json_sax.h:35
virtual bool null()=0
a null value was read
typename BasicJsonType::number_unsigned_t number_unsigned_t
Definition json_sax.h:34
json_sax(json_sax &&) noexcept=default
typename BasicJsonType::binary_t binary_t
Definition json_sax.h:37
virtual bool parse_error(std::size_t position, const std::string &last_token, const detail::exception &ex)=0
a parse error occurred
virtual bool start_array(std::size_t elements)=0
the beginning of an array was read