3#include <userver/formats/common/path.hpp>
4#include <userver/formats/json/parser/typed_parser.hpp>
5#include <userver/utils/meta.hpp>
12template <
typename Item,
typename ItemParser,
typename Array = std::vector<Item>>
15 explicit ArrayParser(ItemParser& item_parser) : item_parser_(item_parser) {
this->item_parser_.Subscribe(*
this); }
17 void Reset()
override {
19 state_ = State::kStart;
22 if constexpr (meta::kIsVector<Array>) {
24
25
26
27
28
29
35 void StartArray()
override {
36 if (state_ == State::kStart) {
37 state_ = State::kInside;
40 Parser().StartArray();
43 void EndArray()
override {
44 if (state_ == State::kInside) {
45 this->SetResult(std::move(storage_));
49 this->Throw(
"end of array");
52 void Int64(int64_t i)
override {
53 PushParser(
"integer");
56 void Uint64(uint64_t i)
override {
57 PushParser(
"integer");
60 void Null()
override {
64 void Bool(
bool b)
override {
68 void Double(
double d)
override {
72 void String(std::string_view sw)
override {
76 void StartObject()
override {
78 Parser().StartObject();
81 std::string Expected()
const override {
return "array"; }
83 void PushParser(std::string_view what) {
84 if (state_ != State::kInside) {
86 this->parser_state_->PopMe(*
this);
88 this->Throw(std::string(what));
91 this->item_parser_.Reset();
92 this->parser_state_->PushParser(item_parser_.GetParser());
96 void OnSend(Item&& item)
override {
97 if constexpr (!meta::kIsVector<Array>) {
98 this->storage_.insert(std::move(item));
100 this->storage_.push_back(std::move(item));
104 std::string GetPathItem()
const override {
return common::GetIndexString(index_ - 1); }
106 BaseParser& Parser() {
return item_parser_.GetParser(); }
109 ItemParser& item_parser_;
110 std::optional<size_t> min_items_, max_items_;
117 State state_{State::kStart};