5#include <userver/formats/json/parser/base_parser.hpp>
6#include <userver/utils/assert.hpp>
15 virtual ~Subscriber() =
default;
17 virtual void OnSend(T&&) = 0;
21class SubscriberSink
final :
public Subscriber<T> {
23 SubscriberSink(T& data) : data_(data) {}
25 void OnSend(T&& value)
override { data_ = std::move(value); }
34 SubscriberSinkOptional(std::optional<T>& data) : data_(data) {}
36 void OnSend(T&& value)
override { data_ = std::move(value); }
38 void OnSend(std::optional<T>&& value)
override { data_ = std::move(value); }
41 std::optional<T>& data_;
131 void Subscribe(
Subscriber<T>& subscriber) { subscriber_ = &subscriber; }
133 using ResultType = T;
139 virtual void Reset() {}
147 void SetResult(T&& value) {
148 parser_state_->PopMe(*
this);
149 if (subscriber_) subscriber_->OnSend(std::move(value));
158template <
typename Parser>
159typename Parser::ResultType ParseSingle(Parser& parser, std::string_view input) {
160 using ResultType =
typename Parser::ResultType;
164 SubscriberSink<ResultType> sink(result);
165 parser.Subscribe(sink);
168 state.PushParser(parser);
169 state.ProcessInput(input);
176template <
typename T,
typename Parser>
177T ParseToType(std::string_view input) {
179 return impl::ParseSingle(parser, input);