-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonStreamingParser2.cpp
534 lines (493 loc) · 16.1 KB
/
JsonStreamingParser2.cpp
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
/**The MIT License (MIT)
Copyright (c) 2015 by Daniel Eichhorn
Contributors:
Stefano Chizzolini
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "JsonStreamingParser2.h"
JsonStreamingParser::JsonStreamingParser() {
reset();
}
void JsonStreamingParser::reset() {
state = STATE_START_DOCUMENT;
bufferPos = 0;
unicodeEscapeBufferPos = 0;
unicodeBufferPos = 0;
characterCounter = 0;
}
void JsonStreamingParser::setHandler(JsonHandler* handler) {
myHandler = handler;
}
void JsonStreamingParser::parse(char c) {
#ifdef ARDUINO_ARCH_ESP8266
yield(); // reduce crashes
#endif
//System.out.print(c);
// valid whitespace characters in JSON (from RFC4627 for JSON) include:
// space, horizontal tab, line feed or new line, and carriage return.
// thanks:
// http://stackoverflow.com/questions/16042274/definition-of-whitespace-in-json
if ((c == ' ' || c == '\t' || c == '\n' || c == '\r')
&& !(state == STATE_IN_STRING || state == STATE_UNICODE || state == STATE_START_ESCAPE
|| state == STATE_IN_NUMBER || state == STATE_START_DOCUMENT)) {
return;
}
switch (state) {
case STATE_IN_STRING:
if (c == '"') {
endString();
} else if (c == '\\') {
state = STATE_START_ESCAPE;
} else if ((c < 0x1f) || (c == 0x7f)) {
//throw new RuntimeException("Unescaped control character encountered: " + c + " at position" + characterCounter);
} else {
buffer[bufferPos] = c;
increaseBufferPointer();
}
break;
case STATE_IN_ARRAY:
if (c == ']') {
endArray();
} else {
path.getCurrent()->step();
startValue(c);
}
break;
case STATE_IN_OBJECT:
if (c == '}') {
endObject();
} else if (c == '"') {
startKey();
} else {
//throw new RuntimeException("Start of string expected for object key. Instead got: " + c + " at position" + characterCounter);
}
break;
case STATE_END_KEY:
if (c != ':') {
//throw new RuntimeException("Expected ':' after key. Instead got " + c + " at position" + characterCounter);
}
state = STATE_AFTER_KEY;
break;
case STATE_AFTER_KEY:
startValue(c);
break;
case STATE_START_ESCAPE:
processEscapeCharacters(c);
break;
case STATE_UNICODE:
processUnicodeCharacter(c);
break;
case STATE_UNICODE_SURROGATE:
unicodeEscapeBuffer[unicodeEscapeBufferPos] = c;
unicodeEscapeBufferPos++;
if (unicodeEscapeBufferPos == 2) {
endUnicodeSurrogateInterstitial();
}
break;
case STATE_AFTER_VALUE: {
// not safe for size == 0!!!
int within = stack[stackPos - 1];
if (within == STACK_OBJECT) {
if (c == '}') {
endObject();
} else if (c == ',') {
state = STATE_IN_OBJECT;
} else {
//throw new RuntimeException("Expected ',' or '}' while parsing object. Got: " + c + ". " + characterCounter);
}
} else if (within == STACK_ARRAY) {
if (c == ']') {
endArray();
} else if (c == ',') {
state = STATE_IN_ARRAY;
} else {
//throw new RuntimeException("Expected ',' or ']' while parsing array. Got: " + c + ". " + characterCounter);
}
} else {
//throw new RuntimeException("Finished a literal, but unclear what state to move to. Last state: " + characterCounter);
}
}break;
case STATE_IN_NUMBER:
if (c >= '0' && c <= '9') {
buffer[bufferPos] = c;
increaseBufferPointer();
} else if (c == '.') {
if (doesCharArrayContain(buffer, bufferPos, '.')) {
//throw new RuntimeException("Cannot have multiple decimal points in a number. " + characterCounter);
} else if (doesCharArrayContain(buffer, bufferPos, 'e')) {
//throw new RuntimeException("Cannot have a decimal point in an exponent." + characterCounter);
}
buffer[bufferPos] = c;
increaseBufferPointer();
} else if (c == 'e' || c == 'E') {
if (doesCharArrayContain(buffer, bufferPos, 'e')) {
//throw new RuntimeException("Cannot have multiple exponents in a number. " + characterCounter);
}
buffer[bufferPos] = c;
increaseBufferPointer();
} else if (c == '+' || c == '-') {
char last = buffer[bufferPos - 1];
if (!(last == 'e' || last == 'E')) {
//throw new RuntimeException("Can only have '+' or '-' after the 'e' or 'E' in a number." + characterCounter);
}
buffer[bufferPos] = c;
increaseBufferPointer();
} else {
endNumber();
// we have consumed one beyond the end of the number
parse(c);
}
break;
case STATE_IN_TRUE:
buffer[bufferPos] = c;
increaseBufferPointer();
if (bufferPos == 4) {
endTrue();
}
break;
case STATE_IN_FALSE:
buffer[bufferPos] = c;
increaseBufferPointer();
if (bufferPos == 5) {
endFalse();
}
break;
case STATE_IN_NULL:
buffer[bufferPos] = c;
increaseBufferPointer();
if (bufferPos == 4) {
endNull();
}
break;
case STATE_START_DOCUMENT:
myHandler->startDocument();
if (c == '[') {
startArray();
} else if (c == '{') {
startObject();
} else {
// throw new ParsingError($this->_line_number,
// $this->_char_number,
// "Document must start with object or array.");
}
break;
//case STATE_DONE:
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Expected end of document.");
//default:
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Internal error. Reached an unknown state: ".$this->_state);
}
characterCounter++;
}
void JsonStreamingParser::increaseBufferPointer() {
bufferPos = min(bufferPos + 1, JSON_PARSER_BUFFER_MAX_LENGTH - 1);
}
void JsonStreamingParser::endString() {
int popped = stack[stackPos - 1];
stackPos--;
if (popped == STACK_KEY) {
buffer[bufferPos] = '\0';
path.getCurrent()->set(buffer);
state = STATE_END_KEY;
} else if (popped == STACK_STRING) {
buffer[bufferPos] = '\0';
myHandler->value(path, elementValue.with(buffer));
state = STATE_AFTER_VALUE;
} else {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Unexpected end of string.");
}
bufferPos = 0;
}
void JsonStreamingParser::startValue(char c) {
#ifdef ARDUINO_ARCH_ESP8266
yield();
#endif
if (c == '[') {
startArray();
} else if (c == '{') {
startObject();
} else if (c == '"') {
startString();
} else if (isDigit(c)) {
startNumber(c);
} else if (c == 't') {
state = STATE_IN_TRUE;
buffer[bufferPos] = c;
increaseBufferPointer();
} else if (c == 'f') {
state = STATE_IN_FALSE;
buffer[bufferPos] = c;
increaseBufferPointer();
} else if (c == 'n') {
state = STATE_IN_NULL;
buffer[bufferPos] = c;
increaseBufferPointer();
} else {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Unexpected character for value: ".$c);
}
}
boolean JsonStreamingParser::isDigit(char c) {
// Only concerned with the first character in a number.
return (c >= '0' && c <= '9') || c == '-';
}
void JsonStreamingParser::endArray() {
int popped = stack[stackPos - 1];
stackPos--;
path.pop();
if (popped != STACK_ARRAY) {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Unexpected end of array encountered.");
}
myHandler->endArray(path);
state = STATE_AFTER_VALUE;
if (stackPos == 0) {
endDocument();
}
}
void JsonStreamingParser::startKey() {
stack[stackPos] = STACK_KEY;
stackPos++;
state = STATE_IN_STRING;
}
void JsonStreamingParser::endObject() {
int popped = stack[stackPos];
stackPos--;
path.pop();
if (popped != STACK_OBJECT) {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Unexpected end of object encountered.");
}
myHandler->endObject(path);
state = STATE_AFTER_VALUE;
if (stackPos == 0) {
endDocument();
}
}
void JsonStreamingParser::processEscapeCharacters(char c) {
if (c == '"') {
buffer[bufferPos] = '"';
increaseBufferPointer();
} else if (c == '\\') {
buffer[bufferPos] = '\\';
increaseBufferPointer();
} else if (c == '/') {
buffer[bufferPos] = '/';
increaseBufferPointer();
} else if (c == 'b') {
buffer[bufferPos] = 0x08;
increaseBufferPointer();
} else if (c == 'f') {
buffer[bufferPos] = '\f';
increaseBufferPointer();
} else if (c == 'n') {
buffer[bufferPos] = '\n';
increaseBufferPointer();
} else if (c == 'r') {
buffer[bufferPos] = '\r';
increaseBufferPointer();
} else if (c == 't') {
buffer[bufferPos] = '\t';
increaseBufferPointer();
} else if (c == 'u') {
state = STATE_UNICODE;
} else {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Expected escaped character after backslash. Got: ".$c);
}
if (state != STATE_UNICODE) {
state = STATE_IN_STRING;
}
}
void JsonStreamingParser::processUnicodeCharacter(char c) {
if (!isHexCharacter(c)) {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Expected hex character for escaped Unicode character. Unicode parsed: "
// . implode($this->_unicode_buffer) . " and got: ".$c);
}
unicodeBuffer[unicodeBufferPos] = c;
unicodeBufferPos++;
if (unicodeBufferPos == 4) {
int codepoint = getHexArrayAsDecimal(unicodeBuffer, unicodeBufferPos);
endUnicodeCharacter(codepoint);
return;
/*if (codepoint >= 0xD800 && codepoint < 0xDC00) {
unicodeHighSurrogate = codepoint;
unicodeBufferPos = 0;
state = STATE_UNICODE_SURROGATE;
} else if (codepoint >= 0xDC00 && codepoint <= 0xDFFF) {
if (unicodeHighSurrogate == -1) {
// throw new ParsingError($this->_line_number,
// $this->_char_number,
// "Missing high surrogate for Unicode low surrogate.");
}
int combinedCodePoint = ((unicodeHighSurrogate - 0xD800) * 0x400) + (codepoint - 0xDC00) + 0x10000;
endUnicodeCharacter(combinedCodePoint);
} else if (unicodeHighSurrogate != -1) {
// throw new ParsingError($this->_line_number,
// $this->_char_number,
// "Invalid low surrogate following Unicode high surrogate.");
endUnicodeCharacter(codepoint);
} else {
endUnicodeCharacter(codepoint);
}*/
}
}
boolean JsonStreamingParser::isHexCharacter(char c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
int JsonStreamingParser::getHexArrayAsDecimal(char hexArray[], int length) {
int result = 0;
for (int i = length; i >= 0; i--) {
char current = hexArray[length - i - 1];
int value = 0;
if (current >= 'a' && current <= 'f') {
value = current - 'a' + 10;
} else if (current >= 'A' && current <= 'F') {
value = current - 'A' + 10;
} else if (current >= '0' && current <= '9') {
value = current - '0';
}
result = (result << 4) | value;
}
return result;
}
boolean JsonStreamingParser::doesCharArrayContain(char myArray[], int length, char c) {
for (int i = 0; i < length; i++) {
if (myArray[i] == c) {
return true;
}
}
return false;
}
void JsonStreamingParser::endUnicodeSurrogateInterstitial() {
char unicodeEscape = unicodeEscapeBuffer[unicodeEscapeBufferPos - 1];
if (unicodeEscape != 'u') {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Expected '\\u' following a Unicode high surrogate. Got: " .
// $unicode_escape);
}
unicodeBufferPos = 0;
unicodeEscapeBufferPos = 0;
state = STATE_UNICODE;
}
void JsonStreamingParser::endNumber() {
buffer[bufferPos] = '\0';
if (strchr(buffer, '.') != NULL) {
float floatValue;
sscanf(buffer, "%f", &floatValue);
myHandler->value(path, elementValue.with(floatValue));
} else {
long intValue;
sscanf(buffer, "%d", &intValue);
myHandler->value(path, elementValue.with(intValue));
}
bufferPos = 0;
state = STATE_AFTER_VALUE;
}
int JsonStreamingParser::convertDecimalBufferToInt(char myArray[], int length) {
int result = 0;
for (int i = 0; i < length; i++) {
char current = myArray[length - i - 1];
result += (current - '0') * 10;
}
return result;
}
void JsonStreamingParser::endDocument() {
myHandler->endDocument();
state = STATE_START_DOCUMENT;
bufferPos = 0;
unicodeEscapeBufferPos = 0;
unicodeBufferPos = 0;
characterCounter = 0;
}
void JsonStreamingParser::endTrue() {
buffer[bufferPos] = '\0';
if(strcmp(buffer, "true") == 0) {
myHandler->value(path, elementValue.with(true));
} else {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Expected 'true'. Got: ".$true);
}
bufferPos = 0;
state = STATE_AFTER_VALUE;
}
void JsonStreamingParser::endFalse() {
buffer[bufferPos] = '\0';
if(strcmp(buffer, "false") == 0) {
myHandler->value(path, elementValue.with(false));
} else {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Expected 'true'. Got: ".$true);
}
bufferPos = 0;
state = STATE_AFTER_VALUE;
}
void JsonStreamingParser::endNull() {
buffer[bufferPos] = '\0';
if(strcmp(buffer, "null") == 0) {
myHandler->value(path, elementValue.with());
} else {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Expected 'true'. Got: ".$true);
}
bufferPos = 0;
state = STATE_AFTER_VALUE;
}
void JsonStreamingParser::startArray() {
myHandler->startArray(path);
state = STATE_IN_ARRAY;
stack[stackPos] = STACK_ARRAY;
path.push();
stackPos++;
}
void JsonStreamingParser::startObject() {
myHandler->startObject(path);
state = STATE_IN_OBJECT;
stack[stackPos] = STACK_OBJECT;
path.push();
stackPos++;
}
void JsonStreamingParser::startString() {
stack[stackPos] = STACK_STRING;
stackPos++;
state = STATE_IN_STRING;
}
void JsonStreamingParser::startNumber(char c) {
state = STATE_IN_NUMBER;
buffer[bufferPos] = c;
increaseBufferPointer();
}
void JsonStreamingParser::endUnicodeCharacter(int codepoint) {
if (codepoint < 0x80){
buffer[bufferPos] = (char) (codepoint);
} else if (codepoint <= 0x800){
buffer[bufferPos] = (char) ((codepoint >> 6) | 0b11000000);
increaseBufferPointer();
buffer[bufferPos] = (char) ((codepoint & 0b00111111) | 0b10000000);
} else if (codepoint == 0x2019){
buffer[bufferPos] = '\''; // \u2019 ’
} else {
buffer[bufferPos] = ' ';
}
increaseBufferPointer();
unicodeBufferPos = 0;
unicodeHighSurrogate = -1;
state = STATE_IN_STRING;
}