#include <userver/formats/json/parser/typed_parser.hpp>
Main base class for SAX parsers.
There are two main groups of SAX parser classes:
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.:
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:
Definition at line 129 of file typed_parser.hpp.
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 | |
ParserState * | parser_state_ {nullptr} |
using formats::json::parser::TypedParser< T >::ResultType = T |
Definition at line 133 of file typed_parser.hpp.
Definition at line 17 of file base_parser.hpp.
Definition at line 20 of file base_parser.hpp.
Definition at line 26 of file base_parser.hpp.
Definition at line 30 of file base_parser.hpp.
Definition at line 24 of file base_parser.hpp.
Definition at line 29 of file base_parser.hpp.
|
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.
Definition at line 18 of file base_parser.hpp.
Definition at line 23 of file base_parser.hpp.
Definition at line 16 of file base_parser.hpp.
|
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.
|
inlineprotected |
Definition at line 147 of file typed_parser.hpp.
|
inlineinherited |
Definition at line 32 of file base_parser.hpp.
Definition at line 25 of file base_parser.hpp.
Definition at line 22 of file base_parser.hpp.
Definition at line 21 of file base_parser.hpp.
|
inline |
Definition at line 131 of file typed_parser.hpp.
Definition at line 37 of file base_parser.hpp.
Definition at line 19 of file base_parser.hpp.
|
protectedinherited |
Definition at line 44 of file base_parser.hpp.