-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlitejson.c
1233 lines (975 loc) · 30.1 KB
/
litejson.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "litejson.h"
//
// common - private
//
/// max length of json_error.message field
#define LJ_ERROR_CHARMAX 512
/// internally-used base size for automatically extendable C
/// string buffers
#define LJ_STRINGOPS_BUFSTEP 20
/// max length of ljftoa C string buffer
#define LJ_STRINGOPS_NUMMAX 10
/// const parameter that is used as a "sign" to lj_substring_until to
/// use a common set of JSON token delimiters as its border
#define LJ_STRINGOPS_JSONTOK '\r'
/// JSON document generation soft tab size
#define LJ_STRINGOPS_TABSIZE 3
#ifdef LJ_DEBUG_ALLOW_COLORS
#define LJ_PRINTF_ADDRESS "\033[93m"
#define LJ_PRINTF_GREEN "\033[92m"
#define LJ_PRINTF_DEBUG "\033[96m"
#define LJ_PRINTF_RESET "\033[0m"
#else
#define LJ_PRINTF_ADDRESS ""
#define LJ_PRINTF_GREEN ""
#define LJ_PRINTF_DEBUG ""
#define LJ_PRINTF_RESET ""
#endif
struct json_value_s {
// value parent container, NULL if this is the root
// one
json_value_ref parent;
// stored value type
json_type_t type;
// key/label if stored in an object
char* key;
// string value
char* strV;
// numeric value
json_number_t numV;
// first child item (container-only)
json_value_ref child;
// next item
json_value_ref next;
};
void ljprintf(const char* fn, const json_index_t line, const char* fc,
const char* msgF, ...) {
#ifdef DEBUG
va_list vl;
va_start(vl, msgF);
fprintf(stderr, "%s[%s:%u %s]%s ", LJ_PRINTF_DEBUG,
fn, line, fc, LJ_PRINTF_RESET);
vfprintf(stderr, msgF, vl);
fprintf(stderr, "%c", '\n');
va_end(vl);
#else
(void)(fn);
(void)(line);
(void)(fc);
(void)(msgF);
#endif
}
///
/// internally-used debug printf function - release builds do not produce
/// any debug output
///
#define ljprintf(...) ljprintf(__FILE__, __LINE__, __FUNCTION__, \
__VA_ARGS__)
/// zero malloc
void* ljmalloc(const json_index_t size) {
if (size < 1)
return NULL; // obv
void* result = malloc(size);
bzero(result, size);
return result;
}
/// convenience wrapper for zero malloc-ing new struct instances
#define ljmalloc_s(nm) ljmalloc(sizeof(struct nm))
//
// json_parse - private
//
///
/// creates a new instance of the json_error structure with the provided
/// data being saved
///
json_error json_error_make(const json_index_t line,
const json_index_t character,
const char* msgF,
...) {
json_error result = { (msgF ? true : false), line, character, NULL };
if (msgF) {
// merge the specified error message template with its parameters
// into a single string
char* msg = calloc(LJ_ERROR_CHARMAX, sizeof(char));
va_list vl;
va_start(vl, msgF);
vsprintf(msg, msgF, vl);
va_end(vl);
result.message = msg;
}
return result;
}
/// sets *p1 to p2 if p1 is a valid pointer
#define LJ_IF_NOT_NULL(p1, p2) \
{ \
if (p1) \
(*p1) = p2; \
}
/// cleans up json_parse-related vars and throws a parsing error
#define LJ_ERROR(...) \
{ \
if (value) \
json_value_release_tree(value); \
LJ_IF_NOT_NULL(errorP, json_error_make(lineC, charC, __VA_ARGS__)) \
return NULL; \
}
#define LJ_IS_CONTAINER(obj) (obj->type == JSON_TYPE_ARRAY || \
obj->type == JSON_TYPE_OBJECT)
/// checks if the specified character is a JSON delimiter token
#define LJ_IS_JSONTOK(current) \
(isspace(current) != 0 || current == ']' || current == '}' || current == ',')
/// [json_parse only] convenience macro to setup a new empty json_value_ref
#define LJ_INIT_EMPTY_OBJ(newObj) \
json_value_ref newObj = ljmalloc_s(json_value_s); \
\
if (futureKey) { \
newObj->key = futureKey; \
futureKey = NULL; \
} \
\
if (!root) \
root = newObj;
#define LJ_ADAPT_OBJ_PARENT(newObj) \
{ \
if (value) { \
if (LJ_IS_CONTAINER(value)) { \
newObj->parent = value; \
if (value->child) { \
json_value_ref lastChild = json_value_get_last(value); \
lastChild->next = newObj; \
} else \
value->child = newObj; \
} else { \
value->next = newObj; \
newObj->parent = value->parent; \
} \
} \
}
///
/// [json_parse] if we are a child item inside a container and we encounter
/// a token meant to close that container, we need to jump to our parent
/// container first and only then continue with closing it (jumping to its
/// parent)
///
#define LJ_JUMP_TO_CHILDS_PARENT_MULTI(value, pType) \
{ \
if (value && value->type != pType && \
value->parent && value->parent->type == pType) { \
\
ljprintf("additional level up from %p to %p, type %u -> %u", \
value, value->parent, value->type, pType); \
value = value->parent; \
} \
}
///
/// [json_parse] adjust the state machine after jumping to the container's
/// parent
///
#define LJ_CLOSE_AND_JUMP_TO_PARENT(value) \
{ \
if (value->parent) { \
ljprintf("%s going up one level from %p to %p %s", \
LJ_PRINTF_GREEN, value, value->parent, LJ_PRINTF_RESET); \
\
value = value->parent; \
\
if (value->type == JSON_TYPE_OBJECT) \
state = JSON_STATE_KEY; \
else if (value->type == JSON_TYPE_ARRAY) \
state = JSON_STATE_ARRAY; \
else \
state = JSON_STATE_KEY; \
} else { \
ljprintf("de facto end of file reached, saying goodbyes and finishing up"); \
break; \
} \
}
/// internally-used parsing state machine
typedef enum {
JSON_STATE_OUTSIDE = 0,
JSON_STATE_KEY = 90,
JSON_STATE_VALUE = 91,
JSON_STATE_ARRAY = 80
} json_parse_state_t;
///
/// makes a substring at *resultP and returns its length. delim1 and delim2
/// are parameters signifying the stop characters to look for while going through
/// the main string. If respectQuotes is specified, then those stop characters
/// are ignored when wrapped around in doublequotes
///
json_index_t lj_substring_until(const char* input, const char delim1,
const char delim2, char** resultP,
const bool respectQuotes) {
if (!input || strlen(input) < 1 || delim1 == '\0') {
// need to have at least one delimiter specified
ljprintf("input = \"%s\", while delim1 = %d, cannot continue",
input, (int8_t)delim1);
return 0;
} else if (input[0] == delim1 || input[0] == delim2)
input++; // chop off right to the start
// automatically extendable buffer
json_index_t resultSize = LJ_STRINGOPS_BUFSTEP;
json_index_t resultLength = 0;
char* result = calloc(resultSize, sizeof(char));
// if true, then we were preceded by a '\\'
bool insideEscape = false;
// if true, we are in double quotes (ignored without respectQuotes)
bool insideQuotes = false;
for (json_index_t index = 0; index < strlen(input); index++) {
char current = input[index];
// extend the buffer first if necessary
if ((resultLength + 2) >= resultSize) {
ljprintf("buffer limit reached (size = %u), result = \"%s\"",
resultSize, result);
resultSize += LJ_STRINGOPS_BUFSTEP;
result = realloc(result, sizeof(char) * resultSize);
}
if (insideEscape) {
// escape the character requested
switch (current) {
case 't': {
current = '\t';
break;
}
case 'n': {
current = '\n';
break;
}
case 'r': {
current = '\r';
break;
}
default:
break;
}
} else if (current == '\\') {
insideEscape = true;
continue;
} else if (current == '"' && respectQuotes)
insideQuotes = !insideQuotes;
if (!insideQuotes) {
if (current == delim1 || current == delim2)
break; // found 'em
else if (delim1 == LJ_STRINGOPS_JSONTOK &&
LJ_IS_JSONTOK(current))
break;
}
result[resultLength++] = current;
result[resultLength] = '\0';
insideEscape = false;
}
if (resultP)
(*resultP) = result;
else
free(result); // avoid memory leak
return resultLength;
}
///
/// strips all isspace() conforming characters from the specified C string's
/// ending
///
void lj_substring_strip_right(char* input) {
if (!input)
return;
json_index_t length = strlen(input);
if (length < 1)
return;
while (length >= 1) {
length--;
if (isspace(input[length]) == 0)
break; // end space search
input[length] = '\0';
}
}
/// verifies if the specified C string contains a stringified number
bool ljisdigit_str(const char* input) {
if (!input)
return false; // nothing to look for
for (json_index_t i = 0; i < strlen(input); i++) {
if (isdigit(input[i]) == 0 && input[i] != '-' && input[i] != '+' && input[i] != '.')
return false;
}
return true;
}
/// C string -> double
json_number_t ljatof(const char* input) {
if (!ljisdigit_str(input))
return 0.0;
json_number_t result = 0.0;
sscanf(input, "%lf", &result);
return result;
}
//
// json_parse - public
//
void json_value_dump_tree(json_value_ref value, const json_index_t offset) {
if (!value)
fprintf(stderr, "(null value)\n");
else {
// type out the correct amount of spaces
for (json_index_t i = 0; i < offset; i++)
fprintf(stderr, "%c", ' ');
fprintf(stderr, "%s%p%s ", LJ_PRINTF_ADDRESS, value, LJ_PRINTF_RESET);
if (value->key)
fprintf(stderr, "key = \"%s\", ", value->key);
else
fprintf(stderr, "no key, ");
fprintf(stderr, "type = %u, container = %s, strV = \"%s\", numV = %f, parent = %p\n",
value->type, LJ_IS_CONTAINER(value) ? "true" : "false", value->strV, value->numV,
value->parent);
if (value->child)
json_value_dump_tree(value->child, offset + 1);
if (value->next)
json_value_dump_tree(value->next, offset);
}
}
json_value_ref json_parse(const char* input, json_error* errorP) {
if (!input || strlen(input) < 1) {
// don't bother parsing empty strings
LJ_IF_NOT_NULL(errorP, json_error_make(0, 0, "NULL or empty string provided as input"));
return NULL;
}
// success boilerplate saved for the future
LJ_IF_NOT_NULL(errorP, json_error_make(0, 0, NULL))
// for detailed parsing error reporting
json_index_t lineC = 1;
json_index_t charC = 0;
// state machine
json_parse_state_t state = JSON_STATE_OUTSIDE;
// current value object
json_value_ref value = NULL;
// root container object
json_value_ref root = NULL;
// key that might be set for the next found value
char* futureKey = NULL;
for (json_index_t index = 0; index < strlen(input); index++) {
const char current = input[index];
charC++;
// skip all spaces as we aren't interested in them
if (isspace(current) != 0) {
if (current == '\n') {
// update line/character counters accordingly
lineC++;
charC = 0;
}
continue;
}
ljprintf("current = '%c', index = %u", current, index);
if (state != JSON_STATE_KEY) {
if (current == '{') {
// looks like we are going deeper and are starting an object
ljprintf("handled: new object {} (state = %u)", state);
LJ_INIT_EMPTY_OBJ(newObj)
newObj->type = JSON_TYPE_OBJECT;
newObj->parent = value;
// adapt parent attributes correctly
LJ_ADAPT_OBJ_PARENT(newObj)
value = newObj;
ljprintf("state change -> key (new object)");
state = JSON_STATE_KEY;
ljprintf("%s enter new object from %p to %p %s",
LJ_PRINTF_GREEN, value->parent, value, LJ_PRINTF_RESET);
continue;
} else if (current == '[') {
ljprintf("handled: new array [] (state = %u)", state);
LJ_INIT_EMPTY_OBJ(newObj)
newObj->type = JSON_TYPE_ARRAY;
// adapt parent attributes correctly
LJ_ADAPT_OBJ_PARENT(newObj)
value = newObj;
ljprintf("state change -> array (new array)");
state = JSON_STATE_ARRAY;
ljprintf("%s enter new array from %p to %p %s",
LJ_PRINTF_GREEN, value->parent, value, LJ_PRINTF_RESET);
continue;
}
}
if (state == JSON_STATE_OUTSIDE) {
ljprintf("handled: outside");
if (current == '"') {
// string, probably
char* strV = NULL;
json_index_t strVLength = lj_substring_until(input + index, '"', '\0', &strV, false);
index += strVLength + 1;
// make an empty string if it is one
if (!strV)
strV = ljmalloc(sizeof(char));
ljprintf("found a string, strV = \"%s\" (length = %u)", strV, strVLength);
LJ_INIT_EMPTY_OBJ(newObj)
newObj->type = JSON_TYPE_STRING;
newObj->strV = strV;
newObj->numV = ljatof(strV);
// adapt parent attributes correctly
LJ_ADAPT_OBJ_PARENT(newObj)
value = newObj;
} else {
// need to read till the end first
char* token = NULL;
json_index_t tokenLength = lj_substring_until(input + index, LJ_STRINGOPS_JSONTOK, '\0', &token, true);
index += tokenLength + 1;
ljprintf("found a (yet) undefined token = \"%s\", length = %u", token, tokenLength);
LJ_INIT_EMPTY_OBJ(newObj)
if (strcmp(token, "null") == 0) {
// a null value, add a placeholder string value and
// move on
newObj->strV = ljmalloc(sizeof(char));
free(token);
} else if (ljisdigit_str(token)) {
// a numeric value
newObj->type = JSON_TYPE_NUMBER;
newObj->strV = token;
newObj->numV = ljatof(token);
} else if (strcmp(token, "true") == 0 ||
strcmp(token, "false") == 0) {
// a boolean value
newObj->type = JSON_TYPE_BOOLEAN;
newObj->strV = token;
newObj->numV = (strcmp(token, "true") == 0);
} else {
free(token); // quick clean up
json_value_release(newObj);
LJ_ERROR("Expected a valid JSON value, got '%c' token", current);
}
// adapt parent attributes correctly
LJ_ADAPT_OBJ_PARENT(newObj)
value = newObj;
continue;
}
}
if (state == JSON_STATE_KEY) {
ljprintf("handled: object state -> key");
// we are inside {} (object) - looking for keys
if (current == '"') {
ljprintf("key found at %u", index);
char* key = NULL;
json_index_t keyLength = lj_substring_until(input + index, '"', '\0', &key, false);
index += keyLength + 1;
futureKey = key;
ljprintf("futureKey = \"%s\", length = %u", futureKey, keyLength);
continue;
} else if (current == ':') {
ljprintf("key delimiter found at %u", index);
// need the key to be found first
if (!futureKey)
LJ_ERROR("Expected key, got '%c' instead", current)
ljprintf("state change -> value");
state = JSON_STATE_VALUE;
} else if (current == ',') {
ljprintf("comma jump");
continue;
} else if (current == '}') {
// the end of this object
ljprintf("reached } at %u", current);
// if we are a child item, we need to first reach out parent
// container to close it
LJ_JUMP_TO_CHILDS_PARENT_MULTI(value, JSON_TYPE_OBJECT)
if (!value)
LJ_ERROR("Stray object end token")
LJ_CLOSE_AND_JUMP_TO_PARENT(value)
ljprintf("state change -> %u", state);
} else
LJ_ERROR("Expected key, got '%c' instead", current);
} else if (state == JSON_STATE_VALUE || state == JSON_STATE_ARRAY) {
// found value, need to get its raw string and then recursively parse
// it
ljprintf("handled: object state -> value");
const char ending = (state == JSON_STATE_ARRAY) ? ']' : '}';
ljprintf("recognized ending - '%c'", ending);
// handle array ending correctly
if (state == JSON_STATE_ARRAY) {
if (current == ',') {
// array comma jump
ljprintf("array comma jump");
continue;
} else if (current == ']') {
ljprintf("reached the closing bracket ] at %u", index);
LJ_JUMP_TO_CHILDS_PARENT_MULTI(value, JSON_TYPE_ARRAY)
if (!value)
LJ_ERROR("Stray array end token")
LJ_CLOSE_AND_JUMP_TO_PARENT(value)
ljprintf("state change -> %u", state);
continue;
}
}
char* valueRaw = NULL;
json_index_t valueRawLength = lj_substring_until(input + index, ',', ending, &valueRaw, false);
if (!valueRaw)
LJ_ERROR("Expected value, got nothing")
ljprintf("found valueRaw = \"%s\" (%u) at %u", valueRaw, valueRawLength, index);
index += valueRawLength - 1;
ljprintf("index updated to %u -> '%c'", index, input[index]);
// strip spaces from the ending
lj_substring_strip_right(valueRaw);
// time of recursive parsing
json_error adaptedError;
json_value_ref newObj = json_parse(valueRaw, &adaptedError);
// avoid memory leaks and clean up a bit
free(valueRaw);
if (adaptedError.fail) {
// first adapt error report values to proper ones
adaptedError.line += lineC - 1;
adaptedError.character += charC;
// now we have to fail the parent too
json_value_release_tree(value);
LJ_IF_NOT_NULL(errorP, adaptedError)
return NULL;
}
// otherwise, we got ourselves a new object
ljprintf("new object <%p>, key = \"%s\", type = %u", newObj, futureKey, newObj->type);
if (futureKey) {
// set the object's label appropriately
newObj->key = futureKey;
futureKey = NULL;
}
// adapt object parent
LJ_ADAPT_OBJ_PARENT(newObj)
value = newObj;
if (value->parent && value->parent->type == JSON_TYPE_OBJECT) {
ljprintf("state changed -> key");
state = JSON_STATE_KEY;
}
}
}
return root;
}
// undef all json_parse-related macros so that they won't be used in
// other methods by accident
#undef LJ_CLOSE_AND_JUMP_TO_PARENT
#undef LJ_JUMP_TO_CHILDS_PARENT_MULTI
#undef LJ_ADAPT_OBJ_PARENT
#undef LJ_INIT_EMPTY_OBJ
#undef LJ_ERROR
//
// json_value_ref API - private
//
json_value_ref json_value_init(const json_type_t type) {
json_value_ref result = ljmalloc_s(json_value_s);
result->type = type;
return result;
}
#define LJ_CLEAN_PREVIOUS_VALUE(value) \
{ \
free(value->strV); \
value->strV = NULL; \
\
if (LJ_IS_CONTAINER(value)) { \
json_value_release_tree(value->child); \
value->child = NULL; \
} \
}
/// double -> C string
char* ljftoa(const json_number_t input) {
char* result = calloc(LJ_STRINGOPS_NUMMAX, sizeof(char));
// TODO: optimize
int32_t forceRound = (int32_t)input;
// if there are no numbers after the comma, read it as int32_t
if ((json_number_t)(forceRound) == input)
sprintf(result, "%d", forceRound);
else
sprintf(result, "%lf", input);
return result;
}
json_value_ref json_value_get_neighbor(json_value_ref base,
const json_index_t index,
const bool justCount,
json_index_t* countP) {
json_index_t count = 0;
json_value_ref current = base;
while (current) {
if (!justCount && count == index)
return current; // the item we are looking for
count++;
if (justCount && !current->next)
break; // return the last item instead
current = current->next;
}
// save count
LJ_IF_NOT_NULL(countP, count)
return current;
}
json_value_ref json_value_find_by_key(json_value_ref base,
const char* key,
json_value_ref* previousP) {
if (!base || !key)
return NULL; // cannot start with NULL
json_value_ref current = base;
json_value_ref previous = NULL;
while (current) {
if (current->key && strcmp(current->key, key) == 0) {
// found the one
LJ_IF_NOT_NULL(previousP, previous)
return current;
}
previous = current;
current = current->next;
}
// not found
return NULL;
}
char* lj_unescape_str(const char* input) {
if (!input)
return NULL;
// TODO: optimize
json_index_t resultSize = strlen(input) * 2 + 2;
json_index_t resultLength = 1;
char* result = calloc(resultSize, sizeof(char));
// begin the resulting string with a double-quote
result[0] = '"';
for (json_index_t i = 0; i < strlen(input); i++) {
char current = input[i];
bool doEscape = false;
switch (current) {
case '\\':
case '"': {
doEscape = true;
break;
}
case '\t': {
current = 't';
doEscape = true;
break;
}
case '\r': {
current = 'r';
doEscape = true;
break;
}
case '\n': {
current = 'n';
doEscape = true;
break;
}
default:
break;
}
if (doEscape)
result[resultLength++] = '\\';
result[resultLength++] = current;
}
// don't forget to close the string
result[resultLength++] = '"';
return result;
}
#define LJ_AUTOREALLOC_IF_NEEDED(condition, result, resultLength, resultSize) \
{ \
if ((resultLength + condition + 2) >= resultSize) { \
resultSize += condition + LJ_STRINGOPS_BUFSTEP; \
result = realloc(result, sizeof(char) * resultSize); \
} \
}
char* json_value_make_string_repr(const json_value_ref root,
json_index_t* lengthP,
const bool humanReadable,
const json_index_t baseSpaceCount) {
// prepare resulting buffer
json_index_t resultSize = LJ_STRINGOPS_BUFSTEP;
json_index_t resultLength = 0;
char* result = calloc(resultSize, sizeof(char));
// used in humanReadable mode - space count
json_index_t spaceCount = baseSpaceCount;
if (LJ_IS_CONTAINER(root)) {
// add the container beginning token first
result[resultLength++] = (root->type == JSON_TYPE_ARRAY) ? '[' : '{';
LJ_AUTOREALLOC_IF_NEEDED(2, result, resultLength, resultSize)
spaceCount += LJ_STRINGOPS_TABSIZE;
// add a new line before listing container items
if (humanReadable)
result[resultLength++] = '\n';
// iterate through each child and add its stringified form
json_value_ref child = root->child;
while (child) {
if (humanReadable) {
// prepend the necessary spaces
LJ_AUTOREALLOC_IF_NEEDED(spaceCount + 1, result, resultLength, resultSize)
for (json_index_t sc = 0; sc < spaceCount; sc++)
result[resultLength++] = ' ';
}
if (root->type == JSON_TYPE_OBJECT) {
// need not to forget to add the keys
char* escapedKey = lj_unescape_str(child->key);
json_index_t escapedKeyLength = escapedKey ? strlen(escapedKey) : 0;
ljprintf("escapedKey = %s", escapedKey);
// allocate buffer space first
LJ_AUTOREALLOC_IF_NEEDED(escapedKeyLength + 3, result, resultLength, resultSize)
if (escapedKeyLength > 0) {
// add the converted quoted key
strcat(result, escapedKey);
resultLength += escapedKeyLength;
result[resultLength++] = ':';
if (humanReadable)
result[resultLength++] = ' ';
}
}
// convert the child object into a document representation too
json_index_t reprLength = 0;
char* repr = json_value_make_string_repr(child, &reprLength, humanReadable, spaceCount);
if (reprLength > 0) {
// add the representation to our final string
LJ_AUTOREALLOC_IF_NEEDED(reprLength + 3, result, resultLength, resultSize)
strcat(result, repr);
resultLength += reprLength;
}
child = child->next;
// don't forget to add a comma seperating other values if there
// are many of them
if (child)
result[resultLength++] = ',';
// new line on each child item
if (humanReadable)
result[resultLength++] = '\n';
}
spaceCount -= LJ_STRINGOPS_TABSIZE;
// end the container too
LJ_AUTOREALLOC_IF_NEEDED(spaceCount + 1, result, resultLength, resultSize)
if (humanReadable) {
// prepend the last few spaces before container end
for (json_index_t sc = 0; sc < spaceCount; sc++)
result[resultLength++] = ' ';
}
result[resultLength++] = (root->type == JSON_TYPE_ARRAY) ? ']' : '}';
} else {
switch (root->type) {
case JSON_TYPE_STRING: {
// use its own quoted representation instead
free(result);
result = lj_unescape_str(root->strV);
resultLength = result ? strlen(result) : 0;
break;
}
case JSON_TYPE_NUMBER:
case JSON_TYPE_BOOLEAN: {
// use its strV variant
free(result);
result = strdup(root->strV);
resultLength = strlen(result);
break;
}
default: {
// use null string
strcpy(result, "null");
resultLength = strlen(result);
break;
}
}
}
LJ_IF_NOT_NULL(lengthP, resultLength)
return result;
}
//
// json_value_ref API - public
//
json_value_ref json_value_init_string(const char* str) {
if (!str) {
ljprintf("NULL string provided as input, cannot continue");
return NULL;
}
json_value_ref result = json_value_init(JSON_TYPE_STRING);
json_value_set_string(result, str);
return result;
}
json_value_ref json_value_init_number(const json_number_t num) {
json_value_ref result = json_value_init(JSON_TYPE_NUMBER);
json_value_set_number(result, num);
return result;
}
json_value_ref json_value_init_boolean(const bool bv) {
json_value_ref result = json_value_init(JSON_TYPE_BOOLEAN);
json_value_set_boolean(result, bv);
return result;
}
json_value_ref json_value_init_null() {
json_value_ref result = json_value_init(JSON_TYPE_NULL);
return result;
}
json_value_ref json_value_init_object() {
json_value_ref result = json_value_init(JSON_TYPE_OBJECT);
return result;
}
json_value_ref json_value_init_array() {
json_value_ref result = json_value_init(JSON_TYPE_ARRAY);
return result;
}