Debian Codegraph @

LspCpp/third_party/rapidjson/example/archiver/archiver.cpp

raw
1 #include "archiver.h"
2 #include <cassert>
3 #include <stack>
4 #include "rapidjson/document.h"
5 #include "rapidjson/prettywriter.h"
6 #include "rapidjson/stringbuffer.h"
7
8 using namespace rapidjson;
9
10 struct JsonReaderStackItem {
11 enum State {
12 BeforeStart, //!< An object/array is in the stack but it is not yet called by StartObject()/StartArray().
13 Started, //!< An object/array is called by StartObject()/StartArray().
14 Closed //!< An array is closed after read all element, but before EndArray().
15 };
16
17 JsonReaderStackItem(const Value* value, State state) : value(value), state(state), index() {}
18
19 const Value* value;
20 State state;
21 SizeType index; // For array iteration
22 };
23
24 typedef std::stack<JsonReaderStackItem> JsonReaderStack;
25
26 #define DOCUMENT reinterpret_cast<Document*>(mDocument)
27 #define STACK (reinterpret_cast<JsonReaderStack*>(mStack))
28 #define TOP (STACK->top())
29 #define CURRENT (*TOP.value)
30
31 JsonReader::JsonReader(const char* json) : mDocument(), mStack(), mError(false) {
32 mDocument = new Document;
33 DOCUMENT->Parse(json);
34 if (DOCUMENT->HasParseError())
35 mError = true;
36 else {
37 mStack = new JsonReaderStack;
38 STACK->push(JsonReaderStackItem(DOCUMENT, JsonReaderStackItem::BeforeStart));
39 }
40 }
41
42 JsonReader::~JsonReader() {
43 delete DOCUMENT;
44 delete STACK;
45 }
46
47 // Archive concept
48 JsonReader& JsonReader::StartObject() {
49 if (!mError) {
50 if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::BeforeStart)
51 TOP.state = JsonReaderStackItem::Started;
52 else
53 mError = true;
54 }
55 return *this;
56 }
57
58 JsonReader& JsonReader::EndObject() {
59 if (!mError) {
60 if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
61 Next();
62 else
63 mError = true;
64 }
65 return *this;
66 }
67
68 JsonReader& JsonReader::Member(const char* name) {
69 if (!mError) {
70 if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started) {
71 Value::ConstMemberIterator memberItr = CURRENT.FindMember(name);
72 if (memberItr != CURRENT.MemberEnd())
73 STACK->push(JsonReaderStackItem(&memberItr->value, JsonReaderStackItem::BeforeStart));
74 else
75 mError = true;
76 }
77 else
78 mError = true;
79 }
80 return *this;
81 }
82
83 bool JsonReader::HasMember(const char* name) const {
84 if (!mError && CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
85 return CURRENT.HasMember(name);
86 return false;
87 }
88
89 JsonReader& JsonReader::StartArray(size_t* size) {
90 if (!mError) {
91 if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::BeforeStart) {
92 TOP.state = JsonReaderStackItem::Started;
93 if (size)
94 *size = CURRENT.Size();
95
96 if (!CURRENT.Empty()) {
97 const Value* value = &CURRENT[TOP.index];
98 STACK->push(JsonReaderStackItem(value, JsonReaderStackItem::BeforeStart));
99 }
100 else
101 TOP.state = JsonReaderStackItem::Closed;
102 }
103 else
104 mError = true;
105 }
106 return *this;
107 }
108
109 JsonReader& JsonReader::EndArray() {
110 if (!mError) {
111 if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::Closed)
112 Next();
113 else
114 mError = true;
115 }
116 return *this;
117 }
118
119 JsonReader& JsonReader::operator&(bool& b) {
120 if (!mError) {
121 if (CURRENT.IsBool()) {
122 b = CURRENT.GetBool();
123 Next();
124 }
125 else
126 mError = true;
127 }
128 return *this;
129 }
130
131 JsonReader& JsonReader::operator&(unsigned& u) {
132 if (!mError) {
133 if (CURRENT.IsUint()) {
134 u = CURRENT.GetUint();
135 Next();
136 }
137 else
138 mError = true;
139 }
140 return *this;
141 }
142
143 JsonReader& JsonReader::operator&(int& i) {
144 if (!mError) {
145 if (CURRENT.IsInt()) {
146 i = CURRENT.GetInt();
147 Next();
148 }
149 else
150 mError = true;
151 }
152 return *this;
153 }
154
155 JsonReader& JsonReader::operator&(double& d) {
156 if (!mError) {
157 if (CURRENT.IsNumber()) {
158 d = CURRENT.GetDouble();
159 Next();
160 }
161 else
162 mError = true;
163 }
164 return *this;
165 }
166
167 JsonReader& JsonReader::operator&(std::string& s) {
168 if (!mError) {
169 if (CURRENT.IsString()) {
170 s = CURRENT.GetString();
171 Next();
172 }
173 else
174 mError = true;
175 }
176 return *this;
177 }
178
179 JsonReader& JsonReader::SetNull() {
180 // This function is for JsonWriter only.
181 mError = true;
182 return *this;
183 }
184
185 void JsonReader::Next() {
186 if (!mError) {
187 assert(!STACK->empty());
188 STACK->pop();
189
190 if (!STACK->empty() && CURRENT.IsArray()) {
191 if (TOP.state == JsonReaderStackItem::Started) { // Otherwise means reading array item pass end
192 if (TOP.index < CURRENT.Size() - 1) {
193 const Value* value = &CURRENT[++TOP.index];
194 STACK->push(JsonReaderStackItem(value, JsonReaderStackItem::BeforeStart));
195 }
196 else
197 TOP.state = JsonReaderStackItem::Closed;
198 }
199 else
200 mError = true;
201 }
202 }
203 }
204
205 #undef DOCUMENT
206 #undef STACK
207 #undef TOP
208 #undef CURRENT
209
210 ////////////////////////////////////////////////////////////////////////////////
211 // JsonWriter
212
213 #define WRITER reinterpret_cast<PrettyWriter<StringBuffer>*>(mWriter)
214 #define STREAM reinterpret_cast<StringBuffer*>(mStream)
215
216 JsonWriter::JsonWriter() : mWriter(), mStream() {
217 mStream = new StringBuffer;
218 mWriter = new PrettyWriter<StringBuffer>(*STREAM);
219 }
220
221 JsonWriter::~JsonWriter() {
222 delete WRITER;
223 delete STREAM;
224 }
225
226 const char* JsonWriter::GetString() const {
227 return STREAM->GetString();
228 }
229
230 JsonWriter& JsonWriter::StartObject() {
231 WRITER->StartObject();
232 return *this;
233 }
234
235 JsonWriter& JsonWriter::EndObject() {
236 WRITER->EndObject();
237 return *this;
238 }
239
240 JsonWriter& JsonWriter::Member(const char* name) {
241 WRITER->String(name, static_cast<SizeType>(strlen(name)));
242 return *this;
243 }
244
245 bool JsonWriter::HasMember(const char*) const {
246 // This function is for JsonReader only.
247 assert(false);
248 return false;
249 }
250
251 JsonWriter& JsonWriter::StartArray(size_t*) {
252 WRITER->StartArray();
253 return *this;
254 }
255
256 JsonWriter& JsonWriter::EndArray() {
257 WRITER->EndArray();
258 return *this;
259 }
260
261 JsonWriter& JsonWriter::operator&(bool& b) {
262 WRITER->Bool(b);
263 return *this;
264 }
265
266 JsonWriter& JsonWriter::operator&(unsigned& u) {
267 WRITER->Uint(u);
268 return *this;
269 }
270
271 JsonWriter& JsonWriter::operator&(int& i) {
272 WRITER->Int(i);
273 return *this;
274 }
275
276 JsonWriter& JsonWriter::operator&(double& d) {
277 WRITER->Double(d);
278 return *this;
279 }
280
281 JsonWriter& JsonWriter::operator&(std::string& s) {
282 WRITER->String(s.c_str(), static_cast<SizeType>(s.size()));
283 return *this;
284 }
285
286 JsonWriter& JsonWriter::SetNull() {
287 WRITER->Null();
288 return *this;
289 }
290
291 #undef STREAM
292 #undef WRITER