Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
json.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_DATA_JSON_H_
8#define ZEPHYR_INCLUDE_DATA_JSON_H_
9
10#include <zephyr/sys/util.h>
11#include <stddef.h>
12#include <zephyr/toolchain.h>
13#include <zephyr/types.h>
14#include <sys/types.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
27
29 /* Before changing this enum, ensure that its maximum
30 * value is still within 7 bits. See comment next to the
31 * declaration of `type` in struct json_obj_descr.
32 */
33
60};
61
62struct json_token {
64 char *start;
65 char *end;
66};
67
68struct json_lexer {
69 void *(*state)(struct json_lexer *lex);
70 char *start;
71 char *pos;
72 char *end;
74};
75
76struct json_obj {
78};
79
89 const char *key;
91 size_t key_len;
94};
95
97 char *start;
98 size_t length;
99};
100
101
103 const char *field_name;
104
105 /* Alignment can be 1, 2, 4, or 8. The macros to create
106 * a struct json_obj_descr will store the alignment's
107 * power of 2 in order to keep this value in the 0-3 range
108 * and thus use only 2 bits.
109 */
111
112 /* 127 characters is more than enough for a field name. */
114
115 /* Valid values here (enum json_tokens): JSON_TOK_STRING,
116 * JSON_TOK_NUMBER, JSON_TOK_TRUE, JSON_TOK_FALSE,
117 * JSON_TOK_OBJECT_START, JSON_TOK_ARRAY_START. (All others
118 * ignored.) Maximum value is '}' (125), so this has to be 7 bits
119 * long.
120 */
122
123 /* 65535 bytes is more than enough for many JSON payloads. */
125
126 union {
127 struct {
131 struct {
135 struct {
136 size_t size;
138 };
139};
140
153typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
154 void *data);
155
156#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
157 __alignof__(type) == 2 ? 1 : \
158 __alignof__(type) == 4 ? 2 : 3)
159
180#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
181 { \
182 .field_name = (#field_name_), \
183 .align_shift = Z_ALIGN_SHIFT(struct_), \
184 .field_name_len = sizeof(#field_name_) - 1, \
185 .type = type_, \
186 .offset = offsetof(struct_, field_name_), \
187 .field = { \
188 .size = SIZEOF_FIELD(struct_, field_name_) \
189 }, \
190 }
191
216#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
217 { \
218 .field_name = (#field_name_), \
219 .align_shift = Z_ALIGN_SHIFT(struct_), \
220 .field_name_len = (sizeof(#field_name_) - 1), \
221 .type = JSON_TOK_OBJECT_START, \
222 .offset = offsetof(struct_, field_name_), \
223 .object = { \
224 .sub_descr = sub_descr_, \
225 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
226 }, \
227 }
228
238#define Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_, union_) \
239 (const struct json_obj_descr[]) \
240 { \
241 { \
242 .align_shift = Z_ALIGN_SHIFT(struct_), \
243 .type = elem_type_, \
244 .offset = offsetof(struct_, len_field_), \
245 union_ \
246 } \
247 }
248
255#define Z_JSON_DESCR_ARRAY(elem_descr_, elem_descr_len_) \
256 { \
257 .array = { \
258 .element_descr = elem_descr_, \
259 .n_elements = elem_descr_len_, \
260 }, \
261 }
262
269#define Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_) \
270 .object = { \
271 .sub_descr = elem_descr_, \
272 .sub_descr_len = elem_descr_len_, \
273 }, \
274
281#define Z_JSON_DESCR_FIELD(struct_, field_name_) \
282 { \
283 .field = { \
284 .size = SIZEOF_FIELD(struct_, field_name_), \
285 }, \
286 }
287
310#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
311 len_field_, elem_type_) \
312 { \
313 .field_name = (#field_name_), \
314 .align_shift = Z_ALIGN_SHIFT(struct_), \
315 .field_name_len = sizeof(#field_name_) - 1, \
316 .type = JSON_TOK_ARRAY_START, \
317 .offset = offsetof(struct_, field_name_), \
318 .array = { \
319 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
320 elem_type_, \
321 Z_JSON_DESCR_FIELD(struct_, field_name_[0])), \
322 .n_elements = (max_len_), \
323 }, \
324 }
325
360#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
361 len_field_, elem_descr_, elem_descr_len_) \
362 { \
363 .field_name = (#field_name_), \
364 .align_shift = Z_ALIGN_SHIFT(struct_), \
365 .field_name_len = sizeof(#field_name_) - 1, \
366 .type = JSON_TOK_ARRAY_START, \
367 .offset = offsetof(struct_, field_name_), \
368 .array = { \
369 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
370 JSON_TOK_OBJECT_START, \
371 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
372 .n_elements = (max_len_), \
373 }, \
374 }
375
419#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
420 elem_descr_, elem_descr_len_) \
421 { \
422 .field_name = (#field_name_), \
423 .align_shift = Z_ALIGN_SHIFT(struct_), \
424 .field_name_len = sizeof(#field_name_) - 1, \
425 .type = JSON_TOK_ARRAY_START, \
426 .offset = offsetof(struct_, field_name_), \
427 .array = { \
428 .element_descr = Z_JSON_ELEMENT_DESCR( \
429 struct_, len_field_, JSON_TOK_ARRAY_START, \
430 Z_JSON_DESCR_ARRAY( \
431 elem_descr_, \
432 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
433 .n_elements = (max_len_), \
434 }, \
435 }
436
454#define JSON_OBJ_DESCR_ARRAY_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, \
455 max_len_, len_field_, elem_descr_, elem_descr_len_) \
456 { \
457 .field_name = (#json_field_name_), \
458 .align_shift = Z_ALIGN_SHIFT(struct_), \
459 .field_name_len = sizeof(#json_field_name_) - 1, \
460 .type = JSON_TOK_ARRAY_START, \
461 .offset = offsetof(struct_, struct_field_name_), \
462 .array = { \
463 .element_descr = Z_JSON_ELEMENT_DESCR( \
464 struct_, len_field_, JSON_TOK_ARRAY_START, \
465 Z_JSON_DESCR_ARRAY( \
466 elem_descr_, \
467 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
468 .n_elements = (max_len_), \
469 }, \
470 }
471
486#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
487 struct_field_name_, type_) \
488 { \
489 .field_name = (json_field_name_), \
490 .align_shift = Z_ALIGN_SHIFT(struct_), \
491 .field_name_len = sizeof(json_field_name_) - 1, \
492 .type = type_, \
493 .offset = offsetof(struct_, struct_field_name_), \
494 .field = { \
495 .size = SIZEOF_FIELD(struct_, struct_field_name_) \
496 }, \
497 }
498
512#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
513 struct_field_name_, sub_descr_) \
514 { \
515 .field_name = (json_field_name_), \
516 .align_shift = Z_ALIGN_SHIFT(struct_), \
517 .field_name_len = (sizeof(json_field_name_) - 1), \
518 .type = JSON_TOK_OBJECT_START, \
519 .offset = offsetof(struct_, struct_field_name_), \
520 .object = { \
521 .sub_descr = sub_descr_, \
522 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
523 }, \
524 }
525
542#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
543 struct_field_name_, max_len_, len_field_, \
544 elem_type_) \
545 { \
546 .field_name = (json_field_name_), \
547 .align_shift = Z_ALIGN_SHIFT(struct_), \
548 .field_name_len = sizeof(json_field_name_) - 1, \
549 .type = JSON_TOK_ARRAY_START, \
550 .offset = offsetof(struct_, struct_field_name_), \
551 .array = { \
552 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
553 elem_type_, \
554 Z_JSON_DESCR_FIELD(struct_, struct_field_name_[0])), \
555 .n_elements = (max_len_), \
556 }, \
557 }
558
599#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
600 struct_field_name_, max_len_, \
601 len_field_, elem_descr_, \
602 elem_descr_len_) \
603 { \
604 .field_name = json_field_name_, \
605 .align_shift = Z_ALIGN_SHIFT(struct_), \
606 .field_name_len = sizeof(json_field_name_) - 1, \
607 .type = JSON_TOK_ARRAY_START, \
608 .offset = offsetof(struct_, struct_field_name_), \
609 .array = { \
610 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
611 JSON_TOK_OBJECT_START, \
612 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
613 .n_elements = (max_len_), \
614 }, \
615 }
616
663int64_t json_obj_parse(char *json, size_t len,
664 const struct json_obj_descr *descr, size_t descr_len,
665 void *val);
666
699int json_arr_parse(char *json, size_t len,
700 const struct json_obj_descr *descr, void *val);
701
718int json_arr_separate_object_parse_init(struct json_obj *json, char *payload, size_t len);
719
734int json_arr_separate_parse_object(struct json_obj *json, const struct json_obj_descr *descr,
735 size_t descr_len, void *val);
736
755int json_obj_separate_parse_init(struct json_obj *json, char *payload, size_t len);
756
782
795ssize_t json_escape(char *str, size_t *len, size_t buf_size);
796
805size_t json_calc_escaped_len(const char *str, size_t len);
806
818 size_t descr_len, const void *val);
819
830 const void *val);
831
845int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len,
846 const void *val, char *buffer, size_t buf_size);
847
860int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val,
861 char *buffer, size_t buf_size);
862
876int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
877 const void *val, json_append_bytes_t append_bytes,
878 void *data);
879
892int json_arr_encode(const struct json_obj_descr *descr, const void *val,
893 json_append_bytes_t append_bytes, void *data);
894
913
914 union {
915 struct {
916 size_t offset;
917 size_t size;
919
920 struct {
923 size_t offset;
925
926 struct {
929 size_t offset;
931
932 struct {
934 size_t sub_descr_len;
935 size_t offset;
937 };
938};
939
954#define JSON_MIXED_ARR_DESCR_PRIM(struct_, field_name_, type_, count_field_) \
955{ \
956 .type = type_, \
957 .count_offset = offsetof(struct_, count_field_), \
958 .primitive = { \
959 .offset = offsetof(struct_, field_name_), \
960 .size = SIZEOF_FIELD(struct_, field_name_) \
961 } \
962}
963
980#define JSON_MIXED_ARR_DESCR_OBJECT(struct_, field_name_, sub_descr_, count_field_) \
981{ \
982 .type = JSON_TOK_OBJECT_START, \
983 .count_offset = offsetof(struct_, count_field_), \
984 .object = { \
985 .sub_descr = sub_descr_, \
986 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
987 .offset = offsetof(struct_, field_name_) \
988 } \
989}
990
1012#define JSON_MIXED_ARR_DESCR_ARRAY(struct_, field_name_, max_len_, elem_descr_, count_field_) \
1013{ \
1014 .type = JSON_TOK_ARRAY_START, \
1015 .count_offset = offsetof(struct_, count_field_), \
1016 .array = { \
1017 .element_descr = elem_descr_, \
1018 .n_elements = (max_len_), \
1019 .offset = offsetof(struct_, field_name_) \
1020 } \
1021}
1022
1038#define JSON_MIXED_ARR_DESCR_MIXED_ARRAY(struct_, field_name_, sub_descr_, count_field_) \
1039{ \
1040 .type = JSON_TOK_MIXED_ARRAY, \
1041 .count_offset = offsetof(struct_, count_field_), \
1042 .mixed_array = { \
1043 .sub_descr = sub_descr_, \
1044 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
1045 .offset = offsetof(struct_, field_name_) \
1046 } \
1047}
1048
1064int json_mixed_arr_parse(char *json, size_t len,
1065 const struct json_mixed_arr_descr *descr,
1066 size_t descr_len, void *val);
1067
1084 size_t descr_len, void *val,
1085 json_append_bytes_t append_bytes,
1086 void *data);
1087
1103 size_t descr_len, void *val,
1104 char *buffer, size_t buf_size);
1105
1119 size_t descr_len, void *val);
1120
1121#ifdef __cplusplus
1122}
1123#endif
1124
1128#endif /* ZEPHYR_INCLUDE_DATA_JSON_H_ */
json_tokens
Definition json.h:28
ssize_t json_calc_mixed_arr_len(const struct json_mixed_arr_descr *descr, size_t descr_len, void *val)
Calculate the length of the encoded JSON mixed array.
int json_mixed_arr_encode(const struct json_mixed_arr_descr *descr, size_t descr_len, void *val, json_append_bytes_t append_bytes, void *data)
Encode a C structure as a JSON mixed array.
ssize_t json_calc_encoded_len(const struct json_obj_descr *descr, size_t descr_len, const void *val)
Calculates the string length to fully encode an object.
int json_mixed_arr_parse(char *json, size_t len, const struct json_mixed_arr_descr *descr, size_t descr_len, void *val)
Parse a JSON mixed array into a C structure.
ssize_t json_escape(char *str, size_t *len, size_t buf_size)
Escapes the string so it can be used to encode JSON objects.
int json_arr_encode(const struct json_obj_descr *descr, const void *val, json_append_bytes_t append_bytes, void *data)
Encodes an array using an arbitrary writer function.
int json_mixed_arr_encode_buf(const struct json_mixed_arr_descr *descr, size_t descr_len, void *val, char *buffer, size_t buf_size)
Encode a C structure as a JSON mixed array into a buffer.
size_t json_calc_escaped_len(const char *str, size_t len)
Calculates the JSON-escaped string length.
int json_arr_separate_object_parse_init(struct json_obj *json, char *payload, size_t len)
Initialize single-object array parsing.
int json_arr_separate_parse_object(struct json_obj *json, const struct json_obj_descr *descr, size_t descr_len, void *val)
Parse a single object from array.
int64_t json_obj_parse(char *json, size_t len, const struct json_obj_descr *descr, size_t descr_len, void *val)
Parses the JSON-encoded object pointed to by json, with size len, according to the descriptor pointed...
int json_obj_next_key_value(struct json_obj *json, struct json_obj_key_value *kv)
Return the next top-level member of a JSON object.
int json_arr_parse(char *json, size_t len, const struct json_obj_descr *descr, void *val)
Parses the JSON-encoded array pointed to by json, with size len, according to the descriptor pointed ...
int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len, const void *val, char *buffer, size_t buf_size)
Encodes an object in a contiguous memory location.
int(* json_append_bytes_t)(const char *bytes, size_t len, void *data)
Function pointer type to append bytes to a buffer while encoding JSON data.
Definition json.h:153
ssize_t json_calc_encoded_arr_len(const struct json_obj_descr *descr, const void *val)
Calculates the string length to fully encode an array.
int json_obj_separate_parse_init(struct json_obj *json, char *payload, size_t len)
Initialize incremental parsing of a JSON object's members.
int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val, char *buffer, size_t buf_size)
Encodes an array in a contiguous memory location.
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)
Encodes an object using an arbitrary writer function.
@ JSON_TOK_STRING_BUF
Definition json.h:40
@ JSON_TOK_OBJ_ARRAY
Definition json.h:46
@ JSON_TOK_UINT
Definition json.h:53
@ JSON_TOK_ARRAY_END
Definition json.h:38
@ JSON_TOK_INT64
Definition json.h:48
@ JSON_TOK_COLON
Definition json.h:41
@ JSON_TOK_UINT64
Definition json.h:49
@ JSON_TOK_MIXED_ARRAY
Definition json.h:56
@ JSON_TOK_DOUBLE_FP
Definition json.h:51
@ JSON_TOK_COMMA
Definition json.h:42
@ JSON_TOK_INT
Definition json.h:52
@ JSON_TOK_OBJECT_START
Definition json.h:35
@ JSON_TOK_OBJECT_END
Definition json.h:36
@ JSON_TOK_TRUE
Definition json.h:54
@ JSON_TOK_FALSE
Definition json.h:55
@ JSON_TOK_NONE
Definition json.h:34
@ JSON_TOK_NULL
Definition json.h:57
@ JSON_TOK_OPAQUE
Definition json.h:45
@ JSON_TOK_ARRAY_START
Definition json.h:37
@ JSON_TOK_FLOAT_FP
Definition json.h:50
@ JSON_TOK_FLOAT
Definition json.h:44
@ JSON_TOK_STRING
Definition json.h:39
@ JSON_TOK_EOF
Definition json.h:59
@ JSON_TOK_NUMBER
Definition json.h:43
@ JSON_TOK_ENCODED_OBJ
Definition json.h:47
@ JSON_TOK_ERROR
Definition json.h:58
__SIZE_TYPE__ ssize_t
Definition types.h:28
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT64_TYPE__ int64_t
Definition stdint.h:75
Definition json.h:68
char * pos
Definition json.h:71
char * start
Definition json.h:70
struct json_token tok
Definition json.h:73
char * end
Definition json.h:72
Descriptor for a mixed-type JSON array.
Definition json.h:910
struct json_mixed_arr_descr::@244026327323221123152345361132012301321227223077::@302344143027364304274016362070142150154337102131 array
const struct json_obj_descr * sub_descr
Definition json.h:921
size_t count_offset
Definition json.h:912
struct json_mixed_arr_descr::@244026327323221123152345361132012301321227223077::@044106164021054207036321133057035223064067326143 primitive
size_t size
Definition json.h:917
size_t offset
Definition json.h:916
struct json_mixed_arr_descr::@244026327323221123152345361132012301321227223077::@123234323321360274065237154215052351134143065123 object
size_t n_elements
Definition json.h:928
struct json_mixed_arr_descr::@244026327323221123152345361132012301321227223077::@345316070205011032254355362244064073315161302366 mixed_array
size_t sub_descr_len
Definition json.h:922
const struct json_obj_descr * element_descr
Definition json.h:927
uint32_t type
Definition json.h:911
Definition json.h:102
const struct json_obj_descr * element_descr
Definition json.h:132
const char * field_name
Definition json.h:103
uint32_t align_shift
Definition json.h:110
const struct json_obj_descr * sub_descr
Definition json.h:128
struct json_obj_descr::@343053305046106243045306154272006024002236014124::@112101001044326121371371164352006075131222304131 array
uint32_t field_name_len
Definition json.h:113
uint32_t offset
Definition json.h:124
uint32_t type
Definition json.h:121
size_t size
Definition json.h:136
size_t n_elements
Definition json.h:133
struct json_obj_descr::@343053305046106243045306154272006024002236014124::@260166306232076263265344137204077164255241313252 object
struct json_obj_descr::@343053305046106243045306154272006024002236014124::@126107274020165106026242023315371242210103260246 field
size_t sub_descr_len
Definition json.h:129
A single top-level "key": value member of a JSON object.
Definition json.h:87
struct json_token value
Member value, returned as a raw token span.
Definition json.h:93
size_t key_len
Length of the key text in bytes.
Definition json.h:91
const char * key
Member key text (not NUL-terminated), or NULL at the end of the object.
Definition json.h:89
Definition json.h:96
size_t length
Definition json.h:98
char * start
Definition json.h:97
Definition json.h:76
struct json_lexer lex
Definition json.h:77
Definition json.h:62
char * start
Definition json.h:64
enum json_tokens type
Definition json.h:63
char * end
Definition json.h:65
Misc utilities.
Macros to abstract toolchain specific capabilities.