7#ifndef ZEPHYR_INCLUDE_DATA_JSON_H_
8#define ZEPHYR_INCLUDE_DATA_JSON_H_
130#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
131 __alignof__(type) == 2 ? 1 : \
132 __alignof__(type) == 4 ? 2 : 3)
154#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
156 .field_name = (#field_name_), \
157 .align_shift = Z_ALIGN_SHIFT(struct_), \
158 .field_name_len = sizeof(#field_name_) - 1, \
160 .offset = offsetof(struct_, field_name_), \
187#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
189 .field_name = (#field_name_), \
190 .align_shift = Z_ALIGN_SHIFT(struct_), \
191 .field_name_len = (sizeof(#field_name_) - 1), \
192 .type = JSON_TOK_OBJECT_START, \
193 .offset = offsetof(struct_, field_name_), \
195 .sub_descr = sub_descr_, \
196 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
209#define Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_, union_) \
210 (const struct json_obj_descr[]) \
213 .align_shift = Z_ALIGN_SHIFT(struct_), \
214 .type = elem_type_, \
215 .offset = offsetof(struct_, len_field_), \
226#define Z_JSON_DESCR_ARRAY(elem_descr_, elem_descr_len_) \
229 .element_descr = elem_descr_, \
230 .n_elements = elem_descr_len_, \
240#define Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_) \
243 .sub_descr = elem_descr_, \
244 .sub_descr_len = elem_descr_len_, \
270#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
271 len_field_, elem_type_) \
273 .field_name = (#field_name_), \
274 .align_shift = Z_ALIGN_SHIFT(struct_), \
275 .field_name_len = sizeof(#field_name_) - 1, \
276 .type = JSON_TOK_ARRAY_START, \
277 .offset = offsetof(struct_, field_name_), \
279 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
281 .n_elements = (max_len_), \
319#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
320 len_field_, elem_descr_, elem_descr_len_) \
322 .field_name = (#field_name_), \
323 .align_shift = Z_ALIGN_SHIFT(struct_), \
324 .field_name_len = sizeof(#field_name_) - 1, \
325 .type = JSON_TOK_ARRAY_START, \
326 .offset = offsetof(struct_, field_name_), \
328 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
329 JSON_TOK_OBJECT_START, \
330 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
331 .n_elements = (max_len_), \
378#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
379 elem_descr_, elem_descr_len_) \
381 .field_name = (#field_name_), \
382 .align_shift = Z_ALIGN_SHIFT(struct_), \
383 .field_name_len = sizeof(#field_name_) - 1, \
384 .type = JSON_TOK_ARRAY_START, \
385 .offset = offsetof(struct_, field_name_), \
387 .element_descr = Z_JSON_ELEMENT_DESCR( \
388 struct_, len_field_, JSON_TOK_ARRAY_START, \
389 Z_JSON_DESCR_ARRAY( \
391 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
392 .n_elements = (max_len_), \
413#define JSON_OBJ_DESCR_ARRAY_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, \
414 max_len_, len_field_, elem_descr_, elem_descr_len_) \
416 .field_name = (#json_field_name_), \
417 .align_shift = Z_ALIGN_SHIFT(struct_), \
418 .field_name_len = sizeof(#json_field_name_) - 1, \
419 .type = JSON_TOK_ARRAY_START, \
420 .offset = offsetof(struct_, struct_field_name_), \
422 .element_descr = Z_JSON_ELEMENT_DESCR( \
423 struct_, len_field_, JSON_TOK_ARRAY_START, \
424 Z_JSON_DESCR_ARRAY( \
426 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
427 .n_elements = (max_len_), \
445#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
446 struct_field_name_, type_) \
448 .field_name = (json_field_name_), \
449 .align_shift = Z_ALIGN_SHIFT(struct_), \
450 .field_name_len = sizeof(json_field_name_) - 1, \
452 .offset = offsetof(struct_, struct_field_name_), \
468#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
469 struct_field_name_, sub_descr_) \
471 .field_name = (json_field_name_), \
472 .align_shift = Z_ALIGN_SHIFT(struct_), \
473 .field_name_len = (sizeof(json_field_name_) - 1), \
474 .type = JSON_TOK_OBJECT_START, \
475 .offset = offsetof(struct_, struct_field_name_), \
477 .sub_descr = sub_descr_, \
478 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
498#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
499 struct_field_name_, max_len_, len_field_, \
502 .field_name = (json_field_name_), \
503 .align_shift = Z_ALIGN_SHIFT(struct_), \
504 .field_name_len = sizeof(json_field_name_) - 1, \
505 .type = JSON_TOK_ARRAY_START, \
506 .offset = offsetof(struct_, struct_field_name_), \
509 Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_,), \
510 .n_elements = (max_len_), \
554#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
555 struct_field_name_, max_len_, \
556 len_field_, elem_descr_, \
559 .field_name = json_field_name_, \
560 .align_shift = Z_ALIGN_SHIFT(struct_), \
561 .field_name_len = sizeof(json_field_name_) - 1, \
562 .type = JSON_TOK_ARRAY_START, \
563 .offset = offsetof(struct_, struct_field_name_), \
565 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
566 JSON_TOK_OBJECT_START, \
567 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
568 .n_elements = (max_len_), \
674 size_t descr_len,
void *val);
711 size_t descr_len,
const void *val);
739 const void *val,
char *buffer,
size_t buf_size);
754 char *buffer,
size_t buf_size);
json_tokens
Definition: json.h:26
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_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:127
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_OBJ_ARRAY
Definition: json.h:47
@ JSON_TOK_ARRAY_END
Definition: json.h:40
@ JSON_TOK_COLON
Definition: json.h:42
@ JSON_TOK_LIST_END
Definition: json.h:39
@ JSON_TOK_COMMA
Definition: json.h:43
@ JSON_TOK_OBJECT_START
Definition: json.h:33
@ JSON_TOK_OBJECT_END
Definition: json.h:34
@ JSON_TOK_TRUE
Definition: json.h:48
@ JSON_TOK_FALSE
Definition: json.h:49
@ JSON_TOK_LIST_START
Definition: json.h:36
@ JSON_TOK_NONE
Definition: json.h:32
@ JSON_TOK_NULL
Definition: json.h:50
@ JSON_TOK_OPAQUE
Definition: json.h:46
@ JSON_TOK_ARRAY_START
Definition: json.h:37
@ JSON_TOK_FLOAT
Definition: json.h:45
@ JSON_TOK_STRING
Definition: json.h:41
@ JSON_TOK_EOF
Definition: json.h:52
@ JSON_TOK_NUMBER
Definition: json.h:44
@ JSON_TOK_ERROR
Definition: json.h:51
__SIZE_TYPE__ ssize_t
Definition: types.h:28
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__INT64_TYPE__ int64_t
Definition: stdint.h:75
char * pos
Definition: json.h:64
char * start
Definition: json.h:63
struct json_token tok
Definition: json.h:66
char * end
Definition: json.h:65
const struct json_obj_descr * element_descr
Definition: json.h:109
const char * field_name
Definition: json.h:80
struct json_obj_descr::@123::@125 object
uint32_t align_shift
Definition: json.h:87
const struct json_obj_descr * sub_descr
Definition: json.h:105
uint32_t field_name_len
Definition: json.h:90
uint32_t offset
Definition: json.h:101
struct json_obj_descr::@123::@126 array
uint32_t type
Definition: json.h:98
size_t n_elements
Definition: json.h:110
size_t sub_descr_len
Definition: json.h:106
size_t length
Definition: json.h:75
char * start
Definition: json.h:74
struct json_lexer lex
Definition: json.h:70
char * start
Definition: json.h:57
enum json_tokens type
Definition: json.h:56
char * end
Definition: json.h:58