userver: userver/formats/json/parser/base_parser.hpp Source File
Loading...
Searching...
No Matches
base_parser.hpp
1#pragma once
2
3#include <userver/formats/json/parser/exception.hpp>
4#include <userver/formats/json/parser/parser_state.hpp>
5
6USERVER_NAMESPACE_BEGIN
7
8namespace formats::json::parser {
9
10/// @brief Base class for SAX parser.
11///
12class BaseParser {
13public:
14 virtual ~BaseParser() = default;
15
16 virtual void Null() { Throw("null"); }
17 virtual void Bool(bool) { Throw("bool"); }
18 virtual void Int64(int64_t) { Throw("integer"); }
19 virtual void Uint64(uint64_t) { Throw("integer"); }
20 virtual void Double(double) { Throw("double"); }
21 virtual void String(std::string_view) { Throw("string"); }
22 virtual void StartObject() { Throw("object"); }
23 virtual void Key(std::string_view key) { Throw("field '" + std::string(key) + "'"); }
24 virtual void EndObject() { Throw("'}'"); }
25 virtual void StartArray() { Throw("array"); }
26 virtual void EndArray() { Throw("']'"); }
27
28 // Low-level variants of EndObject/EndArray
29 virtual void EndObject(size_t /* members */) { EndObject(); }
30 virtual void EndArray(size_t /* members */) { EndArray(); }
31
32 void SetState(ParserState& state) { parser_state_ = &state; }
33
34 virtual std::string GetPathItem() const = 0;
35
36protected:
37 [[noreturn]] void Throw(const std::string& found) {
38 throw InternalParseError(Expected() + " was expected, but " + found + " found");
39 }
40
41 virtual std::string Expected() const = 0;
42
43 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
44 ParserState* parser_state_{nullptr};
45};
46
47} // namespace formats::json::parser
48
49USERVER_NAMESPACE_END