File indexing completed on 2025-01-18 10:02:18
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <cstddef>
0012 #include <string> // string
0013 #include <utility> // move
0014 #include <vector> // vector
0015
0016 #include <nlohmann/detail/exceptions.hpp>
0017 #include <nlohmann/detail/macro_scope.hpp>
0018 #include <nlohmann/detail/string_concat.hpp>
0019
0020 NLOHMANN_JSON_NAMESPACE_BEGIN
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 template<typename BasicJsonType>
0031 struct json_sax
0032 {
0033 using number_integer_t = typename BasicJsonType::number_integer_t;
0034 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
0035 using number_float_t = typename BasicJsonType::number_float_t;
0036 using string_t = typename BasicJsonType::string_t;
0037 using binary_t = typename BasicJsonType::binary_t;
0038
0039
0040
0041
0042
0043 virtual bool null() = 0;
0044
0045
0046
0047
0048
0049
0050 virtual bool boolean(bool val) = 0;
0051
0052
0053
0054
0055
0056
0057 virtual bool number_integer(number_integer_t val) = 0;
0058
0059
0060
0061
0062
0063
0064 virtual bool number_unsigned(number_unsigned_t val) = 0;
0065
0066
0067
0068
0069
0070
0071
0072 virtual bool number_float(number_float_t val, const string_t& s) = 0;
0073
0074
0075
0076
0077
0078
0079
0080 virtual bool string(string_t& val) = 0;
0081
0082
0083
0084
0085
0086
0087
0088 virtual bool binary(binary_t& val) = 0;
0089
0090
0091
0092
0093
0094
0095
0096 virtual bool start_object(std::size_t elements) = 0;
0097
0098
0099
0100
0101
0102
0103
0104 virtual bool key(string_t& val) = 0;
0105
0106
0107
0108
0109
0110 virtual bool end_object() = 0;
0111
0112
0113
0114
0115
0116
0117
0118 virtual bool start_array(std::size_t elements) = 0;
0119
0120
0121
0122
0123
0124 virtual bool end_array() = 0;
0125
0126
0127
0128
0129
0130
0131
0132
0133 virtual bool parse_error(std::size_t position,
0134 const std::string& last_token,
0135 const detail::exception& ex) = 0;
0136
0137 json_sax() = default;
0138 json_sax(const json_sax&) = default;
0139 json_sax(json_sax&&) noexcept = default;
0140 json_sax& operator=(const json_sax&) = default;
0141 json_sax& operator=(json_sax&&) noexcept = default;
0142 virtual ~json_sax() = default;
0143 };
0144
0145
0146 namespace detail
0147 {
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 template<typename BasicJsonType>
0162 class json_sax_dom_parser
0163 {
0164 public:
0165 using number_integer_t = typename BasicJsonType::number_integer_t;
0166 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
0167 using number_float_t = typename BasicJsonType::number_float_t;
0168 using string_t = typename BasicJsonType::string_t;
0169 using binary_t = typename BasicJsonType::binary_t;
0170
0171
0172
0173
0174
0175
0176 explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
0177 : root(r), allow_exceptions(allow_exceptions_)
0178 {}
0179
0180
0181 json_sax_dom_parser(const json_sax_dom_parser&) = delete;
0182 json_sax_dom_parser(json_sax_dom_parser&&) = default;
0183 json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
0184 json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default;
0185 ~json_sax_dom_parser() = default;
0186
0187 bool null()
0188 {
0189 handle_value(nullptr);
0190 return true;
0191 }
0192
0193 bool boolean(bool val)
0194 {
0195 handle_value(val);
0196 return true;
0197 }
0198
0199 bool number_integer(number_integer_t val)
0200 {
0201 handle_value(val);
0202 return true;
0203 }
0204
0205 bool number_unsigned(number_unsigned_t val)
0206 {
0207 handle_value(val);
0208 return true;
0209 }
0210
0211 bool number_float(number_float_t val, const string_t& )
0212 {
0213 handle_value(val);
0214 return true;
0215 }
0216
0217 bool string(string_t& val)
0218 {
0219 handle_value(val);
0220 return true;
0221 }
0222
0223 bool binary(binary_t& val)
0224 {
0225 handle_value(std::move(val));
0226 return true;
0227 }
0228
0229 bool start_object(std::size_t len)
0230 {
0231 ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
0232
0233 if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
0234 {
0235 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
0236 }
0237
0238 return true;
0239 }
0240
0241 bool key(string_t& val)
0242 {
0243 JSON_ASSERT(!ref_stack.empty());
0244 JSON_ASSERT(ref_stack.back()->is_object());
0245
0246
0247 object_element = &(ref_stack.back()->m_value.object->operator[](val));
0248 return true;
0249 }
0250
0251 bool end_object()
0252 {
0253 JSON_ASSERT(!ref_stack.empty());
0254 JSON_ASSERT(ref_stack.back()->is_object());
0255
0256 ref_stack.back()->set_parents();
0257 ref_stack.pop_back();
0258 return true;
0259 }
0260
0261 bool start_array(std::size_t len)
0262 {
0263 ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
0264
0265 if (JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
0266 {
0267 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
0268 }
0269
0270 return true;
0271 }
0272
0273 bool end_array()
0274 {
0275 JSON_ASSERT(!ref_stack.empty());
0276 JSON_ASSERT(ref_stack.back()->is_array());
0277
0278 ref_stack.back()->set_parents();
0279 ref_stack.pop_back();
0280 return true;
0281 }
0282
0283 template<class Exception>
0284 bool parse_error(std::size_t , const std::string& ,
0285 const Exception& ex)
0286 {
0287 errored = true;
0288 static_cast<void>(ex);
0289 if (allow_exceptions)
0290 {
0291 JSON_THROW(ex);
0292 }
0293 return false;
0294 }
0295
0296 constexpr bool is_errored() const
0297 {
0298 return errored;
0299 }
0300
0301 private:
0302
0303
0304
0305
0306
0307
0308 template<typename Value>
0309 JSON_HEDLEY_RETURNS_NON_NULL
0310 BasicJsonType* handle_value(Value&& v)
0311 {
0312 if (ref_stack.empty())
0313 {
0314 root = BasicJsonType(std::forward<Value>(v));
0315 return &root;
0316 }
0317
0318 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
0319
0320 if (ref_stack.back()->is_array())
0321 {
0322 ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
0323 return &(ref_stack.back()->m_value.array->back());
0324 }
0325
0326 JSON_ASSERT(ref_stack.back()->is_object());
0327 JSON_ASSERT(object_element);
0328 *object_element = BasicJsonType(std::forward<Value>(v));
0329 return object_element;
0330 }
0331
0332
0333 BasicJsonType& root;
0334
0335 std::vector<BasicJsonType*> ref_stack {};
0336
0337 BasicJsonType* object_element = nullptr;
0338
0339 bool errored = false;
0340
0341 const bool allow_exceptions = true;
0342 };
0343
0344 template<typename BasicJsonType>
0345 class json_sax_dom_callback_parser
0346 {
0347 public:
0348 using number_integer_t = typename BasicJsonType::number_integer_t;
0349 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
0350 using number_float_t = typename BasicJsonType::number_float_t;
0351 using string_t = typename BasicJsonType::string_t;
0352 using binary_t = typename BasicJsonType::binary_t;
0353 using parser_callback_t = typename BasicJsonType::parser_callback_t;
0354 using parse_event_t = typename BasicJsonType::parse_event_t;
0355
0356 json_sax_dom_callback_parser(BasicJsonType& r,
0357 const parser_callback_t cb,
0358 const bool allow_exceptions_ = true)
0359 : root(r), callback(cb), allow_exceptions(allow_exceptions_)
0360 {
0361 keep_stack.push_back(true);
0362 }
0363
0364
0365 json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
0366 json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default;
0367 json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
0368 json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default;
0369 ~json_sax_dom_callback_parser() = default;
0370
0371 bool null()
0372 {
0373 handle_value(nullptr);
0374 return true;
0375 }
0376
0377 bool boolean(bool val)
0378 {
0379 handle_value(val);
0380 return true;
0381 }
0382
0383 bool number_integer(number_integer_t val)
0384 {
0385 handle_value(val);
0386 return true;
0387 }
0388
0389 bool number_unsigned(number_unsigned_t val)
0390 {
0391 handle_value(val);
0392 return true;
0393 }
0394
0395 bool number_float(number_float_t val, const string_t& )
0396 {
0397 handle_value(val);
0398 return true;
0399 }
0400
0401 bool string(string_t& val)
0402 {
0403 handle_value(val);
0404 return true;
0405 }
0406
0407 bool binary(binary_t& val)
0408 {
0409 handle_value(std::move(val));
0410 return true;
0411 }
0412
0413 bool start_object(std::size_t len)
0414 {
0415
0416 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
0417 keep_stack.push_back(keep);
0418
0419 auto val = handle_value(BasicJsonType::value_t::object, true);
0420 ref_stack.push_back(val.second);
0421
0422
0423 if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
0424 {
0425 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
0426 }
0427
0428 return true;
0429 }
0430
0431 bool key(string_t& val)
0432 {
0433 BasicJsonType k = BasicJsonType(val);
0434
0435
0436 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
0437 key_keep_stack.push_back(keep);
0438
0439
0440 if (keep && ref_stack.back())
0441 {
0442 object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
0443 }
0444
0445 return true;
0446 }
0447
0448 bool end_object()
0449 {
0450 if (ref_stack.back())
0451 {
0452 if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
0453 {
0454
0455 *ref_stack.back() = discarded;
0456 }
0457 else
0458 {
0459 ref_stack.back()->set_parents();
0460 }
0461 }
0462
0463 JSON_ASSERT(!ref_stack.empty());
0464 JSON_ASSERT(!keep_stack.empty());
0465 ref_stack.pop_back();
0466 keep_stack.pop_back();
0467
0468 if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
0469 {
0470
0471 for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
0472 {
0473 if (it->is_discarded())
0474 {
0475 ref_stack.back()->erase(it);
0476 break;
0477 }
0478 }
0479 }
0480
0481 return true;
0482 }
0483
0484 bool start_array(std::size_t len)
0485 {
0486 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
0487 keep_stack.push_back(keep);
0488
0489 auto val = handle_value(BasicJsonType::value_t::array, true);
0490 ref_stack.push_back(val.second);
0491
0492
0493 if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != static_cast<std::size_t>(-1) && len > ref_stack.back()->max_size()))
0494 {
0495 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
0496 }
0497
0498 return true;
0499 }
0500
0501 bool end_array()
0502 {
0503 bool keep = true;
0504
0505 if (ref_stack.back())
0506 {
0507 keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
0508 if (keep)
0509 {
0510 ref_stack.back()->set_parents();
0511 }
0512 else
0513 {
0514
0515 *ref_stack.back() = discarded;
0516 }
0517 }
0518
0519 JSON_ASSERT(!ref_stack.empty());
0520 JSON_ASSERT(!keep_stack.empty());
0521 ref_stack.pop_back();
0522 keep_stack.pop_back();
0523
0524
0525 if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
0526 {
0527 ref_stack.back()->m_value.array->pop_back();
0528 }
0529
0530 return true;
0531 }
0532
0533 template<class Exception>
0534 bool parse_error(std::size_t , const std::string& ,
0535 const Exception& ex)
0536 {
0537 errored = true;
0538 static_cast<void>(ex);
0539 if (allow_exceptions)
0540 {
0541 JSON_THROW(ex);
0542 }
0543 return false;
0544 }
0545
0546 constexpr bool is_errored() const
0547 {
0548 return errored;
0549 }
0550
0551 private:
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567 template<typename Value>
0568 std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
0569 {
0570 JSON_ASSERT(!keep_stack.empty());
0571
0572
0573
0574 if (!keep_stack.back())
0575 {
0576 return {false, nullptr};
0577 }
0578
0579
0580 auto value = BasicJsonType(std::forward<Value>(v));
0581
0582
0583 const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
0584
0585
0586 if (!keep)
0587 {
0588 return {false, nullptr};
0589 }
0590
0591 if (ref_stack.empty())
0592 {
0593 root = std::move(value);
0594 return {true, &root};
0595 }
0596
0597
0598
0599 if (!ref_stack.back())
0600 {
0601 return {false, nullptr};
0602 }
0603
0604
0605 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
0606
0607
0608 if (ref_stack.back()->is_array())
0609 {
0610 ref_stack.back()->m_value.array->emplace_back(std::move(value));
0611 return {true, &(ref_stack.back()->m_value.array->back())};
0612 }
0613
0614
0615 JSON_ASSERT(ref_stack.back()->is_object());
0616
0617 JSON_ASSERT(!key_keep_stack.empty());
0618 const bool store_element = key_keep_stack.back();
0619 key_keep_stack.pop_back();
0620
0621 if (!store_element)
0622 {
0623 return {false, nullptr};
0624 }
0625
0626 JSON_ASSERT(object_element);
0627 *object_element = std::move(value);
0628 return {true, object_element};
0629 }
0630
0631
0632 BasicJsonType& root;
0633
0634 std::vector<BasicJsonType*> ref_stack {};
0635
0636 std::vector<bool> keep_stack {};
0637
0638 std::vector<bool> key_keep_stack {};
0639
0640 BasicJsonType* object_element = nullptr;
0641
0642 bool errored = false;
0643
0644 const parser_callback_t callback = nullptr;
0645
0646 const bool allow_exceptions = true;
0647
0648 BasicJsonType discarded = BasicJsonType::value_t::discarded;
0649 };
0650
0651 template<typename BasicJsonType>
0652 class json_sax_acceptor
0653 {
0654 public:
0655 using number_integer_t = typename BasicJsonType::number_integer_t;
0656 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
0657 using number_float_t = typename BasicJsonType::number_float_t;
0658 using string_t = typename BasicJsonType::string_t;
0659 using binary_t = typename BasicJsonType::binary_t;
0660
0661 bool null()
0662 {
0663 return true;
0664 }
0665
0666 bool boolean(bool )
0667 {
0668 return true;
0669 }
0670
0671 bool number_integer(number_integer_t )
0672 {
0673 return true;
0674 }
0675
0676 bool number_unsigned(number_unsigned_t )
0677 {
0678 return true;
0679 }
0680
0681 bool number_float(number_float_t , const string_t& )
0682 {
0683 return true;
0684 }
0685
0686 bool string(string_t& )
0687 {
0688 return true;
0689 }
0690
0691 bool binary(binary_t& )
0692 {
0693 return true;
0694 }
0695
0696 bool start_object(std::size_t = static_cast<std::size_t>(-1))
0697 {
0698 return true;
0699 }
0700
0701 bool key(string_t& )
0702 {
0703 return true;
0704 }
0705
0706 bool end_object()
0707 {
0708 return true;
0709 }
0710
0711 bool start_array(std::size_t = static_cast<std::size_t>(-1))
0712 {
0713 return true;
0714 }
0715
0716 bool end_array()
0717 {
0718 return true;
0719 }
0720
0721 bool parse_error(std::size_t , const std::string& , const detail::exception& )
0722 {
0723 return false;
0724 }
0725 };
0726
0727 }
0728 NLOHMANN_JSON_NAMESPACE_END