-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.cpp
More file actions
534 lines (438 loc) · 11.2 KB
/
json.cpp
File metadata and controls
534 lines (438 loc) · 11.2 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
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
531
532
533
534
#include "json.h"
namespace json {
string escape(string const& value) {
std::stringstream ss;
for(auto& c : value){
if(c == '"' // TODO: other escapes
|| c == '\\')
ss << '\\';
ss << c;
}
return ss.str();
}
string unescape(string const& value) {
std::stringstream ss;
for(const char * c = value.data(); *c != '\0'; ++c) {
if(*c == '\\')
++c;
ss << *c;
}
return ss.str();
}
/**
* @cond detail
**/
namespace detail {
static const char * const bool_branch[] { "false", "true" };
static const char * const comma_branch[] { "", ", " };
class value_t {
public:
value_t(){}
virtual ~value_t(){}
virtual string json() const =0;
#define JSON_IS(type) \
virtual bool is_##type() const { return false; } \
virtual type const& get_##type() const { throw exception("invalid cast"); } \
virtual type& get_##type() { throw exception("invalid cast"); }
JSON_IS(array);
JSON_IS(bool);
JSON_IS(number);
JSON_IS(string);
JSON_IS(object);
virtual bool is_null() const { return false; }
#undef JSON_IS
};
class number_wrapper : public value_t {
private:
number *_value;
public:
number_wrapper(int v):_value(new number(v)){}
number_wrapper(double v):_value(new number(v)){}
~number_wrapper(){
delete _value;
}
public:
bool is_number() const { return true; }
number const& get_number() const { return *_value; }
number& get_number() { return *_value; }
string json() const {
std::stringstream ss;
ss << *_value;
return ss.str();
}
};
class string_wrapper : public value_t {
private:
string *_value;
public:
string_wrapper(const char* v):_value(new string(v)){}
string_wrapper(string const& v):_value(new string(v)){}
string_wrapper(stringlike const& v):_value(new string(v.to_json_string())){}
~string_wrapper(){
delete _value;
}
public:
bool is_string() const { return true; }
string const& get_string() const { return *_value; }
string& get_string() { return *_value; }
string json() const {
std::stringstream ss;
ss << "\"" << escape(*_value) << "\"";
return ss.str();
}
};
class array_wrapper : public value_t {
private:
array *_value;
public:
array_wrapper(array const& v):_value(new array(v)){}
array_wrapper(arraylike const& v):_value(new array(v.to_json_array())){}
~array_wrapper(){
delete _value;
}
public:
bool is_array() const { return true; }
array const& get_array() const { return *_value; }
array& get_array() { return *_value; }
inline string json() const;
};
class object_wrapper : public value_t {
private:
object *_value;
public:
object_wrapper(object const& v):_value(new object(v)){}
object_wrapper(objectlike const& v):_value(new object(v.to_json_object())){}
~object_wrapper(){
delete _value;
}
public:
bool is_object() const { return true; }
inline object const& get_object() const;
inline object& get_object();
inline string json() const;
};
class bool_wrapper : public value_t {
private:
bool _value;
public:
bool_wrapper(bool v):_value(v){}
~bool_wrapper(){}
public:
bool const& get_bool() const { return _value; }
bool& get_bool() { return _value; }
bool is_bool() const { return true; }
string json() const {
return bool_branch[_value];
}
};
class null_wrapper : public value_t {
public:
null_wrapper(){}
~null_wrapper(){}
public:
bool is_null() const { return true; }
string json() const {
return "null";
}
};
} // namespace detail
/**
* @endcond detail
**/
value::value()
: _value(nullptr)
, _type(JSON_NULL)
{}
value::value(array const& x)
: _value(new detail::array_wrapper(x))
, _type(JSON_ARRAY)
{}
value::value(arraylike const& x)
: _value(new detail::array_wrapper(x))
, _type(JSON_ARRAY)
{}
value::value(bool x)
: _value(new detail::bool_wrapper(x))
, _type(JSON_BOOL)
{}
value::value(double x)
: _value(new detail::number_wrapper(x))
, _type(JSON_NUMBER)
{}
value::value(int x)
: _value(new detail::number_wrapper(x))
, _type(JSON_NUMBER)
{}
value::value(object const& x)
: _value(new detail::object_wrapper(x))
, _type(JSON_OBJECT)
{}
value::value(objectlike const& x)
: _value(new detail::object_wrapper(x))
, _type(JSON_OBJECT)
{}
value::value(string const& x)
: _value(new detail::string_wrapper(x))
, _type(JSON_STRING)
{}
value::value(const char * const& x)
: _value(new detail::string_wrapper(x))
, _type(JSON_STRING)
{}
value::value(stringlike const& x)
: _value(new detail::string_wrapper(x))
, _type(JSON_STRING)
{}
value::~value() {
}
value::value(value const& v)
: _value(v._value)
, _type(v._type)
{}
value& value::operator=(value const& v) {
_value = v._value;
_type = v._type;
return *this;
}
string value::json() const {
return _value->json();
}
/**
* @return the json @ref ValueType of the value
*/
ValueType value::type() const {
return _type;
}
#define IMPLEMENT_IS_GET(TYPE) \
bool value::is_##TYPE() const { return _value->is_##TYPE(); } \
TYPE const& value::get_##TYPE() const { return _value->get_##TYPE(); } \
TYPE& value::get_##TYPE() { return _value->get_##TYPE(); } \
value::operator TYPE&() { return _value->get_##TYPE(); }
IMPLEMENT_IS_GET(array)
IMPLEMENT_IS_GET(bool)
IMPLEMENT_IS_GET(number)
IMPLEMENT_IS_GET(object)
IMPLEMENT_IS_GET(string)
#undef IMPLEMENT_IS_GET
/**
* @return true if the value is null
**/
bool value::is_null() const { return _value->is_null(); }
std::ostream& operator<<(std::ostream& o, value const& v) {
o << v.json();
return o;
}
std::ostream& operator<<(std::ostream& o, array const& v) {
size_t size { v.size() }, count { 0 };
o << "[";
for(auto const& i : v) {
o << i.json();
o << detail::comma_branch[++count < size];
}
o << "]";
return o;
}
std::ostream& operator<<(std::ostream& o, object const& v) {
size_t size { v.size() }, count { 0 };
o << "{";
for(auto const& i : v) {
o << "\"" << escape(i.first) << "\": ";
o << i.second;
o << detail::comma_branch[++count < size];
}
o << "}";
return o;
}
/**
* @cond detail
**/
namespace detail {
inline bool is_whitespace(char c) {
return c == ' '
|| c == '\r'
|| c == '\n'
|| c == '\t';
}
template<typename Iterator>
value parse(Iterator&, Iterator const&);
template<typename Iterator>
array parse_array(Iterator& cur, Iterator const& end) {
array rv;
bool accept = true, has_elements = false;
do {
if(is_whitespace(*cur)) {
continue;
} else if(*cur == ']' && (has_elements ^ accept)) {
// !accept = strict (no trailing commas)
return rv;
} else if(accept) {
rv.push_back(parse(cur, end));
has_elements = true;
accept = false;
} else if(*cur == ',') {
accept = true;
} else {
break;
}
} while(++cur != end);
throw exception("bad array");
}
template<typename Iterator>
value parse_number(Iterator& cur, Iterator const& end) {
std::stringstream io;
do {
io << *cur;
auto next = cur + 1;
if (!isdigit(*next) && *next != '-' && *next != '.' && *next != 'e' && *next != 'E' && *next != '+') {
break;
}
} while (++cur != end);
io.seekg(0);
double d = 0;
io >> d;
return d;
}
template<typename Iterator>
string parse_string(Iterator& cur, Iterator const& end) {
string rv;
for(bool esc = false; cur != end; ++cur){
if(*cur == '"' && !esc)
return rv;
esc = *cur == '\\' && !esc;
if (!esc)
rv += *cur;
}
throw exception(string("bad string: ") + rv);
}
template<typename Iterator>
object parse_object(Iterator& cur, Iterator const& end) {
object rv;
string key;
bool got_key = false, got_elements = false;
do {
if (*cur == '}' && !got_key) {
return rv;
} else if(is_whitespace(*cur)) {
continue;
} else if(*cur == '"') {
key = parse_string(++cur, end);
got_key = true;
} else if(got_key && *cur == ':') {
rv[key] = parse(++cur, end);
got_key = false;
got_elements = true;
} else if(got_elements && !got_key && *cur == ',') {
continue;
} else {
break;
}
} while(++cur != end);
throw exception("bad object");
}
template<typename Iterator>
value parse(Iterator& cur, Iterator const& end) {
do {
switch(*cur){
case '"': return parse_string(++cur, end);
case '[': return parse_array(++cur, end);
case '{': return parse_object(++cur, end);
case '0' ... '9': case '-': return parse_number(cur, end);
case 'n': if(*++cur=='u' && *++cur=='l' && *++cur=='l') return value(); throw exception("bad json: null");
case 't': if(*++cur=='r' && *++cur=='u' && *++cur=='e') return true; throw exception(string("bad json: ") + *cur);
case 'f': if(*++cur=='a' && *++cur=='l' && *++cur=='s' && *++cur=='e') return false; throw exception("bad json: false");
case ' ': case '\r': case '\n': case '\t':
++cur;
continue;
default:
throw exception("bad json");
}
} while(cur != end);
throw exception("bad json");
}
template<typename T>
class peek {
private:
T v;
public:
peek(T v):v(v){}
T operator*() const {
return v;
}
};
template<typename T>
class iterator {
private:
std::basic_istream<T>& istream;
T cur_val;
public:
iterator(std::basic_istream<T>& is)
: istream(is)
, cur_val(is.get())
{}
iterator& operator++() {
istream.get(cur_val);
return *this;
}
T operator*() const {
return cur_val;
}
peek<T> operator+(int i) const {
// Assumes you are peeking
return istream.peek();
};
bool operator!=(iterator const& other) const {
// Assumes you are checking against the end iterator
return istream.good();
}
};
inline
string array_wrapper::json() const
{
std::stringstream ss;
ss << *_value;
return ss.str();
}
inline
string object_wrapper::json() const
{
std::stringstream ss;
ss << *_value;
return ss.str();
}
inline object const& object_wrapper::get_object() const {
return *_value;
}
inline object& object_wrapper::get_object() {
return *_value;
}
} // namespace detail
/**
* @endcond detail
**/
value parse(string const& s) {
auto i = s.begin(), e = s.end();
auto v = detail::parse(i, e);
while(++i != e) {
if(!detail::is_whitespace(*i))
throw exception(string("garbage at end of input: ") + *i);
}
return v;
}
/**
* @brief parse json from an input stream
* @param istream the input stream from which to parse json
* @return the parsed json as a @ref value
* @throw @ref exception if parsing failed
**/
template<typename T>
value parse(std::basic_istream<T>& istream) {
detail::iterator<T> i (istream);
detail::iterator<T>& e = i;
auto v = detail::parse(i, e);
while(++i != e) {
if(!detail::is_whitespace(*i))
throw exception(string("garbage at end of input: ") + *i);
}
return v;
}
} // namespace json