-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrapidjson.h
More file actions
409 lines (346 loc) · 8.26 KB
/
Copy pathrapidjson.h
File metadata and controls
409 lines (346 loc) · 8.26 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#pragma once
#include <stdint.h>
#include <string>
#include <vector>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
using json_buffer = rapidjson::StringBuffer;
[[noreturn]] inline void json_throw_error()
{
throw std::runtime_error("json parse error");
}
inline void json_guarantee(bool condition)
{
if (condition) return;
json_throw_error();
}
class json_value
{
public:
json_value(const rapidjson::Value& v) : _value(v)
{
}
bool has_key(const char* key) const
{
json_guarantee(_value.IsObject());
return _value.HasMember(key);
}
json_value operator[](const char* key) const
{
json_guarantee(_value.IsObject());
auto iter = _value.FindMember(key);
json_guarantee(iter != _value.MemberEnd());
return iter->value;
}
json_value operator[](size_t index) const
{
json_guarantee(_value.IsArray());
if (index >= _value.Size())
throw std::runtime_error("json array out of range");
return _value[index];
}
rapidjson::Value::ConstArray get_array() const
{
json_guarantee(_value.IsArray());
return _value.GetArray();
}
template<class Func>
void for_each(const Func& func)
{
for (auto& item : get_array()) {
func(item);
}
}
rapidjson::Value::ConstObject get_object() const
{
json_guarantee(_value.IsObject());
return _value.GetObject();
}
std::string as_string() const
{
json_buffer buffer;
rapidjson::Writer<json_buffer> writer{buffer};
_value.Accept(writer);
return {buffer.GetString(), buffer.GetLength()};
}
template<class T>
operator T() const
{
T t; *this >> t;
return t;
}
public:
const rapidjson::Value& get_value() const
{
return _value;
}
private:
const rapidjson::Value& _value;
};
class json_document
{
public:
void parse(const char* str)
{
_doc.Parse(str);
}
void parse(const char* str, size_t size)
{
_doc.Parse(str, size);
}
void parse(const std::string& str)
{
_doc.Parse(str.c_str(), str.size());
}
const rapidjson::Document& get_document() const
{
return _doc;
}
json_value operator[](const char* key) const
{
return json_value{_doc}[key] ;
}
operator json_value()
{
return _doc;
}
private:
rapidjson::Document _doc;
public:
template<class T>
friend void operator>>(const json_document& doc, T& t)
{
json_value{doc._doc} >> t;
}
};
inline void operator>>(json_value json, uint32_t& x)
{
auto& v = json.get_value();
if (v.IsUint()) { x = v.GetUint(); return; }
if (v.IsNull()) { x = 0; return; }
if (v.IsString()) {
if (sscanf_s(v.GetString(), "%u", &x) == 1)
return;
}
json_throw_error();
}
inline void operator>>(json_value json, int32_t& x)
{
auto& v = json.get_value();
if (v.IsInt()) { x = v.GetInt(); return; }
if (v.IsNull()) { x = 0; return; }
if (v.IsString()) {
if (sscanf_s(v.GetString(), "%d", &x) == 1)
return;
}
json_throw_error();
}
inline void operator>>(json_value json, uint64_t& x)
{
auto& v = json.get_value();
if (v.IsUint64()) { x = v.GetUint64(); return; }
if (v.IsNull()) { x = 0; return; }
if (v.IsString()) {
if (sscanf_s(v.GetString(), "%I64u", &x) == 1)
return;
}
json_throw_error();
}
inline void operator>>(json_value json, float& x)
{
auto& v = json.get_value();
if (v.IsNumber()) { x = v.GetFloat(); return; }
if (v.IsNull()) { x = 0.f; return; }
if (v.IsString()) {
if (sscanf_s(v.GetString(), "%f", &x) == 1)
return;
}
json_throw_error();
}
inline void operator>>(json_value json, double& x)
{
auto& v = json.get_value();
if (v.IsNumber()) { x = v.GetDouble(); return; }
if (v.IsNull()) { x = 0.0; return; }
if (v.IsString()) {
if (sscanf_s(v.GetString(), "%lf", &x) == 1)
return;
}
json_throw_error();
}
inline void operator>>(json_value json, bool& x)
{
auto& v = json.get_value();
if (v.IsBool()) {
x = v.GetBool();
} else {
json_throw_error();
}
}
inline void operator>>(json_value json, std::string& text)
{
auto& v = json.get_value();
if (v.IsString()) { text = v.GetString(); return; }
if (v.IsNull()) { text.clear(); return; }
text = json.as_string();
}
template<class T>
void operator>>(json_value json, std::vector<T>& data)
{
if (json.get_value().IsObject()) {
json["list"] >> data;
} else {
for (auto& item : json.get_array()) {
T val; json_value{item} >> val;
data.push_back(std::move(val));
}
}
}
class json_writer
{
public:
json_writer(json_buffer& buffer) :
_writer(buffer)
{
}
rapidjson::Writer<json_buffer>& get_writer()
{
return _writer;
}
void array(const std::string& value)
{
if (value.size() > 0) {
_writer.RawValue(value.c_str(), value.size(),
rapidjson::kArrayType);
} else {
_writer.RawValue("[]", 2, rapidjson::kArrayType);
}
}
void object(const std::string& value)
{
if (value.size() > 0) {
_writer.RawValue(value.c_str(), value.size(),
rapidjson::kObjectType);
} else {
_writer.RawValue("{}", 2, rapidjson::kObjectType);
}
}
private:
rapidjson::Writer<json_buffer> _writer;
};
class json_object_writer
{
public:
json_object_writer(json_writer& writer) :
_writer(writer)
{
_writer.get_writer().StartObject();
}
~json_object_writer()
{
_writer.get_writer().EndObject();
}
json_writer& operator[](const char* key)
{
_writer.get_writer().Key(key);
return _writer;
}
private:
json_writer& _writer;
private:
json_object_writer(const json_object_writer&) = delete;
json_object_writer& operator=(const json_object_writer&) = delete;
};
class json_array_writer
{
public:
json_array_writer(json_writer& writer) :
_writer(writer)
{
_writer.get_writer().StartArray();
}
~json_array_writer()
{
_writer.get_writer().EndArray();
}
operator json_writer&()
{
return _writer;
}
private:
json_writer& _writer;
private:
json_array_writer(const json_array_writer&) = delete;
json_array_writer& operator=(const json_array_writer&) = delete;
};
inline void operator<<(json_writer& writer, const std::string& val)
{
writer.get_writer().String(val.c_str(), val.size());
}
inline void operator<<(json_writer& writer, const char* val)
{
writer.get_writer().String(val);
}
inline void operator<<(json_writer& writer, float num)
{
char num_text[32] = {};
sprintf_s(num_text, "%.2f", num);
writer.get_writer().String(num_text);
}
inline void operator<<(json_writer& writer, const json_buffer& buffer)
{
writer.get_writer().String(buffer.GetString());
}
inline void operator<<(json_writer& writer, const json_value& val)
{
std::string str = val;
writer << str;
}
template<bool>
struct json_write_redirector
{
template<class T>
static void write(json_writer& w, const T& t)
{
json_object_writer oo{w};
oo << t;
}
};
template<>
struct json_write_redirector<true>
{
template<class T>
static void write(json_writer& w, const T& t)
{
w << std::to_string(t);
}
};
template<class T>
void operator<<(json_writer& w, const T& t)
{
json_write_redirector<std::is_fundamental_v<T>>::write(w, t);
}
template<class Func>
json_buffer json_serialize(const Func& func)
{
json_buffer buffer;
json_writer writer{buffer};
func(writer);
return buffer;
}
template<class T>
void operator<<(json_writer& writer, const std::vector<T>& val)
{
json_array_writer aa{writer};
for (auto& v : val) {
aa << v;
}
}
template<class T>
void operator<<(json_writer& writer, const std::vector<T*>& val)
{
json_array_writer aa{writer};
for (auto v : val) {
aa << *v;
}
}