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,
13 typename Array = std::vector<Item>>
16 explicit ArrayParser(ItemParser& item_parser) : item_parser_(item_parser) {
17 this->item_parser_.Subscribe(*
this);
20 void Reset()
override {
22 state_ = State::kStart;
25 if constexpr (meta::kIsVector<Array>) {
27
28
29
30
31
32
38 void StartArray()
override {
39 if (state_ == State::kStart) {
40 state_ = State::kInside;
43 Parser().StartArray();
46 void EndArray()
override {
47 if (state_ == State::kInside) {
48 this->SetResult(std::move(storage_));
52 this->Throw(
"end of array");
55 void Int64(int64_t i)
override {
56 PushParser(
"integer");
59 void Uint64(uint64_t i)
override {
60 PushParser(
"integer");
63 void Null()
override {
67 void Bool(
bool b)
override {
71 void Double(
double d)
override {
75 void String(std::string_view sw)
override {
79 void StartObject()
override {
81 Parser().StartObject();
84 std::string Expected()
const override {
return "array"; }
86 void PushParser(std::string_view what) {
87 if (state_ != State::kInside) {
89 this->parser_state_->PopMe(*
this);
91 this->Throw(std::string(what));
94 this->item_parser_.Reset();
95 this->parser_state_->PushParser(item_parser_.GetParser());
99 void OnSend(Item&& item)
override {
100 if constexpr (!meta::kIsVector<Array>) {
101 this->storage_.insert(std::move(item));
103 this->storage_.push_back(std::move(item));
107 std::string GetPathItem()
const override {
108 return common::GetIndexString(index_ - 1);
111 BaseParser& Parser() {
return item_parser_.GetParser(); }
114 ItemParser& item_parser_;
115 std::optional<size_t> min_items_, max_items_;
122 State state_{State::kStart};