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:

: 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 129 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
 

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 133 of file typed_parser.hpp.

Member Function Documentation

◆ Bool()

virtual void formats::json::parser::BaseParser::Bool ( bool )
inlinevirtualinherited

Definition at line 17 of file base_parser.hpp.

◆ Double()

virtual void formats::json::parser::BaseParser::Double ( double )
inlinevirtualinherited

Definition at line 20 of file base_parser.hpp.

◆ EndArray() [1/2]

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

Definition at line 26 of file base_parser.hpp.

◆ EndArray() [2/2]

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

Definition at line 30 of file base_parser.hpp.

◆ EndObject() [1/2]

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

Definition at line 24 of file base_parser.hpp.

◆ EndObject() [2/2]

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

Definition at line 29 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 144 of file typed_parser.hpp.

◆ Int64()

virtual void formats::json::parser::BaseParser::Int64 ( int64_t )
inlinevirtualinherited

Definition at line 18 of file base_parser.hpp.

◆ Key()

virtual void formats::json::parser::BaseParser::Key ( std::string_view key)
inlinevirtualinherited

Definition at line 23 of file base_parser.hpp.

◆ Null()

virtual void formats::json::parser::BaseParser::Null ( )
inlinevirtualinherited

Definition at line 16 of file base_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 139 of file typed_parser.hpp.

◆ SetResult()

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

Definition at line 147 of file typed_parser.hpp.

◆ SetState()

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

Definition at line 32 of file base_parser.hpp.

◆ StartArray()

virtual void formats::json::parser::BaseParser::StartArray ( )
inlinevirtualinherited

Definition at line 25 of file base_parser.hpp.

◆ StartObject()

virtual void formats::json::parser::BaseParser::StartObject ( )
inlinevirtualinherited

Definition at line 22 of file base_parser.hpp.

◆ String()

virtual void formats::json::parser::BaseParser::String ( std::string_view )
inlinevirtualinherited

Definition at line 21 of file base_parser.hpp.

◆ Subscribe()

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

Definition at line 131 of file typed_parser.hpp.

◆ Throw()

void formats::json::parser::BaseParser::Throw ( const std::string & found)
inlineprotectedinherited

Definition at line 37 of file base_parser.hpp.

◆ Uint64()

virtual void formats::json::parser::BaseParser::Uint64 ( uint64_t )
inlinevirtualinherited

Definition at line 19 of file base_parser.hpp.

Member Data Documentation

◆ parser_state_

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

Definition at line 44 of file base_parser.hpp.


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