-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_builder.h
113 lines (85 loc) · 2.53 KB
/
json_builder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
#include "json.h"
#include <functional>
#include <memory>
#include <stack>
namespace json {
/*
The main idea is to save directives and values in std::vector, marking
directives by IS_MARKER and Values by !IS_MARKER
until EndDict() or EndArray() is reached.
If they are reached search previously saved marked directive in the array
and create Node from intermeadiate values and save them to the
previously marked directive place.
*/
inline bool IS_MARKER = true;
using u_ptr_Node = std::unique_ptr<json::Node>;
struct SNode {
u_ptr_Node u_ptr;
bool is_marker;
};
class Builder;
class StartX;
class KeyContext;
class StartArrayContext;
class DictItemContext;
class DictItemContext {
public:
explicit DictItemContext(Builder& builder)
: builder_(builder){};
KeyContext& Key(const std::string& key);
Builder& EndDict();
private:
Builder& builder_;
};
class StartX {
public:
explicit StartX(Builder& builder)
: builder_(builder){};
DictItemContext& StartDict();
StartArrayContext& StartArray();
private:
Builder& builder_;
};
class KeyContext : public StartX {
public:
explicit KeyContext(Builder& builder)
: StartX(builder)
, builder_(builder){};
DictItemContext& Value(const Node::Value& value);
private:
Builder& builder_;
};
class StartArrayContext : public StartX {
public:
explicit StartArrayContext(Builder& builder)
: StartX(builder)
, builder_(builder){};
StartArrayContext& Value(const Node::Value& value);
Builder& EndArray();
private:
Builder& builder_;
};
class Builder : public DictItemContext, KeyContext, StartArrayContext {
public:
friend StartArrayContext;
Builder()
: DictItemContext(*this)
, KeyContext(*this)
, StartArrayContext(*this) {
}
DictItemContext& StartDict();
StartArrayContext& StartArray();
Builder& EndDict();
Builder& EndArray();
KeyContext& Key(const std::string& key);
Builder& Value(const Node::Value& value);
Node& Build();
private:
void CreateNode(const Node::Value& value, bool is_marker);
void CheckIfOblectCompleated();
private:
Node root_ = nullptr;
std::vector<SNode> m_nodes_stack_;
};
} // namespace json