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 {
13 public:
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) {
24 Throw("field '" + std::string(key) + "'");
25 }
26 virtual void EndObject() { Throw("'}'"); }
27 virtual void StartArray() { Throw("array"); }
28 virtual void EndArray() { Throw("']'"); }
29
30 // Low-level variants of EndObject/EndArray
31 virtual void EndObject(size_t /* members */) { EndObject(); }
32 virtual void EndArray(size_t /* members */) { EndArray(); }
33
34 void SetState(ParserState& state) { parser_state_ = &state; }
35
36 virtual std::string GetPathItem() const = 0;
37
38 protected:
39 [[noreturn]] void Throw(const std::string& found) {
40 throw InternalParseError(Expected() + " was expected, but " + found +
41 " found");
42 }
43
44 virtual std::string Expected() const = 0;
45
46 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
47 ParserState* parser_state_{nullptr};
48};
49
50} // namespace formats::json::parser
51
52USERVER_NAMESPACE_END