Zephyr API Documentation 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
25
27 /* Before changing this enum, ensure that its maximum
28 * value is still within 7 bits. See comment next to the
29 * declaration of `type` in struct json_obj_descr.
30 */
31
58};
59
60struct json_token {
62 char *start;
63 char *end;
64};
65
66struct json_lexer {
67 void *(*state)(struct json_lexer *lex);
68 char *start;
69 char *pos;
70 char *end;
72};
73
74struct json_obj {
76};
77
79 char *start;
80 size_t length;
81};
82
83
85 const char *field_name;
86
87 /* Alignment can be 1, 2, 4, or 8. The macros to create
88 * a struct json_obj_descr will store the alignment's
89 * power of 2 in order to keep this value in the 0-3 range
90 * and thus use only 2 bits.
91 */
93
94 /* 127 characters is more than enough for a field name. */
96
97 /* Valid values here (enum json_tokens): JSON_TOK_STRING,
98 * JSON_TOK_NUMBER, JSON_TOK_TRUE, JSON_TOK_FALSE,
99 * JSON_TOK_OBJECT_START, JSON_TOK_ARRAY_START. (All others
100 * ignored.) Maximum value is '}' (125), so this has to be 7 bits
101 * long.
102 */
104
105 /* 65535 bytes is more than enough for many JSON payloads. */
107
108 union {
109 struct {
113 struct {
117 struct {
118 size_t size;
120 };
121};
122
135typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
136 void *data);
137
138#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
139 __alignof__(type) == 2 ? 1 : \
140 __alignof__(type) == 4 ? 2 : 3)
141
162#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
163 { \
164 .field_name = (#field_name_), \
165 .align_shift = Z_ALIGN_SHIFT(struct_), \
166 .field_name_len = sizeof(#field_name_) - 1, \
167 .type = type_, \
168 .offset = offsetof(struct_, field_name_), \
169 .field = { \
170 .size = SIZEOF_FIELD(struct_, field_name_) \
171 }, \
172 }
173
198#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
199 { \
200 .field_name = (#field_name_), \
201 .align_shift = Z_ALIGN_SHIFT(struct_), \
202 .field_name_len = (sizeof(#field_name_) - 1), \
203 .type = JSON_TOK_OBJECT_START, \
204 .offset = offsetof(struct_, field_name_), \
205 .object = { \
206 .sub_descr = sub_descr_, \
207 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
208 }, \
209 }
210
220#define Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_, union_) \
221 (const struct json_obj_descr[]) \
222 { \
223 { \
224 .align_shift = Z_ALIGN_SHIFT(struct_), \
225 .type = elem_type_, \
226 .offset = offsetof(struct_, len_field_), \
227 union_ \
228 } \
229 }
230
237#define Z_JSON_DESCR_ARRAY(elem_descr_, elem_descr_len_) \
238 { \
239 .array = { \
240 .element_descr = elem_descr_, \
241 .n_elements = elem_descr_len_, \
242 }, \
243 }
244
251#define Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_) \
252 .object = { \
253 .sub_descr = elem_descr_, \
254 .sub_descr_len = elem_descr_len_, \
255 }, \
256
263#define Z_JSON_DESCR_FIELD(struct_, field_name_) \
264 { \
265 .field = { \
266 .size = SIZEOF_FIELD(struct_, field_name_), \
267 }, \
268 }
269
292#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
293 len_field_, elem_type_) \
294 { \
295 .field_name = (#field_name_), \
296 .align_shift = Z_ALIGN_SHIFT(struct_), \
297 .field_name_len = sizeof(#field_name_) - 1, \
298 .type = JSON_TOK_ARRAY_START, \
299 .offset = offsetof(struct_, field_name_), \
300 .array = { \
301 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
302 elem_type_, \
303 Z_JSON_DESCR_FIELD(struct_, field_name_[0])), \
304 .n_elements = (max_len_), \
305 }, \
306 }
307
342#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
343 len_field_, elem_descr_, elem_descr_len_) \
344 { \
345 .field_name = (#field_name_), \
346 .align_shift = Z_ALIGN_SHIFT(struct_), \
347 .field_name_len = sizeof(#field_name_) - 1, \
348 .type = JSON_TOK_ARRAY_START, \
349 .offset = offsetof(struct_, field_name_), \
350 .array = { \
351 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
352 JSON_TOK_OBJECT_START, \
353 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
354 .n_elements = (max_len_), \
355 }, \
356 }
357
401#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
402 elem_descr_, elem_descr_len_) \
403 { \
404 .field_name = (#field_name_), \
405 .align_shift = Z_ALIGN_SHIFT(struct_), \
406 .field_name_len = sizeof(#field_name_) - 1, \
407 .type = JSON_TOK_ARRAY_START, \
408 .offset = offsetof(struct_, field_name_), \
409 .array = { \
410 .element_descr = Z_JSON_ELEMENT_DESCR( \
411 struct_, len_field_, JSON_TOK_ARRAY_START, \
412 Z_JSON_DESCR_ARRAY( \
413 elem_descr_, \
414 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
415 .n_elements = (max_len_), \
416 }, \
417 }
418
436#define JSON_OBJ_DESCR_ARRAY_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, \
437 max_len_, len_field_, elem_descr_, elem_descr_len_) \
438 { \
439 .field_name = (#json_field_name_), \
440 .align_shift = Z_ALIGN_SHIFT(struct_), \
441 .field_name_len = sizeof(#json_field_name_) - 1, \
442 .type = JSON_TOK_ARRAY_START, \
443 .offset = offsetof(struct_, struct_field_name_), \
444 .array = { \
445 .element_descr = Z_JSON_ELEMENT_DESCR( \
446 struct_, len_field_, JSON_TOK_ARRAY_START, \
447 Z_JSON_DESCR_ARRAY( \
448 elem_descr_, \
449 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
450 .n_elements = (max_len_), \
451 }, \
452 }
453
468#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
469 struct_field_name_, type_) \
470 { \
471 .field_name = (json_field_name_), \
472 .align_shift = Z_ALIGN_SHIFT(struct_), \
473 .field_name_len = sizeof(json_field_name_) - 1, \
474 .type = type_, \
475 .offset = offsetof(struct_, struct_field_name_), \
476 .field = { \
477 .size = SIZEOF_FIELD(struct_, struct_field_name_) \
478 }, \
479 }
480
494#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
495 struct_field_name_, sub_descr_) \
496 { \
497 .field_name = (json_field_name_), \
498 .align_shift = Z_ALIGN_SHIFT(struct_), \
499 .field_name_len = (sizeof(json_field_name_) - 1), \
500 .type = JSON_TOK_OBJECT_START, \
501 .offset = offsetof(struct_, struct_field_name_), \
502 .object = { \
503 .sub_descr = sub_descr_, \
504 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
505 }, \
506 }
507
524#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
525 struct_field_name_, max_len_, len_field_, \
526 elem_type_) \
527 { \
528 .field_name = (json_field_name_), \
529 .align_shift = Z_ALIGN_SHIFT(struct_), \
530 .field_name_len = sizeof(json_field_name_) - 1, \
531 .type = JSON_TOK_ARRAY_START, \
532 .offset = offsetof(struct_, struct_field_name_), \
533 .array = { \
534 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
535 elem_type_, \
536 Z_JSON_DESCR_FIELD(struct_, struct_field_name_[0])), \
537 .n_elements = (max_len_), \
538 }, \
539 }
540
581#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
582 struct_field_name_, max_len_, \
583 len_field_, elem_descr_, \
584 elem_descr_len_) \
585 { \
586 .field_name = json_field_name_, \
587 .align_shift = Z_ALIGN_SHIFT(struct_), \
588 .field_name_len = sizeof(json_field_name_) - 1, \
589 .type = JSON_TOK_ARRAY_START, \
590 .offset = offsetof(struct_, struct_field_name_), \
591 .array = { \
592 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
593 JSON_TOK_OBJECT_START, \
594 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
595 .n_elements = (max_len_), \
596 }, \
597 }
598
645int64_t json_obj_parse(char *json, size_t len,
646 const struct json_obj_descr *descr, size_t descr_len,
647 void *val);
648
681int json_arr_parse(char *json, size_t len,
682 const struct json_obj_descr *descr, void *val);
683
700int json_arr_separate_object_parse_init(struct json_obj *json, char *payload, size_t len);
701
716int json_arr_separate_parse_object(struct json_obj *json, const struct json_obj_descr *descr,
717 size_t descr_len, void *val);
718
731ssize_t json_escape(char *str, size_t *len, size_t buf_size);
732
741size_t json_calc_escaped_len(const char *str, size_t len);
742
754 size_t descr_len, const void *val);
755
766 const void *val);
767
781int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len,
782 const void *val, char *buffer, size_t buf_size);
783
796int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val,
797 char *buffer, size_t buf_size);
798
812int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
813 const void *val, json_append_bytes_t append_bytes,
814 void *data);
815
828int json_arr_encode(const struct json_obj_descr *descr, const void *val,
829 json_append_bytes_t append_bytes, void *data);
830
849
850 union {
851 struct {
852 size_t offset;
853 size_t size;
855
856 struct {
859 size_t offset;
861
862 struct {
865 size_t offset;
867
868 struct {
870 size_t sub_descr_len;
871 size_t offset;
873 };
874};
875
890#define JSON_MIXED_ARR_DESCR_PRIM(struct_, field_name_, type_, count_field_) \
891{ \
892 .type = type_, \
893 .count_offset = offsetof(struct_, count_field_), \
894 .primitive = { \
895 .offset = offsetof(struct_, field_name_), \
896 .size = SIZEOF_FIELD(struct_, field_name_) \
897 } \
898}
899
916#define JSON_MIXED_ARR_DESCR_OBJECT(struct_, field_name_, sub_descr_, count_field_) \
917{ \
918 .type = JSON_TOK_OBJECT_START, \
919 .count_offset = offsetof(struct_, count_field_), \
920 .object = { \
921 .sub_descr = sub_descr_, \
922 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
923 .offset = offsetof(struct_, field_name_) \
924 } \
925}
926
948#define JSON_MIXED_ARR_DESCR_ARRAY(struct_, field_name_, max_len_, elem_descr_, count_field_) \
949{ \
950 .type = JSON_TOK_ARRAY_START, \
951 .count_offset = offsetof(struct_, count_field_), \
952 .array = { \
953 .element_descr = elem_descr_, \
954 .n_elements = (max_len_), \
955 .offset = offsetof(struct_, field_name_) \
956 } \
957}
958
974#define JSON_MIXED_ARR_DESCR_MIXED_ARRAY(struct_, field_name_, sub_descr_, count_field_) \
975{ \
976 .type = JSON_TOK_MIXED_ARRAY, \
977 .count_offset = offsetof(struct_, count_field_), \
978 .mixed_array = { \
979 .sub_descr = sub_descr_, \
980 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
981 .offset = offsetof(struct_, field_name_) \
982 } \
983}
984
1000int json_mixed_arr_parse(char *json, size_t len,
1001 const struct json_mixed_arr_descr *descr,
1002 size_t descr_len, void *val);
1003
1020 size_t descr_len, void *val,
1021 json_append_bytes_t append_bytes,
1022 void *data);
1023
1039 size_t descr_len, void *val,
1040 char *buffer, size_t buf_size);
1041
1055 size_t descr_len, void *val);
1056
1057#ifdef __cplusplus
1058}
1059#endif
1060
1064#endif /* ZEPHYR_INCLUDE_DATA_JSON_H_ */
json_tokens
Definition json.h:26
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_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:135
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_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:38
@ JSON_TOK_OBJ_ARRAY
Definition json.h:44
@ JSON_TOK_UINT
Definition json.h:51
@ JSON_TOK_ARRAY_END
Definition json.h:36
@ JSON_TOK_INT64
Definition json.h:46
@ JSON_TOK_COLON
Definition json.h:39
@ JSON_TOK_UINT64
Definition json.h:47
@ JSON_TOK_MIXED_ARRAY
Definition json.h:54
@ JSON_TOK_DOUBLE_FP
Definition json.h:49
@ JSON_TOK_COMMA
Definition json.h:40
@ JSON_TOK_INT
Definition json.h:50
@ JSON_TOK_OBJECT_START
Definition json.h:33
@ JSON_TOK_OBJECT_END
Definition json.h:34
@ JSON_TOK_TRUE
Definition json.h:52
@ JSON_TOK_FALSE
Definition json.h:53
@ JSON_TOK_NONE
Definition json.h:32
@ JSON_TOK_NULL
Definition json.h:55
@ JSON_TOK_OPAQUE
Definition json.h:43
@ JSON_TOK_ARRAY_START
Definition json.h:35
@ JSON_TOK_FLOAT_FP
Definition json.h:48
@ JSON_TOK_FLOAT
Definition json.h:42
@ JSON_TOK_STRING
Definition json.h:37
@ JSON_TOK_EOF
Definition json.h:57
@ JSON_TOK_NUMBER
Definition json.h:41
@ JSON_TOK_ENCODED_OBJ
Definition json.h:45
@ JSON_TOK_ERROR
Definition json.h:56
__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:66
char * pos
Definition json.h:69
char * start
Definition json.h:68
struct json_token tok
Definition json.h:71
char * end
Definition json.h:70
Descriptor for a mixed-type JSON array.
Definition json.h:846
struct json_mixed_arr_descr::@164075124070375113020317124123044277254252063155::@375252323377206076146242127313322144320020366157 primitive
const struct json_obj_descr * sub_descr
Definition json.h:857
struct json_mixed_arr_descr::@164075124070375113020317124123044277254252063155::@312074270031132304246007026150122314203032151316 array
size_t count_offset
Definition json.h:848
size_t size
Definition json.h:853
struct json_mixed_arr_descr::@164075124070375113020317124123044277254252063155::@335165272073312300073107172264066307233341354251 object
size_t offset
Definition json.h:852
size_t n_elements
Definition json.h:864
struct json_mixed_arr_descr::@164075124070375113020317124123044277254252063155::@007341373114107254242001225327304240057022334226 mixed_array
size_t sub_descr_len
Definition json.h:858
const struct json_obj_descr * element_descr
Definition json.h:863
uint32_t type
Definition json.h:847
Definition json.h:84
struct json_obj_descr::@365376074033132355222002170203261307250366002126::@063054233131061174035356024370111062256164304224 field
const struct json_obj_descr * element_descr
Definition json.h:114
const char * field_name
Definition json.h:85
uint32_t align_shift
Definition json.h:92
const struct json_obj_descr * sub_descr
Definition json.h:110
struct json_obj_descr::@365376074033132355222002170203261307250366002126::@000230262224034312357266150225204112257263011374 object
uint32_t field_name_len
Definition json.h:95
uint32_t offset
Definition json.h:106
uint32_t type
Definition json.h:103
size_t size
Definition json.h:118
struct json_obj_descr::@365376074033132355222002170203261307250366002126::@365235374054153265207136004206377347035314130057 array
size_t n_elements
Definition json.h:115
size_t sub_descr_len
Definition json.h:111
Definition json.h:78
size_t length
Definition json.h:80
char * start
Definition json.h:79
Definition json.h:74
struct json_lexer lex
Definition json.h:75
Definition json.h:60
char * start
Definition json.h:62
enum json_tokens type
Definition json.h:61
char * end
Definition json.h:63
Misc utilities.
Macros to abstract toolchain specific capabilities.