forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.c
1098 lines (894 loc) · 21 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
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) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/sys/__assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/util.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <zephyr/types.h>
#include <zephyr/data/json.h>
struct json_obj_key_value {
const char *key;
size_t key_len;
struct json_token value;
};
static bool lexer_consume(struct json_lexer *lex, struct json_token *tok,
enum json_tokens empty_token)
{
if (lex->tok.type == empty_token) {
return false;
}
*tok = lex->tok;
lex->tok.type = empty_token;
return true;
}
static bool lexer_next(struct json_lexer *lex, struct json_token *tok)
{
while (lex->state) {
if (lexer_consume(lex, tok, JSON_TOK_NONE)) {
return true;
}
lex->state = lex->state(lex);
}
return lexer_consume(lex, tok, JSON_TOK_EOF);
}
static void *lexer_json(struct json_lexer *lex);
static void emit(struct json_lexer *lex, enum json_tokens token)
{
lex->tok.type = token;
lex->tok.start = lex->start;
lex->tok.end = lex->pos;
lex->start = lex->pos;
}
static int next(struct json_lexer *lex)
{
if (lex->pos >= lex->end) {
lex->pos = lex->end + 1;
return '\0';
}
return *lex->pos++;
}
static void ignore(struct json_lexer *lex)
{
lex->start = lex->pos;
}
static void backup(struct json_lexer *lex)
{
lex->pos--;
}
static int peek(struct json_lexer *lex)
{
int chr = next(lex);
backup(lex);
return chr;
}
static void *lexer_string(struct json_lexer *lex)
{
ignore(lex);
while (true) {
int chr = next(lex);
if (chr == '\0') {
emit(lex, JSON_TOK_ERROR);
return NULL;
}
if (chr == '\\') {
switch (next(lex)) {
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
continue;
case 'u':
if (!isxdigit(next(lex))) {
goto error;
}
if (!isxdigit(next(lex))) {
goto error;
}
if (!isxdigit(next(lex))) {
goto error;
}
if (!isxdigit(next(lex))) {
goto error;
}
break;
default:
goto error;
}
}
if (chr == '"') {
backup(lex);
emit(lex, JSON_TOK_STRING);
next(lex);
ignore(lex);
return lexer_json;
}
}
error:
emit(lex, JSON_TOK_ERROR);
return NULL;
}
static int accept_run(struct json_lexer *lex, const char *run)
{
for (; *run; run++) {
if (next(lex) != *run) {
return -EINVAL;
}
}
return 0;
}
static void *lexer_boolean(struct json_lexer *lex)
{
backup(lex);
switch (next(lex)) {
case 't':
if (!accept_run(lex, "rue")) {
emit(lex, JSON_TOK_TRUE);
return lexer_json;
}
break;
case 'f':
if (!accept_run(lex, "alse")) {
emit(lex, JSON_TOK_FALSE);
return lexer_json;
}
break;
}
emit(lex, JSON_TOK_ERROR);
return NULL;
}
static void *lexer_null(struct json_lexer *lex)
{
if (accept_run(lex, "ull") < 0) {
emit(lex, JSON_TOK_ERROR);
return NULL;
}
emit(lex, JSON_TOK_NULL);
return lexer_json;
}
static void *lexer_number(struct json_lexer *lex)
{
while (true) {
int chr = next(lex);
if (isdigit(chr) || chr == '.') {
continue;
}
backup(lex);
emit(lex, JSON_TOK_NUMBER);
return lexer_json;
}
}
static void *lexer_json(struct json_lexer *lex)
{
while (true) {
int chr = next(lex);
switch (chr) {
case '\0':
emit(lex, JSON_TOK_EOF);
return NULL;
case '}':
case '{':
case '[':
case ']':
case ',':
case ':':
emit(lex, (enum json_tokens)chr);
return lexer_json;
case '"':
return lexer_string;
case 'n':
return lexer_null;
case 't':
case 'f':
return lexer_boolean;
case '-':
if (isdigit(peek(lex))) {
return lexer_number;
}
__fallthrough;
default:
if (isspace(chr)) {
ignore(lex);
continue;
}
if (isdigit(chr)) {
return lexer_number;
}
emit(lex, JSON_TOK_ERROR);
return NULL;
}
}
}
static void lexer_init(struct json_lexer *lex, char *data, size_t len)
{
lex->state = lexer_json;
lex->start = data;
lex->pos = data;
lex->end = data + len;
lex->tok.type = JSON_TOK_NONE;
}
static int obj_init(struct json_obj *json, char *data, size_t len)
{
struct json_token tok;
lexer_init(&json->lex, data, len);
if (!lexer_next(&json->lex, &tok)) {
return -EINVAL;
}
if (tok.type != JSON_TOK_OBJECT_START) {
return -EINVAL;
}
return 0;
}
static int arr_init(struct json_obj *json, char *data, size_t len)
{
struct json_token tok;
lexer_init(&json->lex, data, len);
if (!lexer_next(&json->lex, &tok)) {
return -EINVAL;
}
if (tok.type != JSON_TOK_ARRAY_START) {
return -EINVAL;
}
return 0;
}
static int element_token(enum json_tokens token)
{
switch (token) {
case JSON_TOK_OBJECT_START:
case JSON_TOK_ARRAY_START:
case JSON_TOK_STRING:
case JSON_TOK_NUMBER:
case JSON_TOK_FLOAT:
case JSON_TOK_OPAQUE:
case JSON_TOK_OBJ_ARRAY:
case JSON_TOK_TRUE:
case JSON_TOK_FALSE:
return 0;
default:
return -EINVAL;
}
}
static int obj_next(struct json_obj *json,
struct json_obj_key_value *kv)
{
struct json_token tok;
if (!lexer_next(&json->lex, &tok)) {
return -EINVAL;
}
/* Match end of object or next key */
switch (tok.type) {
case JSON_TOK_OBJECT_END:
kv->key = NULL;
kv->key_len = 0;
kv->value = tok;
return 0;
case JSON_TOK_COMMA:
if (!lexer_next(&json->lex, &tok)) {
return -EINVAL;
}
if (tok.type != JSON_TOK_STRING) {
return -EINVAL;
}
__fallthrough;
case JSON_TOK_STRING:
kv->key = tok.start;
kv->key_len = (size_t)(tok.end - tok.start);
break;
default:
return -EINVAL;
}
/* Match : after key */
if (!lexer_next(&json->lex, &tok)) {
return -EINVAL;
}
if (tok.type != JSON_TOK_COLON) {
return -EINVAL;
}
/* Match value */
if (!lexer_next(&json->lex, &kv->value)) {
return -EINVAL;
}
return element_token(kv->value.type);
}
static int arr_next(struct json_obj *json, struct json_token *value)
{
if (!lexer_next(&json->lex, value)) {
return -EINVAL;
}
if (value->type == JSON_TOK_ARRAY_END) {
return 0;
}
if (value->type == JSON_TOK_COMMA) {
if (!lexer_next(&json->lex, value)) {
return -EINVAL;
}
}
return element_token(value->type);
}
static int decode_num(const struct json_token *token, int32_t *num)
{
/* FIXME: strtod() is not available in newlib/minimal libc,
* so using strtol() here.
*/
char *endptr;
char prev_end;
prev_end = *token->end;
*token->end = '\0';
errno = 0;
*num = strtol(token->start, &endptr, 10);
*token->end = prev_end;
if (errno != 0) {
return -errno;
}
if (endptr != token->end) {
return -EINVAL;
}
return 0;
}
static bool equivalent_types(enum json_tokens type1, enum json_tokens type2)
{
if (type1 == JSON_TOK_TRUE || type1 == JSON_TOK_FALSE) {
return type2 == JSON_TOK_TRUE || type2 == JSON_TOK_FALSE;
}
if (type1 == JSON_TOK_NUMBER && type2 == JSON_TOK_FLOAT) {
return true;
}
if (type1 == JSON_TOK_STRING && type2 == JSON_TOK_OPAQUE) {
return true;
}
if (type1 == JSON_TOK_ARRAY_START && type2 == JSON_TOK_OBJ_ARRAY) {
return true;
}
return type1 == type2;
}
static int obj_parse(struct json_obj *obj,
const struct json_obj_descr *descr, size_t descr_len,
void *val);
static int arr_parse(struct json_obj *obj,
const struct json_obj_descr *elem_descr,
size_t max_elements, void *field, void *val);
static int arr_data_parse(struct json_obj *obj, struct json_obj_token *val);
static int decode_value(struct json_obj *obj,
const struct json_obj_descr *descr,
struct json_token *value, void *field, void *val)
{
if (!equivalent_types(value->type, descr->type)) {
return -EINVAL;
}
switch (descr->type) {
case JSON_TOK_OBJECT_START:
return obj_parse(obj, descr->object.sub_descr,
descr->object.sub_descr_len,
field);
case JSON_TOK_ARRAY_START:
return arr_parse(obj, descr->array.element_descr,
descr->array.n_elements, field, val);
case JSON_TOK_OBJ_ARRAY: {
struct json_obj_token *obj_token = field;
obj_token->start = value->start;
return arr_data_parse(obj, obj_token);
}
case JSON_TOK_FALSE:
case JSON_TOK_TRUE: {
bool *v = field;
*v = value->type == JSON_TOK_TRUE;
return 0;
}
case JSON_TOK_NUMBER: {
int32_t *num = field;
return decode_num(value, num);
}
case JSON_TOK_OPAQUE:
case JSON_TOK_FLOAT: {
struct json_obj_token *obj_token = field;
obj_token->start = value->start;
obj_token->length = value->end - value->start;
return 0;
}
case JSON_TOK_STRING: {
char **str = field;
*value->end = '\0';
*str = value->start;
return 0;
}
default:
return -EINVAL;
}
}
static ptrdiff_t get_elem_size(const struct json_obj_descr *descr)
{
switch (descr->type) {
case JSON_TOK_NUMBER:
return sizeof(int32_t);
case JSON_TOK_OPAQUE:
case JSON_TOK_FLOAT:
case JSON_TOK_OBJ_ARRAY:
return sizeof(struct json_obj_token);
case JSON_TOK_STRING:
return sizeof(char *);
case JSON_TOK_TRUE:
case JSON_TOK_FALSE:
return sizeof(bool);
case JSON_TOK_ARRAY_START:
return descr->array.n_elements * get_elem_size(descr->array.element_descr);
case JSON_TOK_OBJECT_START: {
ptrdiff_t total = 0;
size_t i;
for (i = 0; i < descr->object.sub_descr_len; i++) {
ptrdiff_t s = get_elem_size(&descr->object.sub_descr[i]);
total += ROUND_UP(s, 1 << descr->align_shift);
}
return total;
}
default:
return -EINVAL;
}
}
static int arr_parse(struct json_obj *obj,
const struct json_obj_descr *elem_descr,
size_t max_elements, void *field, void *val)
{
ptrdiff_t elem_size = get_elem_size(elem_descr);
void *last_elem = (char *)field + elem_size * max_elements;
size_t *elements = NULL;
struct json_token value;
if (val) {
elements = (size_t *)((char *)val + elem_descr->offset);
}
__ASSERT_NO_MSG(elem_size > 0);
if (elements) {
*elements = 0;
}
while (!arr_next(obj, &value)) {
if (value.type == JSON_TOK_ARRAY_END) {
return 0;
}
if (field == last_elem) {
return -ENOSPC;
}
if (decode_value(obj, elem_descr, &value, field, NULL) < 0) {
return -EINVAL;
}
if (elements) {
(*elements)++;
}
field = (char *)field + elem_size;
}
return -EINVAL;
}
static int arr_data_parse(struct json_obj *obj, struct json_obj_token *val)
{
bool string_state = false;
int array_in_array = 1;
/* Init length to zero */
val->length = 0;
while (obj->lex.pos != obj->lex.end) {
if (string_state) {
if (*obj->lex.pos == JSON_TOK_STRING) {
string_state = false;
}
} else {
if (*obj->lex.pos == JSON_TOK_ARRAY_END) {
array_in_array--;
if (array_in_array == 0) {
/* Set array data length + 1 object end */
val->length = obj->lex.pos - val->start + 1;
/* Init Lexer that Object Parse can be finished properly */
obj->lex.state = lexer_json;
/* Move position to before array end */
obj->lex.pos--;
obj->lex.tok.end = obj->lex.pos;
obj->lex.tok.start = val->start;
obj->lex.tok.type = JSON_TOK_NONE;
return 0;
}
} else if (*obj->lex.pos == JSON_TOK_STRING) {
string_state = true;
} else if (*obj->lex.pos == JSON_TOK_ARRAY_START) {
/* arrary in array update structure count */
array_in_array++;
}
}
obj->lex.pos++;
}
return -EINVAL;
}
static int obj_parse(struct json_obj *obj, const struct json_obj_descr *descr,
size_t descr_len, void *val)
{
struct json_obj_key_value kv;
int32_t decoded_fields = 0;
size_t i;
int ret;
while (!obj_next(obj, &kv)) {
if (kv.value.type == JSON_TOK_OBJECT_END) {
return decoded_fields;
}
for (i = 0; i < descr_len; i++) {
void *decode_field = (char *)val + descr[i].offset;
/* Field has been decoded already, skip */
if (decoded_fields & (1 << i)) {
continue;
}
/* Check if it's the i-th field */
if (kv.key_len != descr[i].field_name_len) {
continue;
}
if (memcmp(kv.key, descr[i].field_name,
descr[i].field_name_len)) {
continue;
}
/* Store the decoded value */
ret = decode_value(obj, &descr[i], &kv.value,
decode_field, val);
if (ret < 0) {
return ret;
}
decoded_fields |= 1<<i;
break;
}
}
return -EINVAL;
}
int json_obj_parse(char *payload, size_t len,
const struct json_obj_descr *descr, size_t descr_len,
void *val)
{
struct json_obj obj;
int ret;
__ASSERT_NO_MSG(descr_len < (sizeof(ret) * CHAR_BIT - 1));
ret = obj_init(&obj, payload, len);
if (ret < 0) {
return ret;
}
return obj_parse(&obj, descr, descr_len, val);
}
int json_arr_parse(char *payload, size_t len,
const struct json_obj_descr *descr, void *val)
{
struct json_obj arr;
int ret;
ret = arr_init(&arr, payload, len);
if (ret < 0) {
return ret;
}
void *ptr = (char *)val + descr->offset;
return arr_parse(&arr, descr->array.element_descr,
descr->array.n_elements, ptr, val);
}
int json_arr_separate_object_parse_init(struct json_obj *json, char *payload, size_t len)
{
return arr_init(json, payload, len);
}
int json_arr_separate_parse_object(struct json_obj *json, const struct json_obj_descr *descr,
size_t descr_len, void *val)
{
struct json_token tok;
if (!lexer_next(&json->lex, &tok)) {
return -EINVAL;
}
if (tok.type == JSON_TOK_ARRAY_END) {
return 0;
} else if (tok.type == JSON_TOK_COMMA) {
if (!lexer_next(&json->lex, &tok)) {
return -EINVAL;
}
}
if (tok.type != JSON_TOK_OBJECT_START) {
return -EINVAL;
}
return obj_parse(json, descr, descr_len, val);
}
static char escape_as(char chr)
{
switch (chr) {
case '"':
return '"';
case '\\':
return '\\';
case '\b':
return 'b';
case '\f':
return 'f';
case '\n':
return 'n';
case '\r':
return 'r';
case '\t':
return 't';
}
return 0;
}
static int json_escape_internal(const char *str,
json_append_bytes_t append_bytes,
void *data)
{
const char *cur;
int ret = 0;
for (cur = str; ret == 0 && *cur; cur++) {
char escaped = escape_as(*cur);
if (escaped) {
char bytes[2] = { '\\', escaped };
ret = append_bytes(bytes, 2, data);
} else {
ret = append_bytes(cur, 1, data);
}
}
return ret;
}
size_t json_calc_escaped_len(const char *str, size_t len)
{
size_t escaped_len = len;
size_t pos;
for (pos = 0; pos < len; pos++) {
if (escape_as(str[pos])) {
escaped_len++;
}
}
return escaped_len;
}
ssize_t json_escape(char *str, size_t *len, size_t buf_size)
{
char *next; /* Points after next character to escape. */
char *dest; /* Points after next place to write escaped character. */
size_t escaped_len = json_calc_escaped_len(str, *len);
if (escaped_len == *len) {
/*
* If no escape is necessary, there is nothing to do.
*/
return 0;
}
if (escaped_len >= buf_size) {
return -ENOMEM;
}
/*
* By walking backwards in the buffer from the end positions
* of both the original and escaped strings, we avoid using
* extra space. Characters in the original string are
* overwritten only after they have already been escaped.
*/
str[escaped_len] = '\0';
for (next = &str[*len], dest = &str[escaped_len]; next != str;) {
char next_c = *(--next);
char escape = escape_as(next_c);
if (escape) {
*(--dest) = escape;
*(--dest) = '\\';
} else {
*(--dest) = next_c;
}
}
*len = escaped_len;
return 0;
}
static int encode(const struct json_obj_descr *descr, const void *val,
json_append_bytes_t append_bytes, void *data);
static int arr_encode(const struct json_obj_descr *elem_descr,
const void *field, const void *val,
json_append_bytes_t append_bytes, void *data)
{
ptrdiff_t elem_size = get_elem_size(elem_descr);
/*
* NOTE: Since an element descriptor's offset isn't meaningful
* (array elements occur at multiple offsets in `val'), we use
* its space in elem_descr to store the offset to the field
* containing the number of elements.
*/
size_t n_elem = *(size_t *)((char *)val + elem_descr->offset);
size_t i;
int ret;
ret = append_bytes("[", 1, data);
if (ret < 0) {
return ret;
}
for (i = 0; i < n_elem; i++) {
/*
* Though "field" points at the next element in the
* array which we need to encode, the value in
* elem_descr->offset is actually the offset of the
* length field in the "parent" struct containing the
* array.
*
* To patch things up, we lie to encode() about where
* the field is by exactly the amount it will offset
* it. This is a size optimization for struct
* json_obj_descr: the alternative is to keep a
* separate field next to element_descr which is an
* offset to the length field in the parent struct,
* but that would add a size_t to every descriptor.
*/
ret = encode(elem_descr, (char *)field - elem_descr->offset,
append_bytes, data);
if (ret < 0) {
return ret;
}
if (i < n_elem - 1) {
ret = append_bytes(",", 1, data);
if (ret < 0) {
return ret;
}
}
field = (char *)field + elem_size;
}
return append_bytes("]", 1, data);
}
static int str_encode(const char **str, json_append_bytes_t append_bytes,
void *data)
{
int ret;
ret = append_bytes("\"", 1, data);
if (ret < 0) {
return ret;
}
ret = json_escape_internal(*str, append_bytes, data);
if (!ret) {
return append_bytes("\"", 1, data);
}
return ret;
}
static int num_encode(const int32_t *num, json_append_bytes_t append_bytes,
void *data)
{
char buf[3 * sizeof(int32_t)];
int ret;
ret = snprintk(buf, sizeof(buf), "%d", *num);
if (ret < 0) {
return ret;
}
if (ret >= (int)sizeof(buf)) {
return -ENOMEM;
}
return append_bytes(buf, (size_t)ret, data);
}
static int float_ascii_encode(struct json_obj_token *num, json_append_bytes_t append_bytes,
void *data)
{
return append_bytes(num->start, num->length, data);
}
static int opaque_string_encode(struct json_obj_token *opaque, json_append_bytes_t append_bytes,
void *data)
{
int ret;
ret = append_bytes("\"", 1, data);
if (ret < 0) {
return ret;
}
ret = append_bytes(opaque->start, opaque->length, data);
if (ret < 0) {
return ret;
}
return append_bytes("\"", 1, data);
}
static int bool_encode(const bool *value, json_append_bytes_t append_bytes,
void *data)
{
if (*value) {
return append_bytes("true", 4, data);
}
return append_bytes("false", 5, data);
}
static int encode(const struct json_obj_descr *descr, const void *val,
json_append_bytes_t append_bytes, void *data)
{
void *ptr = (char *)val + descr->offset;
switch (descr->type) {
case JSON_TOK_FALSE:
case JSON_TOK_TRUE:
return bool_encode(ptr, append_bytes, data);
case JSON_TOK_STRING:
return str_encode(ptr, append_bytes, data);
case JSON_TOK_ARRAY_START:
return arr_encode(descr->array.element_descr, ptr,
val, append_bytes, data);
case JSON_TOK_OBJECT_START:
return json_obj_encode(descr->object.sub_descr,
descr->object.sub_descr_len,
ptr, append_bytes, data);
case JSON_TOK_NUMBER:
return num_encode(ptr, append_bytes, data);
case JSON_TOK_FLOAT:
return float_ascii_encode(ptr, append_bytes, data);
case JSON_TOK_OPAQUE:
return opaque_string_encode(ptr, append_bytes, data);
default:
return -EINVAL;
}
}
int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
const void *val, json_append_bytes_t append_bytes,
void *data)
{
size_t i;
int ret;
ret = append_bytes("{", 1, data);
if (ret < 0) {
return ret;
}
for (i = 0; i < descr_len; i++) {