7#ifndef ZEPHYR_INCLUDE_DATA_JSON_H_
8#define ZEPHYR_INCLUDE_DATA_JSON_H_
104#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
105 __alignof__(type) == 2 ? 1 : \
106 __alignof__(type) == 4 ? 2 : 3)
128#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
130 .field_name = (#field_name_), \
131 .align_shift = Z_ALIGN_SHIFT(struct_), \
132 .field_name_len = sizeof(#field_name_) - 1, \
134 .offset = offsetof(struct_, field_name_), \
161#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
163 .field_name = (#field_name_), \
164 .align_shift = Z_ALIGN_SHIFT(struct_), \
165 .field_name_len = (sizeof(#field_name_) - 1), \
166 .type = JSON_TOK_OBJECT_START, \
167 .offset = offsetof(struct_, field_name_), \
170 .sub_descr = sub_descr_, \
171 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
198#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
199 len_field_, elem_type_) \
201 .field_name = (#field_name_), \
202 .align_shift = Z_ALIGN_SHIFT(struct_), \
203 .field_name_len = sizeof(#field_name_) - 1, \
204 .type = JSON_TOK_LIST_START, \
205 .offset = offsetof(struct_, field_name_), \
208 .element_descr = (struct json_obj_descr[]) { { \
210 Z_ALIGN_SHIFT(struct_), \
211 .type = elem_type_, \
216 .n_elements = (max_len_), \
255#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
256 len_field_, elem_descr_, elem_descr_len_) \
258 .field_name = (#field_name_), \
259 .align_shift = Z_ALIGN_SHIFT(struct_), \
260 .field_name_len = sizeof(#field_name_) - 1, \
261 .type = JSON_TOK_LIST_START, \
262 .offset = offsetof(struct_, field_name_), \
265 .element_descr = (struct json_obj_descr[]) { { \
267 Z_ALIGN_SHIFT(struct_), \
268 .type = JSON_TOK_OBJECT_START, \
269 .offset = offsetof(struct_, \
280 .n_elements = (max_len_), \
328#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
329 elem_descr_, elem_descr_len_) \
331 .field_name = (#field_name_), \
332 .align_shift = Z_ALIGN_SHIFT(struct_), \
333 .field_name_len = sizeof(#field_name_) - 1, \
334 .type = JSON_TOK_LIST_START, \
335 .offset = offsetof(struct_, field_name_), \
338 .element_descr = (struct json_obj_descr[]) { { \
340 Z_ALIGN_SHIFT(struct_), \
341 .type = JSON_TOK_LIST_START, \
342 .offset = offsetof(struct_, \
350 ZERO_OR_COMPILE_ERROR( \
351 elem_descr_len_ == 1 \
356 .n_elements = (max_len_), \
375#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
376 struct_field_name_, type_) \
378 .field_name = (json_field_name_), \
379 .align_shift = Z_ALIGN_SHIFT(struct_), \
380 .field_name_len = sizeof(json_field_name_) - 1, \
382 .offset = offsetof(struct_, struct_field_name_), \
398#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
399 struct_field_name_, sub_descr_) \
401 .field_name = (json_field_name_), \
402 .align_shift = Z_ALIGN_SHIFT(struct_), \
403 .field_name_len = (sizeof(json_field_name_) - 1), \
404 .type = JSON_TOK_OBJECT_START, \
405 .offset = offsetof(struct_, struct_field_name_), \
408 .sub_descr = sub_descr_, \
409 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
430#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
431 struct_field_name_, max_len_, len_field_, \
434 .field_name = (json_field_name_), \
435 .align_shift = Z_ALIGN_SHIFT(struct_), \
436 .field_name_len = sizeof(json_field_name_) - 1, \
437 .type = JSON_TOK_LIST_START, \
438 .offset = offsetof(struct_, struct_field_name_), \
441 .element_descr = (struct json_obj_descr[]) { { \
443 Z_ALIGN_SHIFT(struct_), \
444 .type = elem_type_, \
445 .offset = offsetof(struct_, \
448 .n_elements = (max_len_), \
493#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
494 struct_field_name_, max_len_, \
495 len_field_, elem_descr_, \
498 .field_name = json_field_name_, \
499 .align_shift = Z_ALIGN_SHIFT(struct_), \
500 .field_name_len = sizeof(json_field_name_) - 1, \
501 .type = JSON_TOK_LIST_START, \
502 .offset = offsetof(struct_, struct_field_name_), \
505 .element_descr = (struct json_obj_descr[]) { { \
507 Z_ALIGN_SHIFT(struct_), \
508 .type = JSON_TOK_OBJECT_START, \
509 .offset = offsetof(struct_, \
520 .n_elements = (max_len_), \
594 size_t descr_len,
const void *val);
610 const void *val,
char *
buffer,
size_t buf_size);
625 char *
buffer,
size_t buf_size);
json_tokens
Definition: json.h:31
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.
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.
size_t json_calc_escaped_len(const char *str, size_t len)
Calculates the JSON-escaped string length.
int 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 pointer 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:101
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_COLON
Definition: json.h:43
@ JSON_TOK_LIST_END
Definition: json.h:41
@ JSON_TOK_COMMA
Definition: json.h:44
@ JSON_TOK_OBJECT_START
Definition: json.h:38
@ JSON_TOK_OBJECT_END
Definition: json.h:39
@ JSON_TOK_TRUE
Definition: json.h:46
@ JSON_TOK_FALSE
Definition: json.h:47
@ JSON_TOK_LIST_START
Definition: json.h:40
@ JSON_TOK_NONE
Definition: json.h:37
@ JSON_TOK_NULL
Definition: json.h:48
@ JSON_TOK_STRING
Definition: json.h:42
@ JSON_TOK_EOF
Definition: json.h:50
@ JSON_TOK_NUMBER
Definition: json.h:45
@ JSON_TOK_ERROR
Definition: json.h:49
static ZTEST_BMEM char buffer[8]
Test mailbox enhance capabilities.
Definition: test_mbox_api.c:566
__SIZE_TYPE__ ssize_t
Definition: types.h:28
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
const struct json_obj_descr * element_descr
Definition: json.h:83
const char * field_name
Definition: json.h:54
uint32_t align_shift
Definition: json.h:61
struct json_obj_descr::@95::@97 object
const struct json_obj_descr * sub_descr
Definition: json.h:79
uint32_t field_name_len
Definition: json.h:64
struct json_obj_descr::@95::@98 array
uint32_t offset
Definition: json.h:75
uint32_t type
Definition: json.h:72
size_t n_elements
Definition: json.h:84
size_t sub_descr_len
Definition: json.h:80
static fdata_t data[2]
Definition: test_fifo_contexts.c:15