Zephyr API Documentation  3.6.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 /* 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
35 /* JSON_TOK_LIST_START will be removed use JSON_TOK_ARRAY_START */
36 JSON_TOK_LIST_START __deprecated = '[',
38 /* JSON_TOK_LIST_END will be removed use JSON_TOK_ARRAY_END */
39 JSON_TOK_LIST_END __deprecated = ']',
53};
54
55struct json_token {
57 char *start;
58 char *end;
59};
60
61struct json_lexer {
62 void *(*state)(struct json_lexer *lex);
63 char *start;
64 char *pos;
65 char *end;
67};
68
69struct json_obj {
71};
72
74 char *start;
75 size_t length;
76};
77
78
80 const char *field_name;
81
82 /* Alignment can be 1, 2, 4, or 8. The macros to create
83 * a struct json_obj_descr will store the alignment's
84 * power of 2 in order to keep this value in the 0-3 range
85 * and thus use only 2 bits.
86 */
88
89 /* 127 characters is more than enough for a field name. */
91
92 /* Valid values here (enum json_tokens): JSON_TOK_STRING,
93 * JSON_TOK_NUMBER, JSON_TOK_TRUE, JSON_TOK_FALSE,
94 * JSON_TOK_OBJECT_START, JSON_TOK_ARRAY_START. (All others
95 * ignored.) Maximum value is '}' (125), so this has to be 7 bits
96 * long.
97 */
99
100 /* 65535 bytes is more than enough for many JSON payloads. */
102
103 union {
104 struct {
108 struct {
112 };
113};
114
127typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
128 void *data);
129
130#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
131 __alignof__(type) == 2 ? 1 : \
132 __alignof__(type) == 4 ? 2 : 3)
133
154#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
155 { \
156 .field_name = (#field_name_), \
157 .align_shift = Z_ALIGN_SHIFT(struct_), \
158 .field_name_len = sizeof(#field_name_) - 1, \
159 .type = type_, \
160 .offset = offsetof(struct_, field_name_), \
161 }
162
187#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
188 { \
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_), \
194 { \
195 .object = { \
196 .sub_descr = sub_descr_, \
197 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
198 }, \
199 }, \
200 }
201
211#define Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_, union_) \
212 (const struct json_obj_descr[]) \
213 { \
214 { \
215 .align_shift = Z_ALIGN_SHIFT(struct_), \
216 .type = elem_type_, \
217 .offset = offsetof(struct_, len_field_), \
218 union_ \
219 } \
220 }
221
228#define Z_JSON_DESCR_ARRAY(elem_descr_, elem_descr_len_) \
229 { \
230 .array = { \
231 .element_descr = elem_descr_, \
232 .n_elements = elem_descr_len_, \
233 }, \
234 }
235
242#define Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_) \
243 { \
244 .object = { \
245 .sub_descr = elem_descr_, \
246 .sub_descr_len = elem_descr_len_, \
247 }, \
248 }
249
272#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
273 len_field_, elem_type_) \
274 { \
275 .field_name = (#field_name_), \
276 .align_shift = Z_ALIGN_SHIFT(struct_), \
277 .field_name_len = sizeof(#field_name_) - 1, \
278 .type = JSON_TOK_ARRAY_START, \
279 .offset = offsetof(struct_, field_name_), \
280 { \
281 .array = { \
282 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
283 elem_type_,), \
284 .n_elements = (max_len_), \
285 }, \
286 }, \
287 }
288
323#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
324 len_field_, elem_descr_, elem_descr_len_) \
325 { \
326 .field_name = (#field_name_), \
327 .align_shift = Z_ALIGN_SHIFT(struct_), \
328 .field_name_len = sizeof(#field_name_) - 1, \
329 .type = JSON_TOK_ARRAY_START, \
330 .offset = offsetof(struct_, field_name_), \
331 { \
332 .array = { \
333 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
334 JSON_TOK_OBJECT_START, \
335 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
336 .n_elements = (max_len_), \
337 }, \
338 }, \
339 }
340
384#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
385 elem_descr_, elem_descr_len_) \
386 { \
387 .field_name = (#field_name_), \
388 .align_shift = Z_ALIGN_SHIFT(struct_), \
389 .field_name_len = sizeof(#field_name_) - 1, \
390 .type = JSON_TOK_ARRAY_START, \
391 .offset = offsetof(struct_, field_name_), \
392 { \
393 .array = { \
394 .element_descr = Z_JSON_ELEMENT_DESCR( \
395 struct_, len_field_, JSON_TOK_ARRAY_START, \
396 Z_JSON_DESCR_ARRAY( \
397 elem_descr_, \
398 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
399 .n_elements = (max_len_), \
400 }, \
401 }, \
402 }
403
421#define JSON_OBJ_DESCR_ARRAY_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, \
422 max_len_, len_field_, elem_descr_, elem_descr_len_) \
423 { \
424 .field_name = (#json_field_name_), \
425 .align_shift = Z_ALIGN_SHIFT(struct_), \
426 .field_name_len = sizeof(#json_field_name_) - 1, \
427 .type = JSON_TOK_ARRAY_START, \
428 .offset = offsetof(struct_, struct_field_name_), \
429 { \
430 .array = { \
431 .element_descr = Z_JSON_ELEMENT_DESCR( \
432 struct_, len_field_, JSON_TOK_ARRAY_START, \
433 Z_JSON_DESCR_ARRAY( \
434 elem_descr_, \
435 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
436 .n_elements = (max_len_), \
437 }, \
438 }, \
439 }
440
455#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
456 struct_field_name_, type_) \
457 { \
458 .field_name = (json_field_name_), \
459 .align_shift = Z_ALIGN_SHIFT(struct_), \
460 .field_name_len = sizeof(json_field_name_) - 1, \
461 .type = type_, \
462 .offset = offsetof(struct_, struct_field_name_), \
463 }
464
478#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
479 struct_field_name_, sub_descr_) \
480 { \
481 .field_name = (json_field_name_), \
482 .align_shift = Z_ALIGN_SHIFT(struct_), \
483 .field_name_len = (sizeof(json_field_name_) - 1), \
484 .type = JSON_TOK_OBJECT_START, \
485 .offset = offsetof(struct_, struct_field_name_), \
486 { \
487 .object = { \
488 .sub_descr = sub_descr_, \
489 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
490 }, \
491 }, \
492 }
493
510#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
511 struct_field_name_, max_len_, len_field_, \
512 elem_type_) \
513 { \
514 .field_name = (json_field_name_), \
515 .align_shift = Z_ALIGN_SHIFT(struct_), \
516 .field_name_len = sizeof(json_field_name_) - 1, \
517 .type = JSON_TOK_ARRAY_START, \
518 .offset = offsetof(struct_, struct_field_name_), \
519 { \
520 .array = { \
521 .element_descr = \
522 Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_,), \
523 .n_elements = (max_len_), \
524 }, \
525 }, \
526 }
527
568#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
569 struct_field_name_, max_len_, \
570 len_field_, elem_descr_, \
571 elem_descr_len_) \
572 { \
573 .field_name = json_field_name_, \
574 .align_shift = Z_ALIGN_SHIFT(struct_), \
575 .field_name_len = sizeof(json_field_name_) - 1, \
576 .type = JSON_TOK_ARRAY_START, \
577 .offset = offsetof(struct_, struct_field_name_), \
578 { \
579 .array = { \
580 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
581 JSON_TOK_OBJECT_START, \
582 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
583 .n_elements = (max_len_), \
584 }, \
585 }, \
586 }
587
618int64_t json_obj_parse(char *json, size_t len,
619 const struct json_obj_descr *descr, size_t descr_len,
620 void *val);
621
654int json_arr_parse(char *json, size_t len,
655 const struct json_obj_descr *descr, void *val);
656
673int json_arr_separate_object_parse_init(struct json_obj *json, char *payload, size_t len);
674
689int json_arr_separate_parse_object(struct json_obj *json, const struct json_obj_descr *descr,
690 size_t descr_len, void *val);
691
704ssize_t json_escape(char *str, size_t *len, size_t buf_size);
705
714size_t json_calc_escaped_len(const char *str, size_t len);
715
727 size_t descr_len, const void *val);
728
739 const void *val);
740
754int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len,
755 const void *val, char *buffer, size_t buf_size);
756
769int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val,
770 char *buffer, size_t buf_size);
771
785int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
786 const void *val, json_append_bytes_t append_bytes,
787 void *data);
788
801int json_arr_encode(const struct json_obj_descr *descr, const void *val,
802 json_append_bytes_t append_bytes, void *data);
803
804#ifdef __cplusplus
805}
806#endif
807
811#endif /* ZEPHYR_INCLUDE_DATA_JSON_H_ */
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
Definition: json.h:61
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
Definition: json.h:79
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
Definition: json.h:73
size_t length
Definition: json.h:75
char * start
Definition: json.h:74
Definition: json.h:69
struct json_lexer lex
Definition: json.h:70
Definition: json.h:55
char * start
Definition: json.h:57
enum json_tokens type
Definition: json.h:56
char * end
Definition: json.h:58
Macros to abstract toolchain specific capabilities.
Misc utilities.