-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjo.c
1324 lines (1259 loc) · 33.4 KB
/
jo.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
// Lightweight JSON syntax management tool kit
// This toolkit works solely at a syntax level, and does not all management of whole JSON object hierarchy
#include "revk.h"
#include <string.h>
#include <malloc.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "esp_log.h"
#ifndef JO_MAX
#define JO_MAX 64
#endif
struct jo_s
{ // cursor to JSON object
char *buf; // Start of JSON string
const char *err; // If in error state
size_t ptr; // Pointer in to buf
size_t len; // Max space for buf
uint8_t parse:1; // This is parsing, not generating
uint8_t alloc:1; // buf is malloced space
uint8_t comma:1; // Set if comma needed / expected
uint8_t tagok:1; // We have skipped an expected tag already in parsing
uint8_t null:1; // We have a null termination (last character stored was 0)
uint8_t lt:1; // Last character stored was <
uint8_t level; // Current level
uint8_t o[(JO_MAX + 7) / 8]; // Bit set at each level if level is object, else it is array
};
const char JO_BASE64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char JO_BASE32[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
const char JO_BASE16[] = "0123456789ABCDEF";
// Note escaping / is optional but done always to avoid </script> issues
#define escapes \
esc ('"', '"') \
esc ('\\', '\\') \
esc ('b', '\b') \
esc ('f', '\f') \
esc ('n', '\n') \
esc ('r', '\r') \
esc ('t', '\t') \
static jo_t
jo_new (void)
{ // Create a jo_t
jo_t j = mallocspi (sizeof (*j));
if (!j)
return j; // Malloc fail
memset (j, 0, sizeof (*j));
return j;
}
static void *
saferealloc (void *m, size_t len)
{
void *n = realloc (m, len);
if (m && !n)
free (m); // Failed, clear existing
return n;
}
static inline void
jo_store (jo_t j, uint8_t c)
{ // Write out byte
if (!j || j->err)
return;
if (j->parse)
{
j->err = "Writing to read only JSON";
return;
}
if (j->ptr >= j->len && (!j->alloc || !(j->buf = saferealloc (j->buf, j->len += 100))))
{
j->err = (j->alloc ? "Cannot allocate space" : "Out of space");
return;
}
j->buf[j->ptr] = c;
j->null = (c ? 0 : 1);
j->lt = (c == '<' ? 1 : 0);
}
static inline void
jo_write (jo_t j, uint8_t c)
{ // Write a byte and advance
jo_store (j, c);
if (j && !j->err)
j->ptr++;
}
jo_t
jo_pad (jo_t * jp, int n)
{ // Ensure padding available
if (!jp)
return NULL;
jo_t j = *jp;
if (!j->parse)
n += j->level + 1; // Allow space to close and null
if (!j->alloc
|| ((j->parse || j->ptr + n > j->len) && !(j->buf = saferealloc (j->buf, j->len = (j->parse ? j->len : j->ptr) + n))))
{ // Cannot pad
jo_free (jp);
return NULL;
}
return j;
}
static inline int
jo_peek (jo_t j)
{ // Peek next raw byte (-1 for end/fail)
if (!j || j->err || !j->parse || j->ptr >= j->len)
return -1;
return j->buf[j->ptr];
}
static inline int
jo_read (jo_t j)
{ // Read and advance next raw byte (-1 for end/fail)
if (!j || j->err || !j->parse || j->ptr >= j->len)
return -1;
return j->buf[j->ptr++];
}
static int
jo_read_str (jo_t j)
{ // Read next (UTF-8) within a string, so decode escaping (-1 for end/fail)
if (!j || !j->parse || j->err)
return -1;
int bad (const char *e)
{ // Fail
if (!j->err)
j->err = e;
return -1;
}
int utf8 (void)
{ // next character
if (jo_peek (j) == '"')
return -1; // Clean end of string
int c = jo_read (j),
q = 0;
if (c < 0)
return c;
if (c >= 0xF7)
return bad ("Bad UTF-8");
if (c >= 0xF0)
{ // Note could check for F0 and next byte as bad
c &= 0x07;
q = 3;
} else if (c >= 0xE0)
{ // Note could check for E0 and next byte as bad
c &= 0x0F;
q = 2;
} else if (c >= 0xC0)
{
if (c < 0xC2)
return bad ("Bad UTF-8");
c &= 0x1F;
q = 1;
} else if (c >= 0x80)
return bad ("Bat UTF-8");
else if (c == '\\')
{ // Escape
c = jo_read (j);
if (c == 'u')
{ // Hex
c = 0;
for (int q = 4; q; q--)
{
int u = jo_read (j);
if (u >= '0' && u <= '9')
c = (c << 4) + (u & 0xF);
else if ((u >= 'A' && u <= 'F') || (u >= 'a' && u <= 'f'))
c = (c << 4) + 9 + (u & 0xF);
else
return bad ("bad hex escape");
}
}
#define esc(a,b) else if(c==a)c=b;
escapes //
esc ('/', '/')
#undef esc
else
return bad ("Bad escape");
}
while (q--)
{ // More UTF-8 characters
int u = jo_read (j);
if (u < 0x80 || u >= 0xC0)
return bad ("Bad UTF-8");
c = (c << 6) + (u & 0x3F);
}
return c;
}
int c = utf8 ();
if (c >= 0xD800 && c <= 0xDBFF)
{ // UTF16 Surrogates
int c2 = utf8 ();
if (c2 < 0xDC00 || c2 > 0xDFFF)
return bad ("Bad UTF-16, second part invalid");
c = ((c & 0x3FF) << 10) + (c2 & 0x3FF) + 0x10000;
}
return c;
}
// Setting up
jo_t
jo_parse_str (const char *buf)
{ // Start parsing a null terminated JSON object string
if (!buf)
return NULL;
jo_t j = jo_parse_mem (buf, strlen (buf) + 1); // Include the null so we set null tag
return j;
}
jo_t
jo_parse_mem (const void *buf, size_t len)
{ // Start parsing a JSON string in memory - does not need a null
if (!buf)
return NULL; // No buf
jo_t j = jo_new ();
if (!j)
return j; // malloc fail
j->parse = 1;
j->buf = (char *) buf;
if (len && !j->buf[len - 1])
{ // We provided a null, nice
len--;
j->null = 1;
}
j->len = len;
return j;
}
jo_t
jo_create_mem (void *buf, size_t len)
{ // Start creating JSON in memory at buf, max space len.
jo_t j = jo_new ();
if (!j)
return j; // malloc fail
j->buf = buf;
j->len = len;
return j;
}
jo_t
jo_create_alloc (void)
{ // Start creating JSON in memory, allocating space as needed.
jo_t j = jo_new ();
if (!j)
return j; // malloc fail
j->alloc = 1;
return j;
}
jo_t
jo_object_alloc (void)
{ // Common
jo_t j = jo_create_alloc ();
jo_object (j, NULL);
return j;
}
static jo_t
jo_link (jo_t j)
{ // Internal use: Copy control without copying the allocated content - copied object has to stay valid
if (!j || j->err)
return NULL; // No j
jo_t n = jo_new ();
if (!n)
return n; // malloc fail
memcpy (n, j, sizeof (*j));
n->alloc = 0;
return n;
}
jo_t
jo_copy (jo_t j)
{ // Copy object - copies the object, and if allocating memory, makes copy of the allocated memory too
if (!j || j->err)
return NULL; // No j
jo_t n = jo_new ();
if (!n)
return n; // malloc fail
memcpy (n, j, sizeof (*j));
if (j->alloc && j->buf)
{
j->null = 0;
n->buf = mallocspi (j->parse ? j->len + 1 : j->len ? : 1);
if (!n->buf)
{
jo_free (&n);
return NULL; // malloc
}
if (j->parse)
{ // Ensure null
n->buf[j->len] = 0;
j->null = 1;
}
memcpy (n->buf, j->buf, j->parse ? j->len : j->ptr);
if (!j->parse && j->ptr && j->buf[j->ptr - 1] == '<')
j->lt = 1;
}
return n;
}
const char *
jo_rewind (jo_t j)
{ // Move to start for parsing. If was writing, closed and set up to read instead. Clears error is reading.
if (!j)
return NULL;
if (!j->parse)
{ // Finish, if possible
while (j->level)
jo_close (j);
jo_store (j, 0);
if (j->err)
return NULL;
j->len = j->ptr;
j->parse = 1;
}
j->err = NULL;
j->ptr = 0;
j->comma = 0;
j->level = 0;
j->tagok = 0;
if (!j->null)
return NULL;
return j->buf;
}
int
jo_level (jo_t j)
{ // Current level, 0 being the top level
if (!j)
return -1;
return j->level;
}
const char *
jo_error (jo_t j, int *pos)
{ // Return NULL if no error, else returns an error string.
if (pos)
*pos = (j ? j->ptr : -1);
if (j && !j->err && !j->parse && !j->alloc && j->ptr + j->level + 1 > j->len)
return "No space to finish JSON";
if (!j)
return "No j";
return j->err;
}
void
jo_free (jo_t * jp)
{ // Free j
if (!jp)
return;
jo_t j = *jp;
if (!j)
return;
*jp = NULL;
if (j->alloc && j->buf)
free (j->buf);
free (j);
}
int
jo_isalloc (jo_t j)
{
if (j && j->alloc)
return 1;
return 0;
}
char *
jo_finish (jo_t * jp)
{ // Finish creating static JSON, return start of static JSON if no error. Frees j. It is an error to use with jo_create_alloc
if (!jp)
return NULL;
jo_t j = *jp;
if (!j)
return NULL;
*jp = NULL;
if (!j->parse)
{
while (j->level)
jo_close (j);
jo_store (j, 0);
}
char *res = j->buf;
if (j->err || j->alloc)
res = NULL;
if (!res && j->alloc && j->buf)
free (j->buf);
free (j);
return res;
}
char *
jo_finisha (jo_t * jp)
{ // Finish creating allocated JSON, returns start of alloc'd memory if no error.
// Frees j. If NULL returned then any allocated space also freed
// It is an error to use with non jo_create_alloc
if (!jp)
return NULL;
jo_t j = *jp;
if (!j)
return NULL;
*jp = NULL;
if (!j->parse)
{
while (j->level)
jo_close (j);
jo_store (j, 0);
}
char *res = j->buf;
if (j->err || !j->alloc)
res = NULL;
if (!res && j->alloc && j->buf)
free (j->buf);
free (j);
return res;
}
int
jo_len (jo_t j)
{ // Return length, including any closing
if (!j)
return -1;
if (j->parse)
return j->len;
return j->ptr + j->level;
}
// Creating
// Note that tag is required if in an object and must be null if not
static void
jo_write_char (jo_t j, uint32_t c)
{
#define esc(a,b) if(c==b){jo_write(j,'\\');jo_write(j,a);return;}
escapes
#undef esc
if (c == '/' && j->lt)
jo_write (j, '\\'); // escape / is optional, but we always do after < to avoid </script>
if (c < ' ' || c >= 0xFF)
{
jo_write (j, '\\');
jo_write (j, 'u');
jo_write (j, JO_BASE16[(c >> 12) & 0xF]);
jo_write (j, JO_BASE16[(c >> 8) & 0xF]);
jo_write (j, JO_BASE16[(c >> 4) & 0xF]);
jo_write (j, JO_BASE16[c & 0xF]);
return;
}
jo_write (j, c);
}
static int
isutf8 (const char *s, int n)
{ // If valid UTF8 returns number of characters, 0 for bad, 1 for ASCII - note this does not allow F8 and beyond. Note this does not check for minimal coding (e.g. C0 00 is invalid but allowed)
if (n < 1)
return 0;
if (*s < 0x80)
return 1; // ASCII
if (*s < 0xC0)
return 0; // follow on byte
if (n < 2 || s[1] < 0x80 || s[1] >= 0xC0)
return 0;
if (*s < 0xE0)
return 2;
if (n < 3 || s[2] < 0x80 || s[2] >= 0xC0)
return 0;
if (*s < 0xF0)
return 3;
if (n < 4 || s[3] < 0x80 || s[3] >= 0xC0)
return 0;
if (*s < 0xF8)
return 4;
return 0;
}
static void
jo_write_str (jo_t j, const char *s, ssize_t len)
{
jo_write (j, '"');
if (len < 0)
len = strlen (s);
while (len)
{
int l = isutf8 (s, len);
if (!l)
{ // Not valid UTF8, write as escaped so obvious, but if you read this back it escapes to unicode, which is technically iffy, but better than just an error
jo_write (j, '\\');
jo_write (j, 'u');
jo_write (j, '0');
jo_write (j, '0');
jo_write (j, JO_BASE16[(*s >> 4) & 0xF]);
jo_write (j, JO_BASE16[*s & 0xF]);
s++;
len--;
continue;
}
len -= l;
while (l--)
jo_write_char (j, *s++);
}
jo_write (j, '"');
}
static const char *
jo_write_check (jo_t j, const char *tag)
{ // Check if we are able to write, and write the tag
if (!j)
return "No j";
if (j->err)
return j->err;
if (j->parse)
return (j->err = "Writing to parse");
if (j->level && (j->o[(j->level - 1) / 8] & (1 << ((j->level - 1) & 7))))
{
if (!tag)
return (j->err = "missing tag in object");
} else if (tag)
return (j->err = "tag in non object");
if (!j->level && j->ptr)
return (j->err = "second value at top level");
if (j->comma)
jo_write (j, ',');
j->comma = 1;
if (tag)
{
jo_write_str (j, tag, -1);
jo_write (j, ':');
}
return j->err;
}
void
jo_json (jo_t j, const char *tag, jo_t json)
{ // Add JSON, if NULL tag, and in object, and JSON is object, add its content
if (!json)
{
jo_null (j, tag);
return;
}
if (!json->parse)
{ // Close the json
while (json->level)
jo_close (json);
jo_store (json, 0);
}
char *p = json->buf,
*e = p + json->ptr;
if (json->parse)
e = p + json->len;
if (!tag && p && *p == '{' && j->level && (j->o[(j->level - 1) / 8] & (1 << ((j->level - 1) & 7))))
{ // Special case of adding in-line object content
p++;
e--;
if (j->comma)
jo_write (j, ',');
j->comma = 1;
} else
{ // Normal add as value (possibly tagged)
if (jo_write_check (j, tag))
return;
}
while (p < e)
jo_write (j, *p++);
}
void
jo_lit (jo_t j, const char *tag, const char *lit)
{ // Add literal
if (jo_write_check (j, tag))
return;
while (*lit)
jo_write (j, *lit++);
}
void
jo_array (jo_t j, const char *tag)
{ // Start an array
if (jo_write_check (j, tag))
return;
if (j->level >= JO_MAX)
{
j->err = "JSON too deep";
return;
}
j->o[j->level / 8] &= ~(1 << (j->level & 7));
j->level++;
j->comma = 0;
jo_write (j, '[');
}
void
jo_object (jo_t j, const char *tag)
{ // Start an object
if (jo_write_check (j, tag))
return;
if (j->level >= JO_MAX)
{
if (!j->err)
j->err = "JSON too deep";
return;
}
j->o[j->level / 8] |= (1 << (j->level & 7));
j->level++;
j->comma = 0;
jo_write (j, '{');
}
void
jo_close (jo_t j)
{ // Close current array or object
if (!j->level)
{
if (!j->err)
j->err = "JSON too many closes";
return;
}
j->level--;
j->comma = 1;
jo_write (j, (j->o[j->level / 8] & (1 << (j->level & 7))) ? '}' : ']');
}
void
jo_stringn (jo_t j, const char *tag, const char *string, ssize_t len)
{ // Add a string
if (!string)
{
jo_null (j, tag);
return;
}
if (jo_write_check (j, tag))
return;
jo_write_str (j, string, len);
}
void
jo_stringf (jo_t j, const char *tag, const char *format, ...)
{ // Add a string (formatted)
if (jo_write_check (j, tag))
return;
char *v = NULL;
va_list ap;
va_start (ap, format);
ssize_t len = vasprintf (&v, format, ap);
va_end (ap);
if (!v)
{
j->err = "malloc for printf";
return;
}
jo_write_str (j, v, len);
free (v);
}
void
jo_litf (jo_t j, const char *tag, const char *format, ...)
{ // Add a literal (formatted)
char temp[100];
va_list ap;
va_start (ap, format);
vsnprintf (temp, sizeof (temp), format, ap);
va_end (ap);
if (!strcmp (temp, "nan"))
strcpy (temp, "null");
jo_lit (j, tag, temp);
}
void
jo_baseN (jo_t j, const char *tag, const void *src, size_t slen, uint8_t bits, const char *alphabet)
{ // base 16/32/64 binary to string
if (jo_write_check (j, tag))
return;
jo_write (j, '"');
unsigned int i = 0,
b = 0,
v = 0;
while (i < slen)
{
b += 8;
v = (v << 8) + ((uint8_t *) src)[i++];
while (b >= bits)
{
b -= bits;
jo_write (j, alphabet[(v >> b) & ((1 << bits) - 1)]);
}
}
if (b)
{ // final bits
b += 8;
v <<= 8;
b -= bits;
jo_write (j, alphabet[(v >> b) & ((1 << bits) - 1)]);
while (b)
{ // padding
while (b >= bits)
{
b -= bits;
jo_write (j, '=');
}
if (b)
b += 8;
}
}
jo_write (j, '"');
}
void
jo_datetime (jo_t j, const char *tag, time_t t)
{
if (t < 1000000000)
jo_null (j, tag);
else
{
struct tm tm;
gmtime_r (&t, &tm);
jo_stringf (j, tag, "%04d-%02d-%02dT%02d:%02d:%02dZ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
tm.tm_sec);
}
}
ssize_t
jo_strncpyd (jo_t j, void *dstv, size_t dlen, uint8_t bits, const char *alphabet)
{ // Base16/32/64 string to binary
uint8_t *dst = dstv;
if (!j || j->err || !j->parse)
return -1;
if (jo_peek (j) != '"')
return -1; // Not a string
jo_t p = jo_link (j);
jo_read (p); // skip "
int b = 0,
v = 0,
c,
ptr = 0;
while ((c = jo_read_str (p)) >= 0 && c != '=')
{
char *q = strchr (alphabet, bits < 6 ? toupper (c) : c);
if (!c || !q)
{ // Bad character
if (!c || isspace (c))
continue; // space
jo_free (&p);
return -1; // Bad
}
v = (v << bits) + (q - alphabet);
b += bits;
if (b >= 8)
{ // output byte
b -= 8;
if (dst && ptr < dlen)
dst[ptr] = (v >> b);
ptr++;
}
}
jo_free (&p);
return ptr;
}
char *
jo_strdup (jo_t j)
{ // Malloc copy of string
ssize_t len = jo_strlen (j);
if (len < 0)
return NULL;
char *str = mallocspi (len + 1);
jo_strncpy (j, str, len + 1);
return str;
}
char *
jo_strdupj (jo_t j)
{ // Malloc copy of whole JSON object
jo_t p = jo_link (j);
ssize_t start = p->ptr;
jo_skip (p);
ssize_t end = p->ptr;
if (p->err)
end = start - 1;
jo_free (&p);
while (end > start && (j->buf[end - 1] == ',' || isspace ((int) (unsigned char) (j->buf[end - 1]))))
end--;
ssize_t len = end - start;
if (len < 0)
return NULL;
char *str = mallocspi (len + 1);
memcpy (str, j->buf + j->ptr, len);
str[len] = 0;
return str;
}
void
jo_int (jo_t j, const char *tag, int64_t val)
{ // Add an integer
jo_litf (j, tag, "%lld", val);
}
void
jo_bool (jo_t j, const char *tag, int val)
{ // Add a bool (true if non zero passed)
jo_lit (j, tag, val ? "true" : "false");
}
void
jo_null (jo_t j, const char *tag)
{ // Add a null
jo_lit (j, tag, "null");
}
// Parsing
static inline int
jo_ws (jo_t j)
{ // Skip white space, and return peek at next
int c = jo_peek (j);
while (c == ' ' || c == '\t' || c == '\r' || c == '\n')
{
jo_read (j);
c = jo_peek (j);
}
return c;
}
jo_type_t
jo_here (jo_t j)
{ // Return what type of thing we are at - we are always at a value of some sort, which has a tag if we are in an object
if (!j || j->err || !j->parse)
return JO_END;
int c = jo_ws (j);
if (c < 0)
return JO_END;
if (c == '}' || c == ']')
{
if (j->tagok)
{
j->err = "Missing value";
return JO_END;
}
if (!j->level)
{
j->err = "Too many closed";
return JO_END;
}
if (c != ((j->o[(j->level - 1) / 8] & (1 << ((j->level - 1) & 7))) ? '}' : ']'))
{
j->err = "Mismatched close";
return JO_END;
}
return JO_CLOSE;
}
if (j->comma)
{
if (!j->level)
{
j->err = "Extra value at top level";
return JO_END;
}
if (c != ',')
{
j->err = "Missing comma";
return JO_END;
}
jo_read (j);
c = jo_ws (j);
j->comma = 0; // Comma consumed
}
// These are a guess of type - a robust check is done by jo_next
if (!j->tagok && j->level && (j->o[(j->level - 1) / 8] & (1 << ((j->level - 1) & 7))))
{ // We should be at a tag
if (c != '"')
{
j->err = "Missing tag";
return JO_END;
}
return JO_TAG;
}
if (c == '"')
return JO_STRING;
if (c == '{')
return JO_OBJECT;
if (c == '[')
return JO_ARRAY;
if (c == 'n')
return JO_NULL;
if (c == 't')
return JO_TRUE;
if (c == 'f')
return JO_FALSE;
if (c == '-' || (c >= '0' && c <= '9'))
return JO_NUMBER;
j->err = "Bad JSON";
return JO_END;
}
jo_type_t
jo_next (jo_t j)
{ // Move to next value, this validates what we are skipping. A tag and its value are separate
int c;
if (!j || !j->parse || j->err)
return JO_END;
switch (jo_here (j))
{
case JO_END: // End or error
break;
case JO_TAG: // Tag
jo_read (j); // "
while (jo_read_str (j) >= 0);
if (!j->err && jo_read (j) != '"')
j->err = "Missing closing quote on tag";
jo_ws (j);
if (!j->err && jo_read (j) != ':')
j->err = "Missing colon after tag";
j->tagok = 1; // We have skipped tag
break;
case JO_OBJECT:
jo_read (j); // {
if (j->level >= JO_MAX)
{
j->err = "JSON too deep";
break;
}
j->o[j->level / 8] |= (1 << (j->level & 7));
j->level++;
j->comma = 0;
j->tagok = 0;
break;
case JO_ARRAY:
jo_read (j); // ]
if (j->level >= JO_MAX)
{
j->err = "JSON too deep";
break;
}
j->o[j->level / 8] &= ~(1 << (j->level & 7));
j->level++;
j->comma = 0;
j->tagok = 0;
break;
case JO_CLOSE:
jo_read (j); // }/]
j->level--; // Was checked by jo_here()
j->comma = 1;
j->tagok = 0;
break;
case JO_STRING:
jo_read (j); // "
while (jo_read_str (j) >= 0);
if (!j->err && jo_read (j) != '"')
j->err = "Missing closing quote on string";
j->comma = 1;
j->tagok = 0;
break;
case JO_NUMBER:
if (jo_peek (j) == '-')
jo_read (j); // minus
if ((c = jo_peek (j)) == '0')
jo_read (j); // just zero
else if (c >= '1' && c <= '9')
while ((c = jo_peek (j)) >= '0' && c <= '9')
jo_read (j); // int
if (jo_peek (j) == '.')
{ // real
jo_read (j);
if ((c = jo_peek (j)) < '0' || c > '9')
j->err = "Bad real, must be digits after decimal point";
else
while ((c = jo_peek (j)) >= '0' && c <= '9')
jo_read (j); // frac
}
if ((c = jo_peek (j)) == 'e' || c == 'E')
{ // exp
jo_read (j);
if ((c = jo_peek (j)) == '-' || c == '+')
jo_read (j);
if ((c = jo_peek (j)) < '0' || c > '9')
j->err = "Bad exp";
else
while ((c = jo_peek (j)) >= '0' && c <= '9')
jo_read (j); // exp
}
j->comma = 1;
j->tagok = 0;
break;
case JO_NULL:
if (jo_read (j) != 'n' || //
jo_read (j) != 'u' || //
jo_read (j) != 'l' || //
jo_read (j) != 'l')
j->err = "Misspelled null";
j->comma = 1;
j->tagok = 0;
break;