-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonx.hpp
530 lines (498 loc) · 15 KB
/
jsonx.hpp
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#ifndef JSONX_HPP
#define JSONX_HPP
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <sstream>
#include <stdint.h>
/**
* @brief Namespace for JsonX.
* Insert "using namespace jsonx" into your source code to automatically resolve names.
*/
namespace jsonx {
class scanner;
class json;
/**
* @brief Using std::string for string values.
*/
typedef std::string json_string_t;
/**
* @brief Using std::vector for json arrays.
*/
typedef std::vector<json> json_array_t;
/**
* @brief Using std::map for json objects.
*/
typedef std::map<const std::string, json> json_object_t;
/**
* @brief Using std::pair for items in json objects.
*/
typedef std::pair<const std::string, json> json_object_value_t;
/**
* @brief Using int as index values in [] expressions.
* Note: Using size_t breaks todays compilers for lots of ambiguities.
*/
typedef int json_index_t;
/**
* @brief Using double for real values.
* Note: Using long double breaks todas compilers.
*/
typedef double json_real_t;
/**
* @brief Main json class.
* Simply use "json" as type for every json node in your program.
*/
class json {
public:
typedef enum {
/*0*/ UNDEFINED_T,
/*1*/ NULL_T,
/*2*/ BOOL_T,
/*3*/ SIGNED_T,
/*4*/ UNSIGNED_T,
/*5*/ REAL_T,
/*6*/ STRING_T,
/*7*/ ARRAY_T,
/*8*/ OBJECT_T
} DataType;
// Constants:
static const json undefined;
static const json null;
static const json_array_t empty_array;
static const json_object_t empty_object;
static const std::string empty_string;
// Constructors:
json() {}
json(DataType t);
json(const json& rhs): json() { copyFrom(rhs); }
json(json&& rhs);
json(const json_array_t& rhs): json() { copyFrom(rhs); }
json(const json_object_t& rhs): json() { copyFrom(rhs); }
json(const char *rhs): json() { copyFrom(rhs); }
json(const std::string& rhs): json() { copyFrom(rhs.c_str()); }
json(bool rhs): json() { copyFrom(rhs); }
json(int64_t rhs): json() { copyFrom(rhs); }
json(int8_t rhs): json(static_cast<int64_t>(rhs)) {}
json(int16_t rhs): json(static_cast<int64_t>(rhs)) {}
json(int32_t rhs): json(static_cast<int64_t>(rhs)) {}
json(uint64_t rhs): json() { copyFrom(rhs); }
json(uint8_t rhs): json(static_cast<uint64_t>(rhs)) {}
json(uint16_t rhs): json(static_cast<uint64_t>(rhs)) {}
json(uint32_t rhs): json(static_cast<uint64_t>(rhs)) {}
json(float rhs): json() { copyFrom(static_cast<json_real_t>(rhs)); }
json(double rhs): json() { copyFrom(static_cast<json_real_t>(rhs)); }
// Destructor:
virtual ~json();
// Operations:
void clear();
size_t size() const;
// IO:
void write(std::ostream &os) const;
json_string_t write() const {
std::ostringstream os;
write(os);
return os.str();
}
void parse(std::istream &is);
void parse(const json_string_t &s) {
std::istringstream is(s);
parse(is);
}
// Type checks:
bool isDefined() const {
return type != UNDEFINED_T;
}
bool isNull() const {
return type == NULL_T;
}
bool isBool() const {
return type == BOOL_T;
}
bool isSigned() const {
return type == SIGNED_T;
}
bool isUnsigned() const {
return type == UNSIGNED_T;
}
bool isInt() const {
return type == SIGNED_T || type == UNSIGNED_T;
}
bool isReal() const {
return type == REAL_T;
}
bool isNumber() const {
return isReal() || isInt();
}
bool isString() const {
return type == STRING_T;
}
bool isArray() const {
return type == ARRAY_T;
}
bool isObject() const {
return type == OBJECT_T;
}
// Type conversions:
bool toBool() const;
bool& toBoolRef();
int64_t toSigned() const;
int64_t& toSignedRef64();
int32_t& toSignedRef32();
int16_t& toSignedRef16();
int8_t& toSignedRef8();
uint64_t toUnsigned() const;
uint64_t& toUnsignedRef64();
uint32_t& toUnsignedRef32();
uint16_t& toUnsignedRef16();
uint8_t& toUnsignedRef8();
int toInt() const {
return static_cast<int>(toSigned());
}
json_real_t toReal() const;
json_real_t& toRealRef();
json_real_t toNumber() {
return toReal();
}
json_string_t toString() const;
json_string_t toString();
const json_string_t& toStringRef() const;
json_string_t& toStringRef();
const json_array_t& toArray() const;
json_array_t toArray();
const json_array_t& toArrayRef() const;
json_array_t& toArrayRef();
const json_object_t& toObject() const;
json_object_t toObject();
const json_object_t& toObjectRef() const;
json_object_t& toObjectRef();
const char *c_str() const {
return toStringRef().c_str();
}
// Automatic type conversations:
operator bool() const {
return toBool();
}
operator bool&() {
return toBoolRef();
}
operator int64_t() const {
return toSigned();
}
operator int32_t() const {
return static_cast<int32_t>(toSigned());
}
operator int16_t() const {
return static_cast<int16_t>(toSigned());
}
operator int8_t() const {
return static_cast<int8_t>(toSigned());
}
operator int64_t&() {
return toSignedRef64();
}
operator int32_t&() {
return toSignedRef32();
}
operator int16_t&() {
return toSignedRef16();
}
operator int8_t&() {
return toSignedRef8();
}
operator uint64_t() const {
return toUnsigned();
}
operator uint32_t() const {
return static_cast<uint32_t>(toUnsigned());
}
operator uint16_t() const {
return static_cast<uint16_t>(toUnsigned());
}
operator uint8_t() const {
return static_cast<uint8_t>(toUnsigned());
}
operator uint64_t&() {
return toUnsignedRef64();
}
operator uint32_t&() {
return toUnsignedRef32();
}
operator uint16_t&() {
return toUnsignedRef16();
}
operator uint8_t&() {
return toUnsignedRef8();
}
operator float() const {
return static_cast<float>(toReal());
}
operator double() const {
return static_cast<double>(toReal());
}
operator json_real_t&() {
return toRealRef();
}
operator const json_string_t&() const {
return toStringRef();
}
operator json_string_t&() {
return toStringRef();
}
// JSON value:
void setUndefined() {
clear();
}
void setNull();
void set(const json& v) {
clear();
copyFrom(v);
}
void set(bool v) {
clear();
copyFrom(v);
}
void set(int8_t v) {
clear();
copyFrom(static_cast<int64_t>(v));
}
void set(uint8_t v) {
clear();
copyFrom(static_cast<uint64_t>(v));
}
void set(int16_t v) {
clear();
copyFrom(static_cast<int64_t>(v));
}
void set(uint16_t v) {
clear();
copyFrom(static_cast<uint64_t>(v));
}
void set(int32_t v) {
clear();
copyFrom(static_cast<int64_t>(v));
}
void set(uint32_t v) {
clear();
copyFrom(static_cast<uint64_t>(v));
}
void set(int64_t v) {
clear();
copyFrom(v);
}
void set(uint64_t v) {
clear();
copyFrom(v);
}
void set(float v) {
clear();
copyFrom(static_cast<json_real_t>(v));
}
void set(double v) {
clear();
copyFrom(static_cast<json_real_t>(v));
}
void set(const json_string_t& v) {
set(v.c_str());
}
void set(const char *v) {
if (type == STRING_T) {
*string_value = v;
} else {
clear();
copyFrom(v);
}
}
void set(const json_array_t& v) {
clear();
copyFrom(v);
}
void set(const json_object_t& v) {
clear();
copyFrom(v);
}
// Assignments:
void operator=(const json& v) { set(v); }
void operator=(bool v) { set(v); }
void operator=(int8_t v) { set(v); }
void operator=(uint8_t v) { set(v); }
void operator=(int16_t v) { set(v); }
void operator=(uint16_t v) { set(v); }
void operator=(int32_t v) { set(v); }
void operator=(uint32_t v) { set(v); }
void operator=(int64_t v) { set(v); }
void operator=(uint64_t v) { set(v); }
void operator=(float v) { set(static_cast<json_real_t>(v)); }
void operator=(double v) { set(static_cast<json_real_t>(v)); }
void operator=(const json_string_t &v) { set(v); }
void operator=(const char *v) { set(v); }
void operator=(const json_array_t& v) { set(v); }
void operator=(const json_object_t& v) { set(v); }
// Compares:
bool operator==(const json &v) const;
bool operator==(const char *v) const { return operator==(json(v)); }
bool operator!=(const json &v) const { return !operator==(v); }
bool operator==(bool v) const { return toBool() == v; }
bool operator==(int64_t v) const { return toSigned() == v; }
bool operator==(uint64_t v) const { return toUnsigned() == v; }
bool operator==(int32_t v) const { return toSigned() == v; }
bool operator==(uint32_t v) const { return toUnsigned() == v; }
bool operator==(int16_t v) const { return toSigned() == v; }
bool operator==(uint16_t v) const { return toUnsigned() == v; }
bool operator==(int8_t v) const { return toSigned() == v; }
bool operator==(uint8_t v) const { return toUnsigned() == v; }
bool operator!=(bool v) const { return toBool() != v; }
bool operator!=(int64_t v) const { return toSigned() != v; }
bool operator!=(uint64_t v) const { return toUnsigned() != v; }
bool operator!=(int32_t v) const { return toSigned() != v; }
bool operator!=(uint32_t v) const { return toUnsigned() != v; }
bool operator!=(int16_t v) const { return toSigned() != v; }
bool operator!=(uint16_t v) const { return toUnsigned() != v; }
bool operator!=(int8_t v) const { return toSigned() != v; }
bool operator!=(uint8_t v) const { return toUnsigned() != v; }
bool operator==(const json_array_t &v) const;
bool operator==(const json_object_t &v) const;
// JSON array:
void add(const json &j);
void addUndefined() { add(undefined); }
void addNull() { add(null); }
void add(bool v) { add(json(v)); }
void add(int64_t v) { add(json(v)); }
void add(int32_t v) { add(json(v)); }
void add(int16_t v) { add(json(v)); }
void add(int8_t v) { add(json(v)); }
void add(uint64_t v) { add(json(v)); }
void add(uint32_t v) { add(json(v)); }
void add(uint16_t v) { add(json(v)); }
void add(uint8_t v) { add(json(v)); }
void add(float v) {
add(json(static_cast<json_real_t>(v)));
}
void add(double v) {
add(json(static_cast<json_real_t>(v)));
}
void add(const json_string_t& v) { add(json(v)); }
void add(const char *v) { add(json(v)); }
void add(const json_array_t& v) { add(json(v)); }
void add(const json_object_t& v) { add(json(v)); }
// JSON object:
void add(const std::string &key, const json &j);
void addUndefined(const std::string &key) {
add(key, undefined);
}
void addNull(const std::string &key) {
add(key, null);
}
void add(const std::string &key, bool v) {
add(key, json(v));
}
void add(const std::string &key, int64_t v) {
add(key, json(v));
}
void add(const std::string &key, int32_t v) {
add(key, json(v));
}
void add(const std::string &key, int16_t v) {
add(key, json(v));
}
void add(const std::string &key, int8_t v) {
add(key, json(v));
}
void add(const std::string &key, uint64_t v) {
add(key, json(v));
}
void add(const std::string &key, uint32_t v) {
add(key, json(v));
}
void add(const std::string &key, uint16_t v) {
add(key, json(v));
}
void add(const std::string &key, uint8_t v) {
add(key, json(v));
}
void add(const std::string &key, float v) {
add(key, json(static_cast<json_real_t>(v)));
}
void add(const std::string &key, double v) {
add(key, json(static_cast<json_real_t>(v)));
}
void add(const std::string &key, const json_string_t& v) {
add(key, json(v));
}
void add(const std::string &key, const char *v) {
add(key, json(v));
}
void add(const std::string &key, const json_array_t& v) {
add(key, json(v));
}
void add(const std::string &key, const json_object_t& v) {
add(key, json(v));
}
// Subscriptions:
const json& at(size_t i) const;
json& at(size_t i);
const json& operator[] (json_index_t i) const {
return at(static_cast<size_t>(i));
}
json& operator[] (json_index_t i) {
return at(static_cast<size_t>(i));
}
const json& find(const char *key) const;
json& find(const char *key);
const json& operator[] (const char *key) const {
return find(key);
}
json& operator[] (const char *key) {
return find(key);
}
const json& operator[] (const std::string& key) const {
return find(key.c_str());
}
json& operator[] (const std::string& key) {
return find(key.c_str());
}
DataType getType() const {
return type;
}
private:
void copyFrom(const json& v);
void copyFrom(bool v);
void copyFrom(int64_t v);
void copyFrom(uint64_t v);
void copyFrom(json_real_t v);
void copyFrom(const char *v);
void copyFrom(const json_array_t& v);
void copyFrom(const json_object_t& v);
void parse(scanner &sc);
friend class json_ref;
friend class json_const;
friend std::ostream &operator<<(std::ostream &os, const json &j)
{
j.write(os);
return os;
}
friend std::istream &operator>>(std::istream &is, json &j)
{
j.parse(is);
return is;
}
DataType type{UNDEFINED_T};
union {
bool bool_value;
uint64_t uint_value;
int64_t int_value;
json_real_t real_value;
json_string_t *string_value;
json_array_t *array_value;
json_object_t *object_value;
};
}; // end class json //
// Json object value helper:
inline json_object_value_t jitem(const char *key, const json& val)
{
return json_object_value_t(key, val);
}
json jarray(std::initializer_list<json> v);
json jobject(std::initializer_list<json_object_value_t> v);
} // end namespace jsonx //
#endif // JSONX_HPP