-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_builder.cpp
140 lines (115 loc) · 3.27 KB
/
json_builder.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "json_builder.h"
namespace json
{
//------------------Builder----------------------------
Builder::Builder()
{
nodes_stack_.push_back(&root_);
}
DictItemContext Builder::StartDict()
{
if (nodes_stack_.back()->IsArray())
{
const_cast<Array&>(nodes_stack_.back()->AsArray()).push_back(Dict());
Node* node = &const_cast<Array&>(nodes_stack_.back()->AsArray()).back();
nodes_stack_.push_back(node);
}
else
{
*nodes_stack_.back() = Dict();
}
return std::move(*this);
}
ArrayItemContext Builder::StartArray()
{
if (nodes_stack_.back()->IsArray())
{
const_cast<Array&>(nodes_stack_.back()->AsArray()).push_back(Array());
Node* node = &const_cast<Array&>(nodes_stack_.back()->AsArray()).back();
nodes_stack_.push_back(node);
}
else
{
*nodes_stack_.back() = Array();
}
return std::move(*this);
}
Builder& Builder::EndDict()
{
nodes_stack_.erase(nodes_stack_.end() - 1);
return *this;
}
Builder& Builder::EndArray()
{
nodes_stack_.erase(nodes_stack_.end() - 1);
return *this;
}
KeyContext Builder::Key(std::string key)
{
nodes_stack_.emplace_back(&const_cast<Dict&>(nodes_stack_.back()->AsDict())[key]);
return std::move(*this);
}
Builder& Builder::Value(Node value)
{
if (nodes_stack_.back()->IsArray())
{
const_cast<Array&>(nodes_stack_.back()->AsArray()).push_back(value);
}
else
{
*nodes_stack_.back() = value;
nodes_stack_.erase(nodes_stack_.end() - 1);
}
return *this;
}
Node Builder::Build()
{
return std::move(root_);
}
//----------------KeyContext----------------
KeyContext::KeyContext(Builder&& builder)
: builder_(builder) {}
DictItemContext KeyContext::Value(Node value)
{
return std::move(builder_.Value(std::move(value)));
}
ArrayItemContext KeyContext::StartArray()
{
return std::move(builder_.StartArray());
}
DictItemContext KeyContext::StartDict()
{
return std::move(builder_.StartDict());
}
//------------------DictItemContext------------------
DictItemContext::DictItemContext(Builder&& builder)
: builder_(builder) {}
KeyContext DictItemContext::Key(std::string key)
{
return std::move(builder_.Key(std::move(key)));
}
Builder& DictItemContext::EndDict()
{
return builder_.EndDict();
}
//-----------------ArrayItemContext------------------------
ArrayItemContext::ArrayItemContext(Builder&& builder)
: builder_(builder) {}
ArrayItemContext& ArrayItemContext::Value(Node value)
{
builder_.Value(std::move(value));
return *this;
}
DictItemContext ArrayItemContext::StartDict()
{
return std::move(builder_.StartDict());
}
ArrayItemContext ArrayItemContext::StartArray()
{
return std::move(builder_.StartArray());
}
Builder& ArrayItemContext::EndArray()
{
return builder_.EndArray();
}
} // namespace json