-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Json.c
961 lines (731 loc) · 22.3 KB
/
Json.c
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
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project MojoJson, which is hosted on GitHub, and licensed under the MIT License.
*
* License: https://github.com/scottcgi/MojoJson/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/MojoJson
*
* Since : 2013-5-29
* Update : 2021-2-6
* Author : scott.cgi
* Version: 1.2.3
*/
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "Json.h"
#define ALog_A(e, ...) e ? (void) 0 : printf(__VA_ARGS__), printf("\n"), assert(e);
#define ALog_D(...) printf(__VA_ARGS__)
// ArrayList tool for JsonArray
//----------------------------------------------------------------------------------------------------------------------
/**
* The list can dynamically increase memory capacity .
*/
typedef struct
{
/**
* Increase memory space when needed, default 10.
*/
int increase;
/**
* The sizeof element type.
*/
int elementTypeSize;
/**
* Elements count.
*/
int size;
/**
* Store memory data, the length is memory capacity.
* if increase capacity, memory data will realloc.
*/
struct
{
/**
* Elements memory space ptr.
*/
void* data;
/**
* Elements count.
*/
int length;
}
elementArr[1];
}
ArrayList;
static void ArrayListRelease(ArrayList* arrayList)
{
free(arrayList->elementArr->data);
arrayList->elementArr->data = NULL;
arrayList->elementArr->length = 0;
arrayList->size = 0;
}
static void ArrayListInit(int elementTypeSize, ArrayList* arrayList)
{
arrayList->elementArr->data = NULL;
arrayList->elementArr->length = 0;
arrayList->elementTypeSize = elementTypeSize;
arrayList->size = 0;
arrayList->increase = 20;
}
static void ArrayListAddCapacity(ArrayList* arrayList, int increase)
{
ALog_A(increase > 0, "Json ArrayListAddCapacity failed, increase = %d cannot <= 0", increase);
void* data = realloc
(
arrayList->elementArr->data,
(size_t) (increase + arrayList->elementArr->length) * arrayList->elementTypeSize
);
ALog_A
(
data != NULL,
"Json ArrayListAddCapacity failed, unable to realloc memory, size = %d, length = %d, increase = %d",
arrayList->size, arrayList->elementArr->length, increase
);
arrayList->elementArr->data = data;
arrayList->elementArr->length += increase;
}
static void* ArrayListAdd(ArrayList* arrayList, void* elementPtr)
{
if (arrayList->size == arrayList->elementArr->length)
{
ArrayListAddCapacity(arrayList, arrayList->increase);
}
return memcpy
(
(char*) arrayList->elementArr->data + arrayList->elementTypeSize * (arrayList->size++),
elementPtr,
(size_t) arrayList->elementTypeSize
);
}
static void* ArrayListInsert(ArrayList* arrayList, int index, void* elementPtr)
{
if (arrayList->size == arrayList->elementArr->length)
{
ArrayListAddCapacity(arrayList, arrayList->increase);
}
void* from = (char*) arrayList->elementArr->data + arrayList->elementTypeSize * index;
void* to = (char*) from + arrayList->elementTypeSize;
// from and to overlap so cannot use memcpy
memmove(to, from, (size_t) arrayList->elementTypeSize * ((arrayList->size++) - index));
return memcpy(from, elementPtr, (size_t) arrayList->elementTypeSize);
}
/**
* Get the element with type.
*/
#define AArrayList_Get(arrayList, index, ElementType) \
(((ElementType*) ((arrayList)->elementArr->data)))[index]
/**
* Shortcut of ArrayListAdd.
*/
#define AArrayList_Add(arrayList, element) \
ArrayListAdd(arrayList, &(element))
/**
* Shortcut of AArrayList->Insert.
*/
#define AArrayList_Insert(arrayList, index, element) \
ArrayListInsert(arrayList, index, &(element))
/**
* Marked ArrayList element type.
*/
#define ArrayList(ElementType) ArrayList
// ArrayStrMap tool for JsonObject
//----------------------------------------------------------------------------------------------------------------------
/**
* The actual element store in ArrayStrMap.
*/
typedef struct
{
/**
* ArrayStrMap value's key.
* the key data will copy into ArrayStrMapElement malloc space.
*/
const char* key;
/**
* The length of key, include '\0'.
*/
int keyLength;
/**
* ArrayStrMap value pointer.
* the value data copy into ArrayStrMapElement malloc space.
*/
void* valuePtr;
}
ArrayStrMapElement;
/**
* A list of elements each of which is a k-v pair.
*/
typedef struct
{
/**
* The sizeof ArrayStrMap value type.
*/
int valueTypeSize;
/**
* Store all ArrayStrMapElements.
*/
ArrayList(ArrayStrMapElement*) elementList[1];
}
ArrayStrMap;
static void ArrayStrMapRelease(ArrayStrMap* arrayStrMap)
{
for (int i = 0; i < arrayStrMap->elementList->size; ++i)
{
free(AArrayList_Get(arrayStrMap->elementList, i, ArrayStrMapElement*));
}
ArrayListRelease(arrayStrMap->elementList);
}
static void ArrayStrMapInit(int valueTypeSize, ArrayStrMap* outArrayStrMap)
{
ArrayListInit(sizeof(ArrayStrMapElement*), outArrayStrMap->elementList);
outArrayStrMap->valueTypeSize = valueTypeSize;
}
static const char* ArrayStrMapGetKey(ArrayStrMap* arrayStrMap, int index)
{
return AArrayList_Get(arrayStrMap->elementList, index, ArrayStrMapElement*)->key;
}
/**
* Search index of key, if negative not found then return "-insertIndex - 1",
* so insert index is "-BinarySearch() - 1".
*/
static int BinarySearch(ArrayList(ArrayStrMapElement)* elementList, const char* key, int keyLength)
{
int high = elementList->size;
int low = -1;
int guess = -1;
while (high - low > 1) // prevent infinite loops
{
// (high + low) always positive, so convert to unsigned
// then the '>>' is unsigned move right
// so the overflow will be handled correctly
// because sign bit shift to right and 0 will be added
guess = (unsigned int) (high + low) >> 1;
ArrayStrMapElement* element = AArrayList_Get(elementList, guess, ArrayStrMapElement*);
if (element->keyLength < keyLength)
{
low = guess;
}
else if (element->keyLength > keyLength)
{
high = guess;
}
else if (element->keyLength == keyLength)
{
int cmp = memcmp(element->key, key, (size_t) keyLength);
if (cmp < 0)
{
low = guess;
}
else if (cmp > 0)
{
high = guess;
}
else if (cmp == 0)
{
// find the key, the guess is positive value
return guess;
}
}
}
// if guess == high
// the guess is bigger than key index and insert value at guess
if (guess == low)
{
// the guess is smaller than key index and insert value behind,
// or if list empty then the guess is -1, also do this make guess at 0
++guess;
}
// when list empty the guess is 0, so we -1 make sure return negative value
return -guess - 1;
}
static void* ArrayStrMapGet(ArrayStrMap* arrayStrMap, const char* key, void* defaultValuePtr)
{
int guess = BinarySearch(arrayStrMap->elementList, key, (int) strlen(key) + 1);
return guess >= 0 ?
AArrayList_Get(arrayStrMap->elementList, guess, ArrayStrMapElement*)->valuePtr : defaultValuePtr;
}
static void* ArrayStrMapTryPut(ArrayStrMap* arrayStrMap, const char* key, void* valuePtr)
{
int keyLength = (int) strlen(key) + 1;
int guess = BinarySearch(arrayStrMap->elementList, key, keyLength);
if (guess < 0)
{
int valueTypeSize = arrayStrMap->valueTypeSize;
ArrayStrMapElement* element = malloc(sizeof(ArrayStrMapElement) + valueTypeSize + keyLength);
element->keyLength = keyLength;
element->valuePtr = (char*) element + sizeof(ArrayStrMapElement);
element->key = (char*) element->valuePtr + valueTypeSize;
memcpy((void*) element->key, key, (size_t) keyLength);
AArrayList_Insert(arrayStrMap->elementList, -guess - 1, element);
return memcpy(element->valuePtr, valuePtr, (size_t) valueTypeSize);
}
else
{
return NULL;
}
}
/**
* Marked ArrayStrMap key and value.
*/
#define ArrayStrMap(keyName, ValueType) ArrayStrMap
/**
* Shortcut of ArrayStrMapGetAt.
* return value.
*/
#define AArrayStrMap_GetAt(arrayStrMap, index, ValueType) \
(*(ValueType*) AArrayList_Get(arrayStrMap->elementList, index, ArrayStrMapElement*)->valuePtr)
/**
* Shortcut of ArrayStrMapGet.
* return value.
*/
#define AArrayStrMap_Get(arrayStrMap, key, ValueType) \
(*(ValueType*) ArrayStrMapGet(arrayStrMap, key, (void*[1]) {NULL}))
/**
* Shortcut of ArrayStrMapTryPut.
*/
#define AArrayStrMap_TryPut(arrayStrMap, key, value) \
ArrayStrMapTryPut(arrayStrMap, key, &(value))
// Define struct of JsonObject and JsonArray
//----------------------------------------------------------------------------------------------------------------------
/**
* For json object that contains a set of k-v pairs.
*/
struct JsonObject
{
ArrayStrMap(objectKey, JsonValue*) valueMap[1];
};
/**
* For json array that contains a list of json value.
*/
struct JsonArray
{
ArrayList(JsonValue*) valueList[1];
};
// Json value create and destory
//----------------------------------------------------------------------------------------------------------------------
/**
* If the JsonValue is JsonType_Array, then free each items and do recursively.
* if the JsonValue is JsonType_Object, then free each k-v and do recursively.
*/
static void Destroy(JsonValue* value)
{
// JsonValue hold the whole memory
// so free JsonValue will be release JsonValue's memory
switch (value->type)
{
case JsonType_Array:
{
ArrayList* list = value->jsonArray->valueList;
for (int i = 0; i < list->size; ++i)
{
Destroy(AArrayList_Get(list, i, JsonValue*));
}
ArrayListRelease(list);
break;
}
case JsonType_Object:
{
ArrayStrMap* map = value->jsonObject->valueMap;
for (int i = 0; i < map->elementList->size; ++i)
{
Destroy(AArrayStrMap_GetAt(map, i, JsonValue*));
}
ArrayStrMapRelease(map);
break;
}
case JsonType_Float:
break;
case JsonType_String:
break;
case JsonType_Null:
break;
}
free(value);
}
static JsonValue* CreateJsonValue(void* data, size_t valueSize, JsonType type)
{
JsonValue* value = malloc(sizeof(JsonValue) + valueSize);
switch (type)
{
case JsonType_Float:
break;
case JsonType_String:
value->jsonString = memcpy((char*) value + sizeof(JsonValue), data, valueSize);
break;
case JsonType_Array:
value->jsonArray = (JsonArray*) ((char*) value + sizeof(JsonValue));
ArrayListInit(sizeof(JsonValue*), value->jsonArray->valueList);
break;
case JsonType_Object:
value->jsonObject = (JsonObject*) ((char*) value + sizeof(JsonValue));
ArrayStrMapInit(sizeof(JsonValue*), value->jsonObject->valueMap);
break;
default:
ALog_A(false, "Json CreateJsonValue unknown JsonType = %d", type);
}
value->type = type;
return value;
}
// JsonObject API
//----------------------------------------------------------------------------------------------------------------------
static bool ObjectGetBool(JsonObject* object, const char* key, bool defaultValue)
{
JsonValue* jsonValue = AArrayStrMap_Get(object->valueMap, key, JsonValue*);
return jsonValue != NULL ? strcmp(jsonValue->jsonString, "true") == 0 : defaultValue;
}
static int ObjectGetInt(JsonObject* object, const char* key, int defaultValue)
{
JsonValue* jsonValue = AArrayStrMap_Get(object->valueMap, key, JsonValue*);
if (jsonValue != NULL)
{
return (int) jsonValue->jsonFloat;
}
return defaultValue;
}
static float ObjectGetFloat(JsonObject* object, const char* key, float defaultValue)
{
JsonValue* jsonValue = AArrayStrMap_Get(object->valueMap, key, JsonValue*);
if (jsonValue != NULL)
{
return jsonValue->jsonFloat;
}
return defaultValue;
}
static char* ObjectGetString(JsonObject* object, const char* key, const char* defaultValue)
{
JsonValue* jsonValue = AArrayStrMap_Get(object->valueMap, key, JsonValue*);
return jsonValue != NULL ? jsonValue->jsonString : (char*) defaultValue;
}
static JsonObject* ObjectGetObject(JsonObject* object, const char* key)
{
JsonValue* jsonValue = AArrayStrMap_Get(object->valueMap, key, JsonValue*);
return jsonValue != NULL ? jsonValue->jsonObject : NULL;
}
static JsonArray* ObjectGetArray(JsonObject* object, const char* key)
{
JsonValue* jsonValue = AArrayStrMap_Get(object->valueMap, key, JsonValue*);
return jsonValue != NULL ? jsonValue->jsonArray : NULL;
}
static JsonType ObjectGetType(JsonObject* object, const char* key)
{
JsonValue* jsonValue = AArrayStrMap_Get(object->valueMap, key, JsonValue*);
if (jsonValue == NULL)
{
return JsonType_Null;
}
return jsonValue->type;
}
static const char* ObjectGetKey(JsonObject* object, int index)
{
return ArrayStrMapGetKey(object->valueMap, index);
}
static JsonObject* ObjectGetObjectByIndex(JsonObject* object, int index)
{
return AArrayStrMap_GetAt(object->valueMap, index, JsonValue*)->jsonObject;
}
static JsonArray* ObjectGetArrayByIndex(JsonObject* object, int index)
{
return AArrayStrMap_GetAt(object->valueMap, index, JsonValue*)->jsonArray;
}
struct AJsonObject AJsonObject[1] =
{{
ObjectGetBool,
ObjectGetInt,
ObjectGetFloat,
ObjectGetType,
ObjectGetString,
ObjectGetObject,
ObjectGetArray,
ObjectGetKey,
ObjectGetObjectByIndex,
ObjectGetArrayByIndex,
}};
// JsonArray API
//----------------------------------------------------------------------------------------------------------------------
static bool ArrayGetBool(JsonArray* array, int index)
{
return strcmp(AArrayList_Get(array->valueList, index, JsonValue*)->jsonString, "true") == 0;
}
static int ArrayGetInt(JsonArray* array, int index)
{
return (int) AArrayList_Get(array->valueList, index, JsonValue*)->jsonFloat;
}
static float ArrayGetFloat(JsonArray* array, int index)
{
return AArrayList_Get(array->valueList, index, JsonValue*)->jsonFloat;
}
static char* ArrayGetString(JsonArray* array, int index)
{
return AArrayList_Get(array->valueList, index, JsonValue*)->jsonString;
}
static JsonObject* ArrayGetObject(JsonArray* array, int index)
{
return AArrayList_Get(array->valueList, index, JsonValue*)->jsonObject;
}
static JsonArray* ArrayGetArray(JsonArray* array, int index)
{
return AArrayList_Get(array->valueList, index, JsonValue*)->jsonArray;
}
static JsonType ArrayGetType(JsonArray* array, int index)
{
if (index < 0 || index >= array->valueList->size)
{
return JsonType_Null;
}
return AArrayList_Get(array->valueList, index, JsonValue*)->type;
}
struct AJsonArray AJsonArray[1] =
{{
ArrayGetBool,
ArrayGetInt,
ArrayGetFloat,
ArrayGetType,
ArrayGetString,
ArrayGetObject,
ArrayGetArray,
}};
// Json parser
//----------------------------------------------------------------------------------------------------------------------
static void SkipWhiteSpace(const char** jsonPtr)
{
const char* json = *jsonPtr;
while (true)
{
switch (*json)
{
case ' ' :
case '\t':
case '\n':
case '\r':
++json;
continue;
default:
break;
}
break;
}
ALog_A(json != NULL, "The Json parse error on NULL, json is incomplete.");
*jsonPtr = json;
}
static void* ParseNumber(const char** jsonPtr)
{
char* endPtr;
JsonValue* value = CreateJsonValue(NULL, 0, JsonType_Float);
value->jsonFloat = strtof(*jsonPtr, &endPtr);
ALog_D("Json number = %.*s", (int) (endPtr - *jsonPtr), *jsonPtr);
*jsonPtr = endPtr;
return value;
}
static int SkipString(const char** jsonPtr, const char** outStrStart)
{
// skip '"'
const char* json = ++(*jsonPtr);
int count = 0;
char c;
// check end '"'
while ((c = json[count++]) != '"')
{
if (c == '\\')
{
// skip escaped quotes
// the escape char may be '\"',which will break while
++count;
}
}
*outStrStart = json;
// already skipped the string end '"'
*jsonPtr += count;
// how many char skipped
// count contains the string end '"', so -1
return count - 1;
}
static JsonValue* ParseString(const char** jsonPtr)
{
const char* strStart;
int length = SkipString(jsonPtr, &strStart);
JsonValue* value = CreateJsonValue((void*) strStart, (length + 1) * sizeof(char), JsonType_String);
value->jsonString[length] = '\0';
ALog_D("Json string = %s", value->jsonString);
return value;
}
// predefine
static JsonValue* ParseValue(const char** jsonPtr);
static JsonValue* ParseArray(const char** jsonPtr)
{
JsonValue* jsonValue = CreateJsonValue(NULL, sizeof(JsonArray), JsonType_Array);
ArrayList* list = jsonValue->jsonArray->valueList;
ALog_D("Json Array: [");
// skip '['
++(*jsonPtr);
do
{
SkipWhiteSpace(jsonPtr);
if (**jsonPtr == ']')
{
break;
}
JsonValue* value = ParseValue(jsonPtr);
// add Array element
AArrayList_Add(list, value);
SkipWhiteSpace(jsonPtr);
if (**jsonPtr == ',')
{
++(*jsonPtr);
}
else
{
ALog_A(**jsonPtr == ']', "Json Array not has ']', error char = %c ", **jsonPtr);
break;
}
}
while (true);
// skip ']'
++(*jsonPtr);
ALog_D("] JsonArray element count = %d", list->size);
return jsonValue;
}
static JsonValue* ParseObject(const char** jsonPtr)
{
JsonValue* jsonValue = CreateJsonValue(NULL, sizeof(JsonObject), JsonType_Object);
ArrayStrMap* map = jsonValue->jsonObject->valueMap;
ALog_D("Json Object: {");
// skip '{'
++(*jsonPtr);
do
{
SkipWhiteSpace(jsonPtr);
if (**jsonPtr == '}')
{
break;
}
ALog_A(**jsonPtr == '"', "Json object parse error, char = %c, should be '\"' ", **jsonPtr);
const char* strStart;
int keyLen = SkipString(jsonPtr, &strStart);
char key[keyLen + 1];
// make string end
key[keyLen] = '\0';
memcpy(key, strStart, (size_t) keyLen);
ALog_D("Json key = %s", key);
SkipWhiteSpace(jsonPtr);
ALog_A((**jsonPtr) == ':', "Json object parse error, char = %c, should be ':' ", **jsonPtr);
// skip ':'
++(*jsonPtr);
JsonValue* value = ParseValue(jsonPtr);
// set object element
AArrayStrMap_TryPut(map, key, value);
SkipWhiteSpace(jsonPtr);
if (**jsonPtr == ',')
{
++(*jsonPtr);
}
else
{
ALog_A(**jsonPtr == '}', "Json Object not has '}', error char = %c ", **jsonPtr);
break;
}
}
while (true);
// skip '}'
++(*jsonPtr);
ALog_D("} JsonObject elements count = %d", map->elementList->size);
return jsonValue;
}
/**
* ParseValue changed the *jsonPtr, so if *jsonPtr is direct malloc will cause error
*/
static JsonValue* ParseValue(const char** jsonPtr)
{
SkipWhiteSpace(jsonPtr);
char c = **jsonPtr;
switch (c)
{
case '{':
return ParseObject(jsonPtr);
case '[':
return ParseArray(jsonPtr);
case '"':
return ParseString(jsonPtr);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
return ParseNumber(jsonPtr);
case 'f':
{
const char* json = *jsonPtr;
if
(
json[1] == 'a' &&
json[2] == 'l' &&
json[3] == 's' &&
json[4] == 'e'
)
{
ALog_D("Json false");
(*jsonPtr) += 5;
// copy with '\0'
return CreateJsonValue("false", 6, JsonType_String);
}
break;
}
case 't':
{
const char* json = *jsonPtr;
if
(
json[1] == 'r' &&
json[2] == 'u' &&
json[3] == 'e'
)
{
ALog_D("Json true");
(*jsonPtr) += 4;
// copy with '\0'
return CreateJsonValue("true", 5, JsonType_String);
}
break;
}
case 'n':
{
const char* json = *jsonPtr;
if
(
json[1] == 'u' &&
json[2] == 'l' &&
json[3] == 'l'
)
{
ALog_D("Json null");
(*jsonPtr) += 4;
// copy with '\0'
return CreateJsonValue("null", 5, JsonType_String);
}
break;
}
default:
break;
}
ALog_A(false, "Invalid json value type, error char = %c", c);
return NULL;
}
static JsonValue* Parse(const char* jsonString)
{
return ParseValue(&jsonString);
}
struct AJson AJson[1] =
{{
Parse,
Destroy,
}};
#undef ALog_A
#undef ALog_D