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,
161template<
typename BasicJsonType>
177 : root(r), allow_exceptions(allow_exceptions_)
189 handle_value(
nullptr);
225 handle_value(std::move(val));
231 ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
233 if (
JSON_HEDLEY_UNLIKELY(len !=
static_cast<std::size_t
>(-1) && len > ref_stack.back()->max_size()))
247 object_element = &(ref_stack.back()->m_value.object->operator[](val));
256 ref_stack.back()->set_parents();
257 ref_stack.pop_back();
265 if (
JSON_HEDLEY_UNLIKELY(len !=
static_cast<std::size_t
>(-1) && len > ref_stack.back()->max_size()))
278 ref_stack.back()->set_parents();
279 ref_stack.pop_back();
283 template<
class Exception>
288 static_cast<void>(ex);
289 if (allow_exceptions)
308 template<
typename Value>
310 BasicJsonType* handle_value(Value&& v)
312 if (ref_stack.empty())
314 root = BasicJsonType(std::forward<Value>(v));
318 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
320 if (ref_stack.back()->is_array())
322 ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
323 return &(ref_stack.back()->m_value.array->back());
328 *object_element = BasicJsonType(std::forward<Value>(v));
329 return object_element;
335 std::vector<BasicJsonType*> ref_stack {};
337 BasicJsonType* object_element =
nullptr;
339 bool errored =
false;
341 const bool allow_exceptions =
true;
344template<
typename BasicJsonType>
358 const bool allow_exceptions_ =
true)
359 : root(r), callback(cb), allow_exceptions(allow_exceptions_)
361 keep_stack.push_back(
true);
373 handle_value(
nullptr);
409 handle_value(std::move(val));
416 const bool keep = callback(
static_cast<int>(ref_stack.size()), parse_event_t::object_start,
discarded);
417 keep_stack.push_back(keep);
419 auto val = handle_value(BasicJsonType::value_t::object,
true);
420 ref_stack.push_back(val.second);
423 if (ref_stack.back() &&
JSON_HEDLEY_UNLIKELY(len !=
static_cast<std::size_t
>(-1) && len > ref_stack.back()->max_size()))
433 BasicJsonType k = BasicJsonType(val);
436 const bool keep = callback(
static_cast<int>(ref_stack.size()), parse_event_t::key, k);
437 key_keep_stack.push_back(keep);
440 if (keep && ref_stack.back())
442 object_element = &(ref_stack.back()->m_value.object->operator[](val) =
discarded);
450 if (ref_stack.back())
452 if (!callback(
static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
459 ref_stack.back()->set_parents();
465 ref_stack.pop_back();
466 keep_stack.pop_back();
468 if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
471 for (
auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
473 if (it->is_discarded())
475 ref_stack.back()->erase(it);
486 const bool keep = callback(
static_cast<int>(ref_stack.size()), parse_event_t::array_start,
discarded);
487 keep_stack.push_back(keep);
490 ref_stack.push_back(val.second);
493 if (ref_stack.back() &&
JSON_HEDLEY_UNLIKELY(len !=
static_cast<std::size_t
>(-1) && len > ref_stack.back()->max_size()))
505 if (ref_stack.back())
507 keep = callback(
static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
510 ref_stack.back()->set_parents();
521 ref_stack.pop_back();
522 keep_stack.pop_back();
525 if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
527 ref_stack.back()->m_value.array->pop_back();
533 template<
class Exception>
538 static_cast<void>(ex);
539 if (allow_exceptions)
567 template<
typename Value>
568 std::pair<bool, BasicJsonType*> handle_value(Value&& v,
const bool skip_callback =
false)
574 if (!keep_stack.back())
576 return {
false,
nullptr};
580 auto value = BasicJsonType(std::forward<Value>(v));
583 const bool keep = skip_callback || callback(
static_cast<int>(ref_stack.size()), parse_event_t::value, value);
588 return {
false,
nullptr};
591 if (ref_stack.empty())
593 root = std::move(value);
594 return {
true, &root};
599 if (!ref_stack.back())
601 return {
false,
nullptr};
605 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
608 if (ref_stack.back()->is_array())
610 ref_stack.back()->m_value.array->emplace_back(std::move(value));
611 return {
true, &(ref_stack.back()->m_value.array->back())};
618 const bool store_element = key_keep_stack.back();
619 key_keep_stack.pop_back();
623 return {
false,
nullptr};
627 *object_element = std::move(value);
628 return {
true, object_element};
634 std::vector<BasicJsonType*> ref_stack {};
636 std::vector<bool> keep_stack {};
638 std::vector<bool> key_keep_stack {};
640 BasicJsonType* object_element =
nullptr;
642 bool errored =
false;
646 const bool allow_exceptions =
true;
648 BasicJsonType
discarded = BasicJsonType::value_t::discarded;
651template<
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:37
Definition: json_sax.h:653
typename BasicJsonType::number_unsigned_t number_unsigned_t
Definition: json_sax.h:656
bool end_object()
Definition: json_sax.h:706
bool start_object(std::size_t=static_cast< std::size_t >(-1))
Definition: json_sax.h:696
bool binary(binary_t &)
Definition: json_sax.h:691
bool number_integer(number_integer_t)
Definition: json_sax.h:671
bool start_array(std::size_t=static_cast< std::size_t >(-1))
Definition: json_sax.h:711
bool boolean(bool)
Definition: json_sax.h:666
bool null()
Definition: json_sax.h:661
bool end_array()
Definition: json_sax.h:716
bool number_unsigned(number_unsigned_t)
Definition: json_sax.h:676
bool string(string_t &)
Definition: json_sax.h:686
typename BasicJsonType::binary_t binary_t
Definition: json_sax.h:659
bool number_float(number_float_t, const string_t &)
Definition: json_sax.h:681
bool parse_error(std::size_t, const std::string &, const detail::exception &)
Definition: json_sax.h:721
bool key(string_t &)
Definition: json_sax.h:701
typename BasicJsonType::number_integer_t number_integer_t
Definition: json_sax.h:655
typename BasicJsonType::number_float_t number_float_t
Definition: json_sax.h:657
typename BasicJsonType::string_t string_t
Definition: json_sax.h:658
Definition: json_sax.h:346
bool boolean(bool val)
Definition: json_sax.h:377
bool parse_error(std::size_t, const std::string &, const Exception &ex)
Definition: json_sax.h:534
typename BasicJsonType::string_t string_t
Definition: json_sax.h:351
bool number_float(number_float_t val, const string_t &)
Definition: json_sax.h:395
constexpr bool is_errored() const
Definition: json_sax.h:546
bool string(string_t &val)
Definition: json_sax.h:401
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:349
typename BasicJsonType::binary_t binary_t
Definition: json_sax.h:352
bool start_object(std::size_t len)
Definition: json_sax.h:413
bool start_array(std::size_t len)
Definition: json_sax.h:484
typename BasicJsonType::number_integer_t number_integer_t
Definition: json_sax.h:348
bool end_array()
Definition: json_sax.h:501
json_sax_dom_callback_parser(json_sax_dom_callback_parser &&)=default
bool key(string_t &val)
Definition: json_sax.h:431
bool end_object()
Definition: json_sax.h:448
~json_sax_dom_callback_parser()=default
typename BasicJsonType::parse_event_t parse_event_t
Definition: json_sax.h:354
typename BasicJsonType::parser_callback_t parser_callback_t
Definition: json_sax.h:353
bool null()
Definition: json_sax.h:371
bool number_unsigned(number_unsigned_t val)
Definition: json_sax.h:389
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:356
typename BasicJsonType::number_float_t number_float_t
Definition: json_sax.h:350
bool number_integer(number_integer_t val)
Definition: json_sax.h:383
bool binary(binary_t &val)
Definition: json_sax.h:407
SAX implementation to create a JSON value from SAX events.
Definition: json_sax.h:163
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:217
json_sax_dom_parser(BasicJsonType &r, const bool allow_exceptions_=true)
Definition: json_sax.h:176
bool parse_error(std::size_t, const std::string &, const Exception &ex)
Definition: json_sax.h:284
bool null()
Definition: json_sax.h:187
~json_sax_dom_parser()=default
typename BasicJsonType::string_t string_t
Definition: json_sax.h:168
bool start_object(std::size_t len)
Definition: json_sax.h:229
typename BasicJsonType::number_unsigned_t number_unsigned_t
Definition: json_sax.h:166
bool number_unsigned(number_unsigned_t val)
Definition: json_sax.h:205
bool number_integer(number_integer_t val)
Definition: json_sax.h:199
typename BasicJsonType::number_integer_t number_integer_t
Definition: json_sax.h:165
json_sax_dom_parser & operator=(const json_sax_dom_parser &)=delete
bool binary(binary_t &val)
Definition: json_sax.h:223
json_sax_dom_parser(json_sax_dom_parser &&)=default
bool boolean(bool val)
Definition: json_sax.h:193
bool end_array()
Definition: json_sax.h:273
bool number_float(number_float_t val, const string_t &)
Definition: json_sax.h:211
bool end_object()
Definition: json_sax.h:251
constexpr bool is_errored() const
Definition: json_sax.h:296
typename BasicJsonType::binary_t binary_t
Definition: json_sax.h:169
typename BasicJsonType::number_float_t number_float_t
Definition: json_sax.h:167
bool start_array(std::size_t len)
Definition: json_sax.h:261
bool key(string_t &val)
Definition: json_sax.h:241
#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:192
#define JSON_THROW(exception)
Definition: macro_scope.h:163
detail namespace with internal helper functions
Definition: xchar.h:20
std::function< bool(int, parse_event_t, BasicJsonType &)> parser_callback_t
Definition: parser.h:52
OutStringType concat(Args &&... args)
Definition: string_concat.h:137
parse_event_t
Definition: parser.h:35
@ value
the parser finished reading a JSON value
@ discarded
discarded by the parser callback function
std::string to_string(const T &t)
Definition: base.h:92
array(T, Ts...) -> array< T, 1+sizeof...(Ts)>
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