-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-plus.cpp
3181 lines (2698 loc) · 64.3 KB
/
json-plus.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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// json-plus.cpp
//
// Author:
// Brian Sullender
// SULLE WAREHOUSE LLC
//
// Description:
// The source file for the json_plus namespace.
// https://github.com/sullewarehouse/json-plus
//
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
#include <memory.h>
#include <cstring>
#include "json-plus.h"
using namespace json_plus;
// The number of 'char' units to add to a string buffer size when the buffer is too small
// Increasing this number may result in faster parsing but will use more memory
#define JSON_PARSER_BUFFER_INCREASE 32
// The number of 'char' units to add to the generator buffer size when the buffer is too small
// Increasing this number may result in faster encoding but will use more memory
#define JSON_GENERATOR_BUFFER_INCREASE 32
// JSON token types
enum class JSON_TOKEN
{
CURLY_OPEN,
CURLY_CLOSE,
COLON,
STRING,
NUMBER,
ARRAY_OPEN,
ARRAY_CLOSE,
COMMA,
LITERAL,
JSON_END,
UNRECOGNIZED_TOKEN
};
// JSON error strings
static const char* JSON_ERROR_STRINGS[] =
{
"none",
// general errors:
"invalid paramter.",
"out of memory.",
// parse errors:
"unrecognized token.",
"unexpected token, json must start with an object or array; '{' or '[' tokens.",
// parse object errors:
"object syntax error, expected a ':' token before value.",
"object syntax error, key already defined, expected a ':' token and value.",
"object syntax error, key not defined.",
"unexpected closing square bracket ']' token, use closing curly bracket '}' instead to close the object.",
"expected object closing curly bracket '}' token, encountered end of json instead.",
"expected key-value pair, encountered end of object instead.",
// parse array errors:
"unexpected value in array, use the comma ',' token to separate values.",
"unexpected key-value colon ':' token, key-value pair illegal in array, use key-value pairs in object.",
"unexpected closing curly bracket '}' token, use closing square bracket ']' instead to close the array.",
"expected array closing square bracket ']' token, encountered end of json instead.",
"expected array value, encountered end of array instead.",
// parse string errors:
"quotation mark, reverse solidus, and the control characters(0x00 - 0x1F) must be escaped.",
"use (n, r, t, f, b) for control characters (line feed, carriage return, tab, form feed, backspace) respectively.",
"quotation mark, reverse solidus, or control character must follow a reverse solidus character.",
"expected string closing double quotes \" token, encountered end of json instead.",
// parse literal name errors:
"invalid literal name, only 'false', 'null' and 'true' are valid (lowercase only)."
};
// JSON generator context
struct JSON_GENERATOR_CONTEXT
{
bool error;
char* buffer;
size_t bufferLength;
size_t index;
bool visualEscape;
const char* format;
long indentation;
jmp_buf env;
};
// JSON parser context initializer
JSON_PARSER_CONTEXT::JSON_PARSER_CONTEXT()
{
this->errorCode = JSON_ERROR_CODE::NONE;
this->errorDescription = JSON_ERROR_STRINGS[(int)JSON_ERROR_CODE::NONE];
this->visualEscapeOnly = false;
this->charNumber = 0;
this->lineNumber = 0;
this->beginIndex = 0;
this->errorLength = 0;
}
// ---------------------------- //
// ** _JSON_NODE methods ** //
// ---------------------------- //
const char* _JSON_NODE::String()
{
if (this->type == JSON_TYPE::STRING) {
return (const char*)this->value;
}
return NULL;
}
bool _JSON_NODE::Boolean()
{
if (this->type == JSON_TYPE::BOOLEAN) {
return (bool)this->value;
}
return false;
}
double _JSON_NODE::Double()
{
if (this->type == JSON_TYPE::NUMBER) {
return atof((char*)this->value);
}
return 0.0f;
}
int _JSON_NODE::Int()
{
if (this->type == JSON_TYPE::NUMBER) {
return atoi((char*)this->value);
}
return 0;
}
long _JSON_NODE::Long()
{
if (this->type == JSON_TYPE::NUMBER) {
return atol((char*)this->value);
}
return 0;
}
long long _JSON_NODE::Int64()
{
if (this->type == JSON_TYPE::NUMBER) {
return atoll((char*)this->value);
}
return 0;
}
void json_free_node(JSON_NODE* node)
{
if (node->type == JSON_TYPE::OBJECT) {
JSON_Free((JSON_NODE*)node->value);
}
else if (node->type == JSON_TYPE::ARRAY) {
JSON_Free((JSON_NODE*)node->value);
}
else
{
if (node->type != JSON_TYPE::BOOLEAN) {
if (node->value) {
free(node->value);
}
}
}
if (node->key) {
free(node->key);
}
if (node->format) {
free((void*)node->format);
}
free(node);
}
// ------------------------ //
// ** JSON generator ** //
// ------------------------ //
// Forward declaration of json_GeneratorIndentation
// Output tabs for a line, this is automatically enabled when a format string is provided
void json_GeneratorIndentation(JSON_GENERATOR_CONTEXT* context);
// ---------------------------------- //
// ** JSON generator functions ** //
// ---------------------------------- //
// Append a new character to the JSON string buffer
// If `bFormat` is true then the function checks formatting parameters
void json_GeneratorAppend(JSON_GENERATOR_CONTEXT* context, unsigned long CodePoint, bool bFormat)
{
unsigned char CharUnits;
unsigned long newLines;
const char* pFormat = NULL;
if ((bFormat) && (context->format))
{
if (CodePoint == '}') {
context->indentation--;
}
newLines = 0;
pFormat = context->format;
while (*pFormat != '\0')
{
if (*pFormat == '\n') {
newLines++;
}
else if (*pFormat == CodePoint) {
for (unsigned long i = 0; i < newLines; i++) {
json_GeneratorAppend(context, '\n', false);
json_GeneratorIndentation(context);
}
pFormat++;
break;
}
else {
newLines = 0;
}
pFormat++;
}
}
CharUnits = UTF8_Encoding::EncodeUnsafe(NULL, CodePoint);
// buffer big enough for CodePoint + NULL character ?
if ((context->index + CharUnits + 1) >= context->bufferLength)
{
context->bufferLength += JSON_GENERATOR_BUFFER_INCREASE;
char* pNewBuffer = (char*)realloc(context->buffer, context->bufferLength);
if (pNewBuffer != NULL) {
context->buffer = pNewBuffer;
}
else {
context->error = true;
longjmp(context->env, 1);
}
}
context->index += UTF8_Encoding::EncodeUnsafe(&context->buffer[context->index], CodePoint);
if ((bFormat) && (pFormat != NULL))
{
if (CodePoint == '{') {
context->indentation++;
}
while ((*pFormat != '\0') && (*pFormat != 'e'))
{
json_GeneratorAppend(context, *pFormat, false);
if (*pFormat == '\n') {
json_GeneratorIndentation(context);
}
pFormat++;
}
}
}
void json_GeneratorIndentation(JSON_GENERATOR_CONTEXT* context)
{
for (long i = 0; i < context->indentation; i++)
{
json_GeneratorAppend(context, '\t', false);
}
}
// Recursive JSON text generator (from node)
char* json_GenerateText(JSON_NODE* json_node, JSON_GENERATOR_CONTEXT* context)
{
unsigned char CharUnits;
unsigned long CodePoint;
JSON_NODE* node;
const char* format;
long p = -1, n = 0;
const char* extraChars;
format = NULL;
// Look for special 'p' formatting character
if (context->format != NULL)
{
format = context->format;
while (*format != '\0')
{
if (*format == 'p') {
p = 0;
format++;
// Get number of key-value pairs for a single line
while ((*format >= '0') && (*format <= '9')) {
p *= 10;
p += (*format - '0');
format++;
}
break;
}
format++;
}
}
node = json_node;
while (node != NULL)
{
if (node->key != NULL)
{
json_GeneratorAppend(context, '"', true);
const char* pKey = node->key;
CharUnits = UTF8_Encoding::GetCharacterUnits(*pKey);
CodePoint = UTF8_Encoding::Decode(CharUnits, pKey);
while (CodePoint != '\0')
{
// Escape special characters
switch (CodePoint)
{
case 0x22: // " quotation mark
case 0x5C: // \ reverse solidus
json_GeneratorAppend(context, '\\', false);
json_GeneratorAppend(context, CodePoint, false);
break;
case 0x08: // b backspace
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 'b', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
case 0x0C: // f form feed
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 'f', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
case 0x0A: // n line feed
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 'n', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
case 0x0D: // r carriage return
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 'r', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
case 0x09: // t tab
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 't', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
default:
json_GeneratorAppend(context, CodePoint, false);
break;
}
pKey += CharUnits;
CharUnits = UTF8_Encoding::GetCharacterUnits(*pKey);
CodePoint = UTF8_Encoding::Decode(CharUnits, pKey);
}
json_GeneratorAppend(context, '"', true);
json_GeneratorAppend(context, ':', true);
}
if (node->type == JSON_TYPE::OBJECT)
{
format = NULL;
if (node->format) {
format = context->format;
context->format = node->format;
}
json_GeneratorAppend(context, '{', true);
json_GenerateText((JSON_NODE*)node->value, context);
json_GeneratorAppend(context, '}', true);
if (format) {
context->format = format;
}
}
else if (node->type == JSON_TYPE::ARRAY)
{
format = NULL;
if (node->format) {
format = context->format;
context->format = node->format;
}
json_GeneratorAppend(context, '[', true);
json_GenerateText((JSON_NODE*)node->value, context);
json_GeneratorAppend(context, ']', true);
if (format) {
context->format = format;
}
}
else if (node->type == JSON_TYPE::STRING)
{
json_GeneratorAppend(context, '"', true);
const char* pValue = (const char*)node->value;
CharUnits = UTF8_Encoding::GetCharacterUnits(*pValue);
CodePoint = UTF8_Encoding::Decode(CharUnits, pValue);
while (CodePoint != '\0')
{
// Escape special characters
switch (CodePoint)
{
case 0x22: // " quotation mark
case 0x5C: // \ reverse solidus
json_GeneratorAppend(context, '\\', false);
json_GeneratorAppend(context, CodePoint, false);
break;
case 0x08: // b backspace
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 'b', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
case 0x0C: // f form feed
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 'f', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
case 0x0A: // n line feed
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 'n', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
case 0x0D: // r carriage return
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 'r', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
case 0x09: // t tab
json_GeneratorAppend(context, '\\', false);
if (context->visualEscape) {
json_GeneratorAppend(context, 't', false);
}
else {
json_GeneratorAppend(context, CodePoint, false);
}
break;
default:
json_GeneratorAppend(context, CodePoint, false);
break;
}
pValue += CharUnits;
CharUnits = UTF8_Encoding::GetCharacterUnits(*pValue);
CodePoint = UTF8_Encoding::Decode(CharUnits, pValue);
}
json_GeneratorAppend(context, '"', true);
}
else if (node->type == JSON_TYPE::NUMBER)
{
const char* pValue = (const char*)node->value;
CharUnits = UTF8_Encoding::GetCharacterUnits(*pValue);
CodePoint = UTF8_Encoding::Decode(CharUnits, pValue);
while (CodePoint != '\0')
{
json_GeneratorAppend(context, CodePoint, false);
pValue += CharUnits;
CharUnits = UTF8_Encoding::GetCharacterUnits(*pValue);
CodePoint = UTF8_Encoding::Decode(CharUnits, pValue);
}
}
else if (node->type == JSON_TYPE::BOOLEAN)
{
bool bValue = (bool)node->value;
if (bValue) {
json_GeneratorAppend(context, 't', false);
json_GeneratorAppend(context, 'r', false);
json_GeneratorAppend(context, 'u', false);
json_GeneratorAppend(context, 'e', false);
}
else {
json_GeneratorAppend(context, 'f', false);
json_GeneratorAppend(context, 'a', false);
json_GeneratorAppend(context, 'l', false);
json_GeneratorAppend(context, 's', false);
json_GeneratorAppend(context, 'e', false);
}
}
node = node->next;
if (node != NULL)
{
json_GeneratorAppend(context, ',', true);
if (p != -1) {
n++;
if (n >= p) {
json_GeneratorAppend(context, '\n', true);
json_GeneratorIndentation(context);
extraChars = format;
while ((*extraChars != '\0') && (*extraChars != 'e')) {
json_GeneratorAppend(context, *extraChars, true);
extraChars++;
}
n = 0;
}
}
}
}
return NULL;
}
// ------------------------------ //
// ** Forward declarations ** //
// ------------------------------ //
// Parse JSON string
char* json_ParseString(char** pp_json, JSON_PARSER_CONTEXT* context);
// Parse JSON number
char* json_ParseNumber(char** pp_json, JSON_PARSER_CONTEXT* context);
// Parse JSON literal
bool json_ParseLiteral(char** pp_json, JSON_PARSER_CONTEXT* context, JSON_TYPE* pType);
// Parse JSON array
JSON_NODE* json_ParseArray(char** pp_json, JSON_PARSER_CONTEXT* context);
// Parse JSON object
JSON_NODE* json_ParseObject(char** pp_json, JSON_PARSER_CONTEXT* context);
// --------------------------------------- //
// ** Internal JSON parse functions ** //
// --------------------------------------- //
// Get the next token in the JSON string
JSON_TOKEN json_GetToken(char** pp_json, JSON_PARSER_CONTEXT* context)
{
const char* pJson;
unsigned char CharUnits;
unsigned long CodePoint;
JSON_TOKEN token;
pJson = *pp_json;
token = JSON_TOKEN::UNRECOGNIZED_TOKEN;
getCodePoint:
CharUnits = UTF8_Encoding::GetCharacterUnits(*pJson);
CodePoint = UTF8_Encoding::Decode(CharUnits, pJson);
if (CodePoint == ' ')
{
pJson += CharUnits;
context->charNumber++;
goto getCodePoint;
}
if (CodePoint == '\t')
{
pJson += CharUnits;
context->charNumber++;
goto getCodePoint;
}
if (CodePoint == '\n')
{
pJson += CharUnits;
context->lineNumber++;
context->charNumber++;
goto getCodePoint;
}
if (CodePoint == '\r')
{
pJson += CharUnits;
context->charNumber++;
goto getCodePoint;
}
if (CodePoint == '\"')
{
token = JSON_TOKEN::STRING;
pJson += CharUnits;
context->charNumber++;
}
else if (CodePoint == '{')
{
token = JSON_TOKEN::CURLY_OPEN;
pJson += CharUnits;
context->charNumber++;
}
else if (CodePoint == '}')
{
token = JSON_TOKEN::CURLY_CLOSE;
pJson += CharUnits;
context->charNumber++;
}
else if ((CodePoint == '-') || ((CodePoint >= '0') && (CodePoint <= '9')))
{
token = JSON_TOKEN::NUMBER;
}
else if (((CodePoint >= 'A') && (CodePoint <= 'Z')) || ((CodePoint >= 'a') && (CodePoint <= 'z')))
{
token = JSON_TOKEN::LITERAL;
}
else if (CodePoint == '[')
{
token = JSON_TOKEN::ARRAY_OPEN;
pJson += CharUnits;
context->charNumber++;
}
else if (CodePoint == ']')
{
token = JSON_TOKEN::ARRAY_CLOSE;
pJson += CharUnits;
context->charNumber++;
}
else if (CodePoint == ':')
{
token = JSON_TOKEN::COLON;
pJson += CharUnits;
context->charNumber++;
}
else if (CodePoint == ',')
{
token = JSON_TOKEN::COMMA;
pJson += CharUnits;
context->charNumber++;
}
else if (CodePoint == 0)
{
token = JSON_TOKEN::JSON_END;
}
else
{
context->charNumber++;
}
*pp_json = (char*)pJson;
return token;
}
// Parse a JSON string (key or value)
char* json_ParseString(char** pp_json, JSON_PARSER_CONTEXT* context)
{
size_t i;
unsigned char CharUnits;
unsigned long CodePoint;
const char* pJson;
bool bEscape;
char* buffer;
size_t bufferLength;
char* pNewBuffer;
pJson = *pp_json;
bEscape = false;
// bufferLength = 0;
// buffer = NULL;
// Allocating here makes the compiler happy but is not actually needed
bufferLength = JSON_PARSER_BUFFER_INCREASE;
buffer = (char*)malloc(bufferLength);
if (buffer == NULL) {
context->errorCode = JSON_ERROR_CODE::OUT_OF_MEMORY;
context->errorDescription = JSON_ERROR_STRINGS[(int)context->errorCode];
return NULL;
}
i = 0;
while (true)
{
context->beginIndex = context->charNumber;
CharUnits = UTF8_Encoding::GetCharacterUnits(*pJson);
CodePoint = UTF8_Encoding::Decode(CharUnits, pJson);
if (!bEscape)
{
// Escape character?
if (CodePoint == '\\')
{
bEscape = true;
pJson += CharUnits;
context->charNumber++;
continue;
}
// End of string?
if (CodePoint == '\"')
{
pJson += CharUnits;
context->charNumber++;
break;
}
// All Unicode characters may be placed within the
// quotation marks, except for the characters that MUST be escaped :
// quotation mark, reverse solidus, and the control characters(U + 0000
// through U + 001F).
if (((CodePoint >= 0x0000) && (CodePoint <= 0x001F)) ||
(CodePoint == '"') || (CodePoint == '\\'))
{
context->errorCode = JSON_ERROR_CODE::STRING_CHARACTERS_MUST_BE_ESCAPED;
context->charNumber++;
break;
}
}
else
{
// Convert visual representation of control characters
if (CodePoint == 'n') {
CodePoint = 0x0A; // line feed
}
else if (CodePoint == 'r') {
CodePoint = 0x0D; // carriage return
}
else if (CodePoint == 't') {
CodePoint = 0x09; // tab
}
else if (CodePoint == 'f') {
CodePoint = 0x0C; // form feed
}
else if (CodePoint == 'b') {
CodePoint = 0x08; // backspace
}
else
{
// Force strict string escaping for code editor?
if (context->visualEscapeOnly == true)
{
if ((CodePoint == 0x0A) || (CodePoint == 0x0D) ||
(CodePoint == 0x09) || (CodePoint == 0x0C) || (CodePoint == 0x08)) {
context->errorCode = JSON_ERROR_CODE::STRING_FORCED_STRICT_ESCAPING;
context->charNumber++;
break;
}
}
// Must be a quotation mark, reverse solidus, or control character
if ((CodePoint > 0x001F) && (CodePoint != '"') && (CodePoint != '\\')) {
context->errorCode = JSON_ERROR_CODE::STRING_UNUSED_ESCAPE_CHARACTER;
context->charNumber++;
break;
}
}
bEscape = false;
}
if (CodePoint == '\0') {
context->errorCode = JSON_ERROR_CODE::EXPECTED_DOUBLE_QUOTES_ENCOUNTERED_JSON_END;
context->charNumber++;
break;
}
// buffer big enough for CodePoint + NULL character ?
if ((i + CharUnits + 1) > bufferLength)
{
bufferLength += JSON_PARSER_BUFFER_INCREASE;
pNewBuffer = (char*)realloc(buffer, bufferLength);
if (pNewBuffer != NULL) {
buffer = pNewBuffer;
}
else {
context->errorCode = JSON_ERROR_CODE::OUT_OF_MEMORY;
break;
}
}
UTF8_Encoding::EncodeUnsafe(&buffer[i], CodePoint);
pJson += CharUnits;
context->charNumber++;
i += CharUnits;
}
if (context->errorCode != JSON_ERROR_CODE::NONE) {
if (buffer != NULL) {
free(buffer);
}
return NULL;
}
buffer[i] = '\0';
*pp_json = (char*)pJson;
return buffer;
}
// Parse a JSON number, we return the number as an individual string to avoid type assumptions
char* json_ParseNumber(char** pp_json, JSON_PARSER_CONTEXT* context)
{
unsigned char CharUnits;
unsigned long CodePoint;
const char* pJson;
size_t strLen;
char* result;
pJson = *pp_json;
context->beginIndex = context->charNumber;
CharUnits = UTF8_Encoding::GetCharacterUnits(*pJson);
CodePoint = UTF8_Encoding::Decode(CharUnits, pJson);
strLen = 0;
while ((CodePoint == '-') || ((CodePoint >= '0') && (CodePoint <= '9')) || (CodePoint == '.'))
{
pJson += CharUnits;
CharUnits = UTF8_Encoding::GetCharacterUnits(*pJson);
CodePoint = UTF8_Encoding::Decode(CharUnits, pJson);
strLen += CharUnits;
context->charNumber++;
}
result = (char*)malloc(strLen + 1);
if (result == 0) {
return 0;
}
memcpy(result, *pp_json, strLen);
result[strLen] = 0;
*pp_json += strLen;
return result;
}
// JSON literal names are: "false", "null" and "true"
// The literal names MUST be lowercase. No other literal names are allowed.
bool json_ParseLiteral(char** pp_json, JSON_PARSER_CONTEXT* context, JSON_TYPE* pType)
{
const char* pJson;
unsigned char CharUnits;
unsigned long CodePoint;
size_t wordLength;
pJson = *pp_json;
context->beginIndex = context->charNumber;
wordLength = 0;
bool bValue = false;
while (true)
{
CharUnits = UTF8_Encoding::GetCharacterUnits(*pJson);
CodePoint = UTF8_Encoding::Decode(CharUnits, pJson);
if (((CodePoint >= 'A') && (CodePoint <= 'Z')) ||
((CodePoint >= 'a') && (CodePoint <= 'z')) ||
((CodePoint >= '0') && (CodePoint <= '9')) ||
(CodePoint == '_'))
{
wordLength += CharUnits;
context->charNumber++;
pJson += CharUnits;
}
else {
break;
}
}
if (strncmp(*pp_json, "false", wordLength) == 0) {
*pType = JSON_TYPE::BOOLEAN;
}
else if (strncmp(*pp_json, "true", wordLength) == 0) {
*pType = JSON_TYPE::BOOLEAN;
bValue = true;
}
else if (strncmp(*pp_json, "null", wordLength) == 0) {
*pType = JSON_TYPE::NULL_TYPE;
}
else {
context->errorCode = JSON_ERROR_CODE::INVALID_LITERAL_NAME;
}
*pp_json = (char*)pJson;
return bValue;
}
// Parse a JSON array
JSON_NODE* json_ParseArray(char** pp_json, JSON_PARSER_CONTEXT* context)
{
JSON_TOKEN token;
JSON_NODE* root, * node, * prev_node;
const char* pJson;
bool hasCompleted;
root = node = prev_node = 0;
hasCompleted = false;
pJson = *pp_json;
while (!hasCompleted)
{
context->beginIndex = context->charNumber;
token = json_GetToken((char**)&pJson, context);
switch (token)
{
case JSON_TOKEN::CURLY_CLOSE:
context->errorCode = JSON_ERROR_CODE::UNEXPECTED_CLOSING_CURLY_BRACKET;
break;
case JSON_TOKEN::COLON:
context->errorCode = JSON_ERROR_CODE::UNEXPECTED_PAIR_COLON_TOKEN;
break;
case JSON_TOKEN::CURLY_OPEN:
if (node)
{
context->errorCode = JSON_ERROR_CODE::UNEXPECTED_ARRAY_VALUE;
break;
}
else
{
node = (JSON_NODE*)malloc(sizeof(JSON_NODE));
if (!node)
{
context->errorCode = JSON_ERROR_CODE::OUT_OF_MEMORY;
break;
}
memset(node, 0, sizeof(JSON_NODE));
node->type = JSON_TYPE::OBJECT;
node->value = json_ParseObject((char**)&pJson, context);
node->format = NULL;
if (prev_node) {
prev_node->next = node;
}
}
break;