-
Notifications
You must be signed in to change notification settings - Fork 238
/
cJSON.c
2233 lines (2050 loc) · 49 KB
/
cJSON.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
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
/*
Copyright (c) 2009 Dave Gamble
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.
*/
/* cJSON */
/* JSON parser in C. */
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
#include "cJSON.h"
/* Determine the number of bits that an integer has using the preprocessor */
#if INT_MAX == 32767
/* 16 bits */
#define INTEGER_SIZE 0x0010
#elif INT_MAX == 2147483647
/* 32 bits */
#define INTEGER_SIZE 0x0100
#elif INT_MAX == 9223372036854775807
/* 64 bits */
#define INTEGER_SIZE 0x1000
#else
#error "Failed to determine the size of an integer"
#endif
/* define our own boolean type */
typedef int bool;
#define true ((bool)1)
#define false ((bool)0)
static const char *global_ep = NULL;
const char *cJSON_GetErrorPtr(void)
{
return global_ep;
}
/* case insensitive strcmp */
static int cJSON_strcasecmp(const char *s1, const char *s2)
{
if (!s1)
{
return (s1 == s2) ? 0 : 1; /* both NULL? */
}
if (!s2)
{
return 1;
}
for(; tolower(*(const unsigned char *)s1) == tolower(*(const unsigned char *)s2); ++s1, ++s2)
{
if (*s1 == '\0')
{
return 0;
}
}
return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
}
static void *(*cJSON_malloc)(size_t sz) = malloc;
static void (*cJSON_free)(void *ptr) = free;
static char* cJSON_strdup(const char* str)
{
size_t len = 0;
char *copy = NULL;
len = strlen(str) + 1;
if (!(copy = (char*)cJSON_malloc(len)))
{
return NULL;
}
memcpy(copy, str, len);
return copy;
}
void cJSON_InitHooks(cJSON_Hooks* hooks)
{
if (!hooks)
{
/* Reset hooks */
cJSON_malloc = malloc;
cJSON_free = free;
return;
}
cJSON_malloc = (hooks->malloc_fn) ? hooks->malloc_fn : malloc;
cJSON_free = (hooks->free_fn) ? hooks->free_fn : free;
}
/* Internal constructor. */
static cJSON *cJSON_New_Item(void)
{
cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
if (node)
{
memset(node, '\0', sizeof(cJSON));
}
return node;
}
/* Delete a cJSON structure. */
void cJSON_Delete(cJSON *c)
{
cJSON *next = NULL;
while (c)
{
next = c->next;
if (!(c->type & cJSON_IsReference) && c->child)
{
cJSON_Delete(c->child);
}
if (!(c->type & cJSON_IsReference) && c->valuestring)
{
cJSON_free(c->valuestring);
}
if (!(c->type & cJSON_StringIsConst) && c->string)
{
cJSON_free(c->string);
}
cJSON_free(c);
c = next;
}
}
/* Parse the input text to generate a number, and populate the result into item. */
static const char *parse_number(cJSON *item, const char *num)
{
double n = 0;
double sign = 1;
double scale = 0;
int subscale = 0;
int signsubscale = 1;
/* Has sign? */
if (*num == '-')
{
sign = -1;
num++;
}
/* is zero */
if (*num == '0')
{
num++;
}
/* Number? */
if ((*num >= '1') && (*num <= '9'))
{
do
{
n = (n * 10.0) + (*num++ - '0');
}
while ((*num >= '0') && (*num<='9'));
}
/* Fractional part? */
if ((*num == '.') && (num[1] >= '0') && (num[1] <= '9'))
{
num++;
do
{
n = (n *10.0) + (*num++ - '0');
scale--;
} while ((*num >= '0') && (*num <= '9'));
}
/* Exponent? */
if ((*num == 'e') || (*num == 'E'))
{
num++;
/* With sign? */
if (*num == '+')
{
num++;
}
else if (*num == '-')
{
signsubscale = -1;
num++;
}
/* Number? */
while ((*num>='0') && (*num<='9'))
{
subscale = (subscale * 10) + (*num++ - '0');
}
}
/* number = +/- number.fraction * 10^+/- exponent */
n = sign * n * pow(10.0, (scale + subscale * signsubscale));
item->valuedouble = n;
item->valueint = (int)n;
item->type = cJSON_Number;
return num;
}
/* calculate the next largest power of 2 */
static int pow2gt (int x)
{
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
#if INTEGER_SIZE & 0x1110 /* at least 16 bit */
x |= x >> 8;
#endif
#if INTEGER_SIZE & 0x1100 /* at least 32 bit */
x |= x >> 16;
#endif
#if INT_SIZE & 0x1000 /* 64 bit */
x |= x >> 32;
#endif
return x + 1;
}
typedef struct
{
char *buffer;
int length;
int offset;
} printbuffer;
/* realloc printbuffer if necessary to have at least "needed" bytes more */
static char* ensure(printbuffer *p, int needed)
{
char *newbuffer = NULL;
int newsize = 0;
if (!p || !p->buffer)
{
return NULL;
}
needed += p->offset;
if (needed <= p->length)
{
return p->buffer + p->offset;
}
newsize = pow2gt(needed);
newbuffer = (char*)cJSON_malloc(newsize);
if (!newbuffer)
{
cJSON_free(p->buffer);
p->length = 0;
p->buffer = NULL;
return NULL;
}
if (newbuffer)
{
memcpy(newbuffer, p->buffer, p->length);
}
cJSON_free(p->buffer);
p->length = newsize;
p->buffer = newbuffer;
return newbuffer + p->offset;
}
/* calculate the new length of the string in a printbuffer */
static int update(const printbuffer *p)
{
char *str = NULL;
if (!p || !p->buffer)
{
return 0;
}
str = p->buffer + p->offset;
return p->offset + strlen(str);
}
/* Render the number nicely from the given item into a string. */
static char *print_number(const cJSON *item, printbuffer *p)
{
char *str = NULL;
double d = item->valuedouble;
/* special case for 0. */
if (d == 0)
{
if (p)
{
str = ensure(p, 2);
}
else
{
str = (char*)cJSON_malloc(2);
}
if (str)
{
strcpy(str,"0");
}
}
/* value is an int */
else if ((fabs(((double)item->valueint) - d) <= DBL_EPSILON) && (d <= INT_MAX) && (d >= INT_MIN))
{
if (p)
{
str = ensure(p, 21);
}
else
{
/* 2^64+1 can be represented in 21 chars. */
str = (char*)cJSON_malloc(21);
}
if (str)
{
sprintf(str, "%d", item->valueint);
}
}
/* value is a floating point number */
else
{
if (p)
{
/* This is a nice tradeoff. */
str = ensure(p, 64);
}
else
{
/* This is a nice tradeoff. */
str=(char*)cJSON_malloc(64);
}
if (str)
{
/* This checks for NaN and Infinity */
if ((d * 0) != 0)
{
sprintf(str, "null");
}
else if ((fabs(floor(d) - d) <= DBL_EPSILON) && (fabs(d) < 1.0e60))
{
sprintf(str, "%.0f", d);
}
else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9))
{
sprintf(str, "%e", d);
}
else
{
sprintf(str, "%f", d);
}
}
}
return str;
}
/* parse 4 digit hexadecimal number */
static unsigned parse_hex4(const char *str)
{
unsigned h = 0;
/* first digit */
if ((*str >= '0') && (*str <= '9'))
{
h += (*str) - '0';
}
else if ((*str >= 'A') && (*str <= 'F'))
{
h += 10 + (*str) - 'A';
}
else if ((*str >= 'a') && (*str <= 'f'))
{
h += 10 + (*str) - 'a';
}
else /* invalid */
{
return 0;
}
/* second digit */
h = h << 4;
str++;
if ((*str >= '0') && (*str <= '9'))
{
h += (*str) - '0';
}
else if ((*str >= 'A') && (*str <= 'F'))
{
h += 10 + (*str) - 'A';
}
else if ((*str >= 'a') && (*str <= 'f'))
{
h += 10 + (*str) - 'a';
}
else /* invalid */
{
return 0;
}
/* third digit */
h = h << 4;
str++;
if ((*str >= '0') && (*str <= '9'))
{
h += (*str) - '0';
}
else if ((*str >= 'A') && (*str <= 'F'))
{
h += 10 + (*str) - 'A';
}
else if ((*str >= 'a') && (*str <= 'f'))
{
h += 10 + (*str) - 'a';
}
else /* invalid */
{
return 0;
}
/* fourth digit */
h = h << 4;
str++;
if ((*str >= '0') && (*str <= '9'))
{
h += (*str) - '0';
}
else if ((*str >= 'A') && (*str <= 'F'))
{
h += 10 + (*str) - 'A';
}
else if ((*str >= 'a') && (*str <= 'f'))
{
h += 10 + (*str) - 'a';
}
else /* invalid */
{
return 0;
}
return h;
}
/* first bytes of UTF8 encoding for a given length in bytes */
static const unsigned char firstByteMark[7] =
{
0x00, /* should never happen */
0x00, /* 0xxxxxxx */
0xC0, /* 110xxxxx */
0xE0, /* 1110xxxx */
0xF0, /* 11110xxx */
0xF8,
0xFC
};
/* Parse the input text into an unescaped cstring, and populate item. */
static const char *parse_string(cJSON *item, const char *str, const char **ep)
{
const char *ptr = str + 1;
const char *end_ptr =str + 1;
char *ptr2 = NULL;
char *out = NULL;
int len = 0;
unsigned uc = 0;
unsigned uc2 = 0;
/* not a string! */
if (*str != '\"')
{
*ep = str;
return NULL;
}
while ((*end_ptr != '\"') && *end_ptr && ++len)
{
if (*end_ptr++ == '\\')
{
if (*end_ptr == '\0')
{
/* prevent buffer overflow when last input character is a backslash */
return NULL;
}
/* Skip escaped quotes. */
end_ptr++;
}
}
/* This is at most how long we need for the string, roughly. */
out = (char*)cJSON_malloc(len + 1);
if (!out)
{
return NULL;
}
item->valuestring = out; /* assign here so out will be deleted during cJSON_Delete() later */
item->type = cJSON_String;
ptr = str + 1;
ptr2 = out;
/* loop through the string literal */
while (ptr < end_ptr)
{
if (*ptr != '\\')
{
*ptr2++ = *ptr++;
}
/* escape sequence */
else
{
ptr++;
switch (*ptr)
{
case 'b':
*ptr2++ = '\b';
break;
case 'f':
*ptr2++ = '\f';
break;
case 'n':
*ptr2++ = '\n';
break;
case 'r':
*ptr2++ = '\r';
break;
case 't':
*ptr2++ = '\t';
break;
case '\"':
case '\\':
case '/':
*ptr2++ = *ptr;
break;
case 'u':
/* transcode utf16 to utf8. See RFC2781 and RFC3629. */
uc = parse_hex4(ptr + 1); /* get the unicode char. */
ptr += 4;
if (ptr >= end_ptr)
{
/* invalid */
*ep = str;
return NULL;
}
/* check for invalid. */
if (((uc >= 0xDC00) && (uc <= 0xDFFF)) || (uc == 0))
{
*ep = str;
return NULL;
}
/* UTF16 surrogate pairs. */
if ((uc >= 0xD800) && (uc<=0xDBFF))
{
if ((ptr + 6) > end_ptr)
{
/* invalid */
*ep = str;
return NULL;
}
if ((ptr[1] != '\\') || (ptr[2] != 'u'))
{
/* missing second-half of surrogate. */
*ep = str;
return NULL;
}
uc2 = parse_hex4(ptr + 3);
ptr += 6; /* \uXXXX */
if ((uc2 < 0xDC00) || (uc2 > 0xDFFF))
{
/* invalid second-half of surrogate. */
*ep = str;
return NULL;
}
/* calculate unicode codepoint from the surrogate pair */
uc = 0x10000 + (((uc & 0x3FF) << 10) | (uc2 & 0x3FF));
}
/* encode as UTF8
* takes at maximum 4 bytes to encode:
* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
len = 4;
if (uc < 0x80)
{
/* normal ascii, encoding 0xxxxxxx */
len = 1;
}
else if (uc < 0x800)
{
/* two bytes, encoding 110xxxxx 10xxxxxx */
len = 2;
}
else if (uc < 0x10000)
{
/* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
len = 3;
}
ptr2 += len;
switch (len) {
case 4:
/* 10xxxxxx */
*--ptr2 = ((uc | 0x80) & 0xBF);
uc >>= 6;
case 3:
/* 10xxxxxx */
*--ptr2 = ((uc | 0x80) & 0xBF);
uc >>= 6;
case 2:
/* 10xxxxxx */
*--ptr2 = ((uc | 0x80) & 0xBF);
uc >>= 6;
case 1:
/* depending on the length in bytes this determines the
* encoding ofthe first UTF8 byte */
*--ptr2 = (uc | firstByteMark[len]);
}
ptr2 += len;
break;
default:
*ep = str;
return NULL;
}
ptr++;
}
}
*ptr2 = '\0';
if (*ptr == '\"')
{
ptr++;
}
return ptr;
}
/* Render the cstring provided to an escaped version that can be printed. */
static char *print_string_ptr(const char *str, printbuffer *p)
{
const char *ptr = NULL;
char *ptr2 = NULL;
char *out = NULL;
int len = 0;
bool flag = false;
unsigned char token = '\0';
/* empty string */
if (!str)
{
if (p)
{
out = ensure(p, 3);
}
else
{
out = (char*)cJSON_malloc(3);
}
if (!out)
{
return NULL;
}
strcpy(out, "\"\"");
return out;
}
/* set "flag" to 1 if something needs to be escaped */
for (ptr = str; *ptr; ptr++)
{
flag |= (((*ptr > 0) && (*ptr < 32)) /* unprintable characters */
|| (*ptr == '\"') /* double quote */
|| (*ptr == '\\')) /* backslash */
? 1
: 0;
}
/* no characters have to be escaped */
if (!flag)
{
len = ptr - str;
if (p)
{
out = ensure(p, len + 3);
}
else
{
out = (char*)cJSON_malloc(len + 3);
}
if (!out)
{
return NULL;
}
ptr2 = out;
*ptr2++ = '\"';
strcpy(ptr2, str);
ptr2[len] = '\"';
ptr2[len + 1] = '\0';
return out;
}
ptr = str;
/* calculate additional space that is needed for escaping */
while ((token = *ptr) && ++len)
{
if (strchr("\"\\\b\f\n\r\t", token))
{
len++; /* +1 for the backslash */
}
else if (token < 32)
{
len += 5; /* +5 for \uXXXX */
}
ptr++;
}
if (p)
{
out = ensure(p, len + 3);
}
else
{
out = (char*)cJSON_malloc(len + 3);
}
if (!out)
{
return NULL;
}
ptr2 = out;
ptr = str;
*ptr2++ = '\"';
/* copy the string */
while (*ptr)
{
if (((unsigned char)*ptr > 31) && (*ptr != '\"') && (*ptr != '\\'))
{
/* normal character, copy */
*ptr2++ = *ptr++;
}
else
{
/* character needs to be escaped */
*ptr2++ = '\\';
switch (token = *ptr++)
{
case '\\':
*ptr2++ = '\\';
break;
case '\"':
*ptr2++ = '\"';
break;
case '\b':
*ptr2++ = 'b';
break;
case '\f':
*ptr2++ = 'f';
break;
case '\n':
*ptr2++ = 'n';
break;
case '\r':
*ptr2++ = 'r';
break;
case '\t':
*ptr2++ = 't';
break;
default:
/* escape and print as unicode codepoint */
sprintf(ptr2, "u%04x", token);
ptr2 += 5;
break;
}
}
}
*ptr2++ = '\"';
*ptr2++ = '\0';
return out;
}
/* Invoke print_string_ptr (which is useful) on an item. */
static char *print_string(const cJSON *item, printbuffer *p)
{
return print_string_ptr(item->valuestring, p);
}
/* Predeclare these prototypes. */
static const char *parse_value(cJSON *item, const char *value, const char **ep);
static char *print_value(const cJSON *item, int depth, bool fmt, printbuffer *p);
static const char *parse_array(cJSON *item, const char *value, const char **ep);
static char *print_array(const cJSON *item, int depth, bool fmt, printbuffer *p);
static const char *parse_object(cJSON *item, const char *value, const char **ep);
static char *print_object(const cJSON *item, int depth, bool fmt, printbuffer *p);
/* Utility to jump whitespace and cr/lf */
static const char *skip(const char *in)
{
while (in && *in && ((unsigned char)*in<=32))
{
in++;
}
return in;
}
/* Parse an object - create a new root, and populate. */
cJSON *cJSON_ParseWithOpts(const char *value, const char **return_parse_end, bool require_null_terminated)
{
const char *end = NULL;
/* use global error pointer if no specific one was given */
const char **ep = return_parse_end ? return_parse_end : &global_ep;
cJSON *c = cJSON_New_Item();
*ep = NULL;
if (!c) /* memory fail */
{
return NULL;
}
end = parse_value(c, skip(value), ep);
if (!end)
{
/* parse failure. ep is set. */
cJSON_Delete(c);
return NULL;
}
/* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
if (require_null_terminated)
{
end = skip(end);
if (*end)
{
cJSON_Delete(c);
*ep = end;
return NULL;
}
}
if (return_parse_end)
{
*return_parse_end = end;
}
return c;
}
/* Default options for cJSON_Parse */
cJSON *cJSON_Parse(const char *value)
{
return cJSON_ParseWithOpts(value, 0, 0);
}
/* Render a cJSON item/entity/structure to text. */
char *cJSON_Print(const cJSON *item)
{
return print_value(item, 0, 1, 0);
}
char *cJSON_PrintUnformatted(const cJSON *item)
{
return print_value(item, 0, 0, 0);
}
char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, bool fmt)
{
printbuffer p;
p.buffer = (char*)cJSON_malloc(prebuffer);
if (!p.buffer)
{
return NULL;
}
p.length = prebuffer;
p.offset = 0;
return print_value(item, 0, fmt, &p);
}
/* Parser core - when encountering text, process appropriately. */
static const char *parse_value(cJSON *item, const char *value, const char **ep)
{
if (!value)
{
/* Fail on null. */
return NULL;
}
/* parse the different types of values */
if (!strncmp(value, "null", 4))
{
item->type = cJSON_NULL;
return value + 4;
}
if (!strncmp(value, "false", 5))
{
item->type = cJSON_False;
return value + 5;
}
if (!strncmp(value, "true", 4))
{
item->type = cJSON_True;
item->valueint = 1;
return value + 4;
}
if (*value == '\"')
{
return parse_string(item, value, ep);
}
if ((*value == '-') || ((*value >= '0') && (*value <= '9')))
{
return parse_number(item, value);
}
if (*value == '[')
{
return parse_array(item, value, ep);
}
if (*value == '{')
{
return parse_object(item, value, ep);
}
/* failure. */
*ep = value;
return NULL;
}
/* Render a value to text. */
static char *print_value(const cJSON *item, int depth, bool fmt, printbuffer *p)
{
char *out = NULL;
if (!item)
{
return NULL;
}
if (p)
{
switch ((item->type) & 0xFF)
{
case cJSON_NULL:
out = ensure(p, 5);
if (out)
{
strcpy(out, "null");
}
break;
case cJSON_False:
out = ensure(p, 6);
if (out)
{
strcpy(out, "false");
}
break;
case cJSON_True:
out = ensure(p, 5);
if (out)
{
strcpy(out, "true");
}
break;
case cJSON_Number:
out = print_number(item, p);
break;
case cJSON_String:
out = print_string(item, p);
break;
case cJSON_Array:
out = print_array(item, depth, fmt, p);
break;
case cJSON_Object:
out = print_object(item, depth, fmt, p);
break;
}
}
else
{
switch ((item->type) & 0xFF)
{
case cJSON_NULL:
out = cJSON_strdup("null");
break;
case cJSON_False:
out = cJSON_strdup("false");
break;
case cJSON_True:
out = cJSON_strdup("true");
break;
case cJSON_Number:
out = print_number(item, 0);