userver: formats::json::parser::TypedParser< T > Class Template Reference
Loading...
Searching...
No Matches
formats::json::parser::TypedParser< T > Class Template Referenceabstract

#include <userver/formats/json/parser/typed_parser.hpp>

Detailed Description

template<typename T>
class formats::json::parser::TypedParser< T >

Main base class for SAX parsers.

There are two main groups of SAX parser classes:

  • typed parser
  • proxy parser

TypedParser derivative is a parser that handles JSON tokens by itself. It implements methods of BaseParser for handling specific tokens (e.g. Null(), StartArray(), Double()). Usually parser implements only 1-2 methods of BaseParser for handling a fixed set of JSON tokens and leaves default implementation for the rest (iow, treat other JSON tokens as a parse error).

Parser usually maintains its state as a field(s). Parse methods are called when a specific input token is read, and implementation updates the parser state. When finished, the parser calls SetResult() with the cooked value. It pops current parser from the parser stack and signals the subscriber with the result.

TypedParser may delegate part of its job to subparsers. It is very common to define a parser for an object/array and reuse field parsers for JSON object fields parsing. A subparser is usually implemented as a field of a parser class. When such parser wants to start subobject parsing, it pushes the subparser onto the stack (and maybe calls some of its parse methods).

E.g.:

void SomeParser::Double(double d) {
subparser_->Reset();
parser_state_->PushParser(subparser_.GetParser());
subparser_->Double(d);
}

You may also implement a proxy parser. It derives neither from TypedParser nor any other userver's parser class. A proxy parser is a class that delegates the whole job of input token handling to subparser(s), but somehow mutates the result (e.g. converts or validates it) - proxies the result. It doesn't implement any JSON token handling methods by itself. Usually proxy parser stores a subparser as a field and maybe stores some housekeeping settings for result handling.

Duck typing is used in proxy parsers to negate virtual methods overhead. A proxy parser must implement the following methods:

class ProxyParser final
: public formats::json::parser::Subscriber<Subparser::ResultType> {
public:
// Define result type that will be passed to OnSend() of a subscriber
using ResultType = Result;
ProxyParser() {
// Proxy parser wants to be called when the subparser
// signals with the result
subparser.Subscribe(*this);
}
// Reset() of proxy parser MUST call Reset() of subparsers in contrast to
// a typed parser as a proxy parser doesn't control pushing of subparser
// onto the stack.
void Reset() {
subparser_.Reset();
}
void Subscribe(formats::json::parser::Subscriber<Result>& subscriber) {
subscriber_ = &subscriber;
}
// The core method of proxy parser. It converts/filters the result value
// and signals the (maybe mutated) result further to its subscriber.
void OnSend(Subparser::ResultType&& result) override {
if (subscriber_) subscriber_->OnSend(Result(std::move(result)));
}
// Returns a typed parser that is responsible for actual JSON parsing.
auto& GetParser() { return subparser_.GetParser(); }
private:
Subparser subparser_;
Subscriber* subscriber_{nullptr};
}

Definition at line 137 of file typed_parser.hpp.

Inheritance diagram for formats::json::parser::TypedParser< T >:

Public Types

using ResultType = T

Public Member Functions

void Subscribe (Subscriber< T > &subscriber)
virtual void Reset ()
TypedParser< T > & GetParser ()
virtual void Null ()
virtual void Bool (bool)
virtual void Int64 (int64_t)
virtual void Uint64 (uint64_t)
virtual void Double (double)
virtual void String (std::string_view)
virtual void StartObject ()
virtual void Key (std::string_view key)
virtual void EndObject ()
virtual void EndObject (size_t)
virtual void StartArray ()
virtual void EndArray ()
virtual void EndArray (size_t)
void SetState (ParserState &state)
virtual std::string GetPathItem () const =0
std::string GetCurrentPath () const

Protected Member Functions

void SetResult (T &&value)
void Throw (const std::string &found)
virtual std::string Expected () const =0

Protected Attributes

ParserStateparser_state_ {nullptr}

Member Typedef Documentation

◆ ResultType

template<typename T>
using formats::json::parser::TypedParser< T >::ResultType = T

Definition at line 141 of file typed_parser.hpp.

Member Function Documentation

◆ EndArray()

virtual void formats::json::parser::BaseParser::EndArray ( size_t )
inlinevirtualinherited

Definition at line 40 of file base_parser.hpp.

◆ EndObject()

virtual void formats::json::parser::BaseParser::EndObject ( size_t )
inlinevirtualinherited

Definition at line 39 of file base_parser.hpp.

◆ GetCurrentPath()

std::string formats::json::parser::BaseParser::GetCurrentPath ( ) const
inlineinherited

Definition at line 46 of file base_parser.hpp.

◆ GetParser()

template<typename T>
TypedParser< T > & formats::json::parser::TypedParser< T >::GetParser ( )
inline

Returns an actual parser. It is commonly used in PushParser() to identify typed parser of a proxy parser.

Definition at line 152 of file typed_parser.hpp.

◆ Reset()

template<typename T>
virtual void formats::json::parser::TypedParser< T >::Reset ( )
inlinevirtual

Resets parser's internal state. It should not call Reset() of subparsers (if any). Subparsers' Reset() should be called just before pushing it onto the stack.

Reimplemented in formats::json::parser::ArrayParser< Item, ItemParser, Array >, and formats::json::parser::MapParser< Map, ValueParser >.

Definition at line 147 of file typed_parser.hpp.

◆ SetResult()

template<typename T>
void formats::json::parser::TypedParser< T >::SetResult ( T && value)
inlineprotected

Definition at line 155 of file typed_parser.hpp.

◆ SetState()

void formats::json::parser::BaseParser::SetState ( ParserState & state)
inlineinherited

Definition at line 42 of file base_parser.hpp.

◆ Subscribe()

template<typename T>
void formats::json::parser::TypedParser< T >::Subscribe ( Subscriber< T > & subscriber)
inline

Definition at line 139 of file typed_parser.hpp.

Member Data Documentation

◆ parser_state_

ParserState* formats::json::parser::BaseParser::parser_state_ {nullptr}
protectedinherited

Definition at line 54 of file base_parser.hpp.


The documentation for this class was generated from the following file: