forked from squix78/json-streaming-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonStreamingParser.cpp
516 lines (480 loc) · 15.9 KB
/
JsonStreamingParser.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
/**The MIT License (MIT)
Copyright (c) 2015 by Daniel Eichhorn
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.
See more at http://blog.squix.ch and https://github.com/squix78/json-streaming-parser
*/
#include "JsonStreamingParser.h"
JsonStreamingParser::JsonStreamingParser() {
reset();
}
void JsonStreamingParser::reset() {
state = STATE_START_DOCUMENT;
bufferPos = 0;
unicodeEscapeBufferPos = 0;
unicodeBufferPos = 0;
characterCounter = 0;
}
void JsonStreamingParser::setListener(JsonListener* listener) {
myListener = listener;
}
void JsonStreamingParser::parse(char c) {
//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 {
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:
myListener->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, BUFFER_MAX_LENGTH - 1);
}
void JsonStreamingParser::endString() {
int popped = stack[stackPos - 1];
stackPos--;
if (popped == STACK_KEY) {
buffer[bufferPos] = '\0';
myListener->key(String(buffer));
state = STATE_END_KEY;
} else if (popped == STACK_STRING) {
buffer[bufferPos] = '\0';
myListener->value(String(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) {
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--;
if (popped != STACK_ARRAY) {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Unexpected end of array encountered.");
}
myListener->endArray();
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--;
if (popped != STACK_OBJECT) {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Unexpected end of object encountered.");
}
myListener->endObject();
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 = 0; i < length; 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 += value * 16^i;
}
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';
String value = String(buffer);
//float result = 0.0;
//if (doesCharArrayContain(buffer, bufferPos, '.')) {
// result = value.toFloat();
//} else {
// needed special treatment in php, maybe not in Java and c
// result = value.toFloat();
//}
myListener->value(value.c_str());
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() {
myListener->endDocument();
state = STATE_DONE;
}
void JsonStreamingParser::endTrue() {
buffer[bufferPos] = '\0';
String value = String(buffer);
if (value.equals("true")) {
myListener->value("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';
String value = String(buffer);
if (value.equals("false")) {
myListener->value("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';
String value = String(buffer);
if (value.equals("null")) {
myListener->value("null");
} else {
// throw new ParsingError($this->_line_number, $this->_char_number,
// "Expected 'true'. Got: ".$true);
}
bufferPos = 0;
state = STATE_AFTER_VALUE;
}
void JsonStreamingParser::startArray() {
myListener->startArray();
state = STATE_IN_ARRAY;
stack[stackPos] = STACK_ARRAY;
stackPos++;
}
void JsonStreamingParser::startObject() {
myListener->startObject();
state = STATE_IN_OBJECT;
stack[stackPos] = STACK_OBJECT;
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) {
buffer[bufferPos] = convertCodepointToCharacter(codepoint);
increaseBufferPointer();
unicodeBufferPos = 0;
unicodeHighSurrogate = -1;
state = STATE_IN_STRING;
}
char JsonStreamingParser::convertCodepointToCharacter(int num) {
if (num <= 0x7F)
return (char) (num);
// if(num<=0x7FF) return (char)((num>>6)+192) + (char)((num&63)+128);
// if(num<=0xFFFF) return
// chr((num>>12)+224).chr(((num>>6)&63)+128).chr((num&63)+128);
// if(num<=0x1FFFFF) return
// chr((num>>18)+240).chr(((num>>12)&63)+128).chr(((num>>6)&63)+128).chr((num&63)+128);
return ' ';
}