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///
13public:
14 virtual ~BaseParser() = default;
15
16 BaseParser() = default;
17 BaseParser(BaseParser&&) = delete;
18 BaseParser(const BaseParser&) = delete;
19 BaseParser& operator=(const BaseParser&) = delete;
20 BaseParser& operator=(BaseParser&&) = delete;
21
22 virtual void Null() { Throw("null"); }
23 virtual void Bool(bool) { Throw("bool"); }
24 virtual void Int64(int64_t) { Throw("integer"); }
25 virtual void Uint64(uint64_t) { Throw("integer"); }
26 virtual void Double(double) { Throw("double"); }
27 virtual void String(std::string_view) { Throw("string"); }
28 virtual void StartObject() { Throw("object"); }
29 virtual void Key(std::string_view key) { Throw("field '" + std::string(key) + "'"); }
30 virtual void EndObject() { Throw("'}'"); }
31 virtual void StartArray() { Throw("array"); }
32 virtual void EndArray() { Throw("']'"); }
33
34 // Low-level variants of EndObject/EndArray
35 virtual void EndObject(size_t /* members */) { EndObject(); }
36 virtual void EndArray(size_t /* members */) { EndArray(); }
37
38 void SetState(ParserState& state) { parser_state_ = &state; }
39
40 virtual std::string GetPathItem() const = 0;
41
42 std::string GetCurrentPath() const { return parser_state_->GetCurrentPath(); }
43
44protected:
45 [[noreturn]] void Throw(const std::string& found) {
46 throw InternalParseError(Expected() + " was expected, but " + found + " found");
47 }
48
49 virtual std::string Expected() const = 0;
50
51 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
52 ParserState* parser_state_{nullptr};
53};
54
55} // namespace formats::json::parser
56
57USERVER_NAMESPACE_END