-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_parser.h
583 lines (494 loc) · 14.2 KB
/
json_parser.h
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Copyright (C) 2012 Anders Dalle Henning Dalvander
//
// Use, modification and distribution are subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(ADHD_JSON_PARSER_H)
#define ADHD_JSON_PARSER_H
#include "json_value.h"
#include <sstream>
#include <stack>
#include <math.h>
namespace adhd
{
/// Visitor for building a JSON value.
struct json_builder
{
std::stack<json_value*> s;
json_value keyvalue;
std::string key;
json_builder(json_value& root)
: s()
{
s.push(&root);
}
void null_value()
{
*s.top() = json_null();
}
void string_value(const std::string& val)
{
*s.top() = json_string(val);
}
void number_value(double val)
{
*s.top() = json_number(val);
}
void bool_value(bool val)
{
*s.top() = json_bool(val);
}
void begin_array()
{
*s.top() = json_array();
}
void end_array()
{
}
void begin_object()
{
*s.top() = json_object();
}
void end_object()
{
}
void begin_key()
{
s.push(&keyvalue);
}
void end_key()
{
key = s.top()->get_string();
s.pop();
}
void begin_value()
{
if (s.top()->is_object())
s.push(&s.top()->put_child(key));
else
s.push(&s.top()->append_child());
}
void end_value()
{
s.pop();
}
};
/// Parser to convert a JSON document into a JSON value.
class json_parser
{
public:
json_value parse(const std::string& str)
{
return parse(str.c_str());
}
template <typename TIterator>
json_value parse(TIterator iter)
{
json_value root;
parse(iter, root);
return root;
}
template <typename TIterator>
void parse(TIterator iter, json_value& root)
{
json_builder builder(root);
parse(iter, builder);
}
template <typename TIterator, typename TVisitor>
void parse(TIterator iter, TVisitor& visitor)
{
skip_whitespace(iter);
switch (*iter)
{
case '{':
parse_object(iter, visitor);
break;
case '[':
parse_array(iter, visitor);
break;
default:
throw json_parse_exception("expected object or array");
}
skip_whitespace(iter);
if (*iter != '\0')
{
throw json_parse_exception("expected end");
}
}
private:
template <typename TIterator, typename TVisitor>
void parse_object(TIterator& iter, TVisitor& visitor)
{
assert(*iter == '{');
++iter; // Skip '{'
visitor.begin_object();
skip_whitespace(iter);
// Is it an empty object?
if (*iter == '}')
{
++iter;
visitor.end_object();
return;
}
for (;;)
{
if (*iter != '"')
{
throw json_parse_exception("expected string");
}
visitor.begin_key();
parse_string(iter, visitor);
visitor.end_key();
skip_whitespace(iter);
if (*iter++ != ':')
{
throw json_parse_exception("expected name-separator");
}
skip_whitespace(iter);
visitor.begin_value();
parse_value(iter, visitor);
visitor.end_value();
skip_whitespace(iter);
switch (*iter++)
{
case ',':
skip_whitespace(iter);
break;
case '}':
visitor.end_object();
return;
default:
throw json_parse_exception("expected value-separator or end-object");
}
}
}
template <typename TIterator, typename TVisitor>
void parse_array(TIterator& iter, TVisitor& visitor)
{
assert(*iter == '[');
++iter; // Skip '['
visitor.begin_array();
skip_whitespace(iter);
// Is it an empty array?
if (*iter == ']')
{
++iter;
visitor.end_array();
return;
}
for (;;)
{
visitor.begin_value();
parse_value(iter, visitor);
visitor.end_value();
skip_whitespace(iter);
switch (*iter++)
{
case ',':
skip_whitespace(iter);
break;
case ']':
visitor.end_array();
return;
default:
throw json_parse_exception("expected value-separator or end-array");
}
}
}
template <typename TIterator, typename TVisitor>
void parse_null(TIterator& iter, TVisitor& visitor)
{
assert(*iter == 'n');
++iter;
if (*iter++ == 'u' && *iter++ == 'l' && *iter++ == 'l')
{
visitor.null_value();
}
else
{
throw json_parse_exception("expected value");
}
}
template <typename TIterator, typename TVisitor>
void parse_true(TIterator& iter, TVisitor& visitor)
{
assert(*iter == 't');
++iter;
if (*iter++ == 'r' && *iter++ == 'u' && *iter++ == 'e')
{
visitor.bool_value(true);
}
else
{
throw json_parse_exception("expected value");
}
}
template <typename TIterator, typename TVisitor>
void parse_false(TIterator& iter, TVisitor& visitor)
{
assert(*iter == 'f');
++iter;
if (*iter++ == 'a' && *iter++ == 'l' && *iter++ == 's' && *iter++ == 'e')
{
visitor.bool_value(false);
}
else
{
throw json_parse_exception("expected value");
}
}
template <typename TIterator>
unsigned parse_fourhex(TIterator& iter)
{
unsigned codepoint = 0;
for (int i = 0; i < 4; ++i)
{
codepoint <<= 4;
const char c = *iter;
if (c >= '0' && c <= '9')
{
codepoint += c - '0';
}
else if (c >= 'A' && c <= 'F')
{
codepoint += 10 + c - 'A';
}
else if (c >= 'a' && c <= 'f')
{
codepoint += 10 + c - 'a';
}
else
{
throw json_parse_exception("expected 4hexdig");
}
++iter;
}
return codepoint;
}
// Parse string, handling the prefix and suffix double quotes and escaping.
template <typename TIterator, typename TVisitor>
void parse_string(TIterator& iter, TVisitor& visitor)
{
assert(*iter == '\"');
++iter; // Skip '\"'
std::ostringstream ss;
for (;;)
{
char c = *iter++;
if (c == '\\')
{
// Escape character
char e = *iter++;
switch (e)
{
case '\"':
ss << '\"';
break;
case '/':
ss << '/';
break;
case '\\':
ss << '\\';
break;
case 'b':
ss << '\b';
break;
case 'f':
ss << '\f';
break;
case 'n':
ss << '\n';
break;
case 'r':
ss << '\r';
break;
case 't':
ss << '\t';
break;
case 'u':
{
// Escaped unicode
unsigned codepoint = parse_fourhex(iter);
if (codepoint >= 0xd800u && codepoint <= 0xdbffu)
{
// Handle UTF-16 surrogate pairs
if (*iter++ != '\\' || *iter++ != 'u')
{
throw json_parse_exception("expected trailing surrogate");
}
const unsigned codepoint2 = parse_fourhex(iter);
if (codepoint2 < 0xdc00u || codepoint2 > 0xdfffu)
{
throw json_parse_exception("expected trailing surrogate");
}
codepoint = (((codepoint - 0xd800u) << 10) | (codepoint2 - 0xdc00u)) + 0x10000u;
}
else if (codepoint >= 0xdc00u && codepoint <= 0xdfffu)
{
throw json_parse_exception("unexpected trailing surrogate.");
}
if (codepoint < 0x80u)
{
ss << static_cast<unsigned char>(codepoint);
}
else if (codepoint < 0x800u)
{
ss << static_cast<unsigned char>((codepoint >> 6) & 0x1fu | 0xc0u);
ss << static_cast<unsigned char>((codepoint & 0x3fu) | 0x80u);
}
else if (codepoint < 0x10000u)
{
ss << static_cast<unsigned char>((codepoint >> 12) & 0x0fu | 0xe0u);
ss << static_cast<unsigned char>(((codepoint >> 6) & 0x3fu) | 0x80u);
ss << static_cast<unsigned char>((codepoint & 0x3fu) | 0x80u);
}
else
{
ss << static_cast<unsigned char>((codepoint >> 18) & 0x07u | 0xf0u);
ss << static_cast<unsigned char>(((codepoint >> 12) & 0x3fu) | 0x80u);
ss << static_cast<unsigned char>(((codepoint >> 6) & 0x3fu) | 0x80u);
ss << static_cast<unsigned char>((codepoint & 0x3fu) | 0x80u);
}
}
break;
default:
throw json_parse_exception("expected escape");
}
}
else if (c == '\"')
{
visitor.string_value(ss.str());
return;
}
else if (c == '\0')
{
throw json_parse_exception("expected char or quotation-mark");
}
else if (json_value::need_escaping()(c)) //else if (static_cast<unsigned char>(c) < 0x20)
{
throw json_parse_exception("expected char");
}
else
{
ss << c;
}
}
}
template <typename TIterator, typename TInteger>
void parse_int(TIterator& iter, TInteger& integer)
{
while (*iter >= '0' && *iter <= '9')
{
const int digit = *iter++ - '0';
integer = integer * 10 + digit;
}
}
template <typename TIterator>
double parse_fraction(TIterator& iter)
{
double fraction = 0;
double factor = 0.1;
while (*iter >= '0' && *iter <= '9')
{
const int digit = *iter++ - '0';
fraction = fraction + digit * factor;
factor *= 0.1;
}
return fraction;
}
template <typename TIterator, typename TVisitor>
void parse_number(TIterator& iter, TVisitor& visitor)
{
// Check sign.
const bool minus = *iter == '-';
if (minus)
{
++iter;
}
double number = 0;
// Is it zero or an integer?
if (*iter == '0')
{
++iter;
}
else if (*iter >= '1' && *iter <= '9')
{
parse_int(iter, number);
}
else
{
throw json_parse_exception("expected integer");
}
// Is it a fraction?
if (*iter == '.')
{
++iter;
// Only digits are allowed.
if (!(*iter >= '0' && *iter <= '9'))
{
throw json_parse_exception("expected fraction");
}
// Parse and add fraction.
number += parse_fraction(iter);
}
// Is it an exponent?
if (*iter == 'e' || *iter == 'E')
{
++iter;
// Check sign of exponent.
const bool negative_exponent = *iter == '-';
if (*iter == '-' || *iter == '+')
{
++iter;
}
// Only digits are allowed.
if (!(*iter >= '0' && *iter <= '9'))
{
throw json_parse_exception("expected exponent");
}
// Parse and apply exponent.
int exponent = 0;
parse_int(iter, exponent);
number *= pow(10.0, negative_exponent ? -exponent : exponent);
}
visitor.number_value(minus ? -number : number);
}
template <typename TIterator, typename TVisitor>
void parse_value(TIterator& iter, TVisitor& visitor)
{
switch (*iter)
{
case 'n':
parse_null(iter, visitor);
break;
case 't':
parse_true(iter, visitor);
break;
case 'f':
parse_false(iter, visitor);
break;
case '"':
parse_string(iter, visitor);
break;
case '{':
parse_object(iter, visitor);
break;
case '[':
parse_array(iter, visitor);
break;
default:
parse_number(iter, visitor);
break;
}
}
template <typename TIterator>
void skip_whitespace(TIterator& iter)
{
while (*iter == ' ' || *iter == '\n' || *iter == '\r' || *iter == '\t')
{
++iter;
}
}
};
}
#endif