Zephyr API Documentation 4.0.0-rc2
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
52};
53
54struct json_token {
56 char *start;
57 char *end;
58};
59
60struct json_lexer {
61 void *(*state)(struct json_lexer *lex);
62 char *start;
63 char *pos;
64 char *end;
66};
67
68struct json_obj {
70};
71
73 char *start;
74 size_t length;
75};
76
77
79 const char *field_name;
80
81 /* Alignment can be 1, 2, 4, or 8. The macros to create
82 * a struct json_obj_descr will store the alignment's
83 * power of 2 in order to keep this value in the 0-3 range
84 * and thus use only 2 bits.
85 */
87
88 /* 127 characters is more than enough for a field name. */
90
91 /* Valid values here (enum json_tokens): JSON_TOK_STRING,
92 * JSON_TOK_NUMBER, JSON_TOK_TRUE, JSON_TOK_FALSE,
93 * JSON_TOK_OBJECT_START, JSON_TOK_ARRAY_START. (All others
94 * ignored.) Maximum value is '}' (125), so this has to be 7 bits
95 * long.
96 */
98
99 /* 65535 bytes is more than enough for many JSON payloads. */
101
102 union {
103 struct {
107 struct {
111 };
112};
113
126typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
127 void *data);
128
129#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
130 __alignof__(type) == 2 ? 1 : \
131 __alignof__(type) == 4 ? 2 : 3)
132
153#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
154 { \
155 .field_name = (#field_name_), \
156 .align_shift = Z_ALIGN_SHIFT(struct_), \
157 .field_name_len = sizeof(#field_name_) - 1, \
158 .type = type_, \
159 .offset = offsetof(struct_, field_name_), \
160 }
161
186#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
187 { \
188 .field_name = (#field_name_), \
189 .align_shift = Z_ALIGN_SHIFT(struct_), \
190 .field_name_len = (sizeof(#field_name_) - 1), \
191 .type = JSON_TOK_OBJECT_START, \
192 .offset = offsetof(struct_, field_name_), \
193 .object = { \
194 .sub_descr = sub_descr_, \
195 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
196 }, \
197 }
198
208#define Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_, union_) \
209 (const struct json_obj_descr[]) \
210 { \
211 { \
212 .align_shift = Z_ALIGN_SHIFT(struct_), \
213 .type = elem_type_, \
214 .offset = offsetof(struct_, len_field_), \
215 union_ \
216 } \
217 }
218
225#define Z_JSON_DESCR_ARRAY(elem_descr_, elem_descr_len_) \
226 { \
227 .array = { \
228 .element_descr = elem_descr_, \
229 .n_elements = elem_descr_len_, \
230 }, \
231 }
232
239#define Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_) \
240 { \
241 .object = { \
242 .sub_descr = elem_descr_, \
243 .sub_descr_len = elem_descr_len_, \
244 }, \
245 }
246
269#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
270 len_field_, elem_type_) \
271 { \
272 .field_name = (#field_name_), \
273 .align_shift = Z_ALIGN_SHIFT(struct_), \
274 .field_name_len = sizeof(#field_name_) - 1, \
275 .type = JSON_TOK_ARRAY_START, \
276 .offset = offsetof(struct_, field_name_), \
277 .array = { \
278 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
279 elem_type_,), \
280 .n_elements = (max_len_), \
281 }, \
282 }
283
318#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
319 len_field_, elem_descr_, elem_descr_len_) \
320 { \
321 .field_name = (#field_name_), \
322 .align_shift = Z_ALIGN_SHIFT(struct_), \
323 .field_name_len = sizeof(#field_name_) - 1, \
324 .type = JSON_TOK_ARRAY_START, \
325 .offset = offsetof(struct_, field_name_), \
326 .array = { \
327 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
328 JSON_TOK_OBJECT_START, \
329 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
330 .n_elements = (max_len_), \
331 }, \
332 }
333
377#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
378 elem_descr_, elem_descr_len_) \
379 { \
380 .field_name = (#field_name_), \
381 .align_shift = Z_ALIGN_SHIFT(struct_), \
382 .field_name_len = sizeof(#field_name_) - 1, \
383 .type = JSON_TOK_ARRAY_START, \
384 .offset = offsetof(struct_, field_name_), \
385 .array = { \
386 .element_descr = Z_JSON_ELEMENT_DESCR( \
387 struct_, len_field_, JSON_TOK_ARRAY_START, \
388 Z_JSON_DESCR_ARRAY( \
389 elem_descr_, \
390 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
391 .n_elements = (max_len_), \
392 }, \
393 }
394
412#define JSON_OBJ_DESCR_ARRAY_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, \
413 max_len_, len_field_, elem_descr_, elem_descr_len_) \
414 { \
415 .field_name = (#json_field_name_), \
416 .align_shift = Z_ALIGN_SHIFT(struct_), \
417 .field_name_len = sizeof(#json_field_name_) - 1, \
418 .type = JSON_TOK_ARRAY_START, \
419 .offset = offsetof(struct_, struct_field_name_), \
420 .array = { \
421 .element_descr = Z_JSON_ELEMENT_DESCR( \
422 struct_, len_field_, JSON_TOK_ARRAY_START, \
423 Z_JSON_DESCR_ARRAY( \
424 elem_descr_, \
425 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
426 .n_elements = (max_len_), \
427 }, \
428 }
429
444#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
445 struct_field_name_, type_) \
446 { \
447 .field_name = (json_field_name_), \
448 .align_shift = Z_ALIGN_SHIFT(struct_), \
449 .field_name_len = sizeof(json_field_name_) - 1, \
450 .type = type_, \
451 .offset = offsetof(struct_, struct_field_name_), \
452 }
453
467#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
468 struct_field_name_, sub_descr_) \
469 { \
470 .field_name = (json_field_name_), \
471 .align_shift = Z_ALIGN_SHIFT(struct_), \
472 .field_name_len = (sizeof(json_field_name_) - 1), \
473 .type = JSON_TOK_OBJECT_START, \
474 .offset = offsetof(struct_, struct_field_name_), \
475 .object = { \
476 .sub_descr = sub_descr_, \
477 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
478 }, \
479 }
480
497#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
498 struct_field_name_, max_len_, len_field_, \
499 elem_type_) \
500 { \
501 .field_name = (json_field_name_), \
502 .align_shift = Z_ALIGN_SHIFT(struct_), \
503 .field_name_len = sizeof(json_field_name_) - 1, \
504 .type = JSON_TOK_ARRAY_START, \
505 .offset = offsetof(struct_, struct_field_name_), \
506 .array = { \
507 .element_descr = \
508 Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_,), \
509 .n_elements = (max_len_), \
510 }, \
511 }
512
553#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
554 struct_field_name_, max_len_, \
555 len_field_, elem_descr_, \
556 elem_descr_len_) \
557 { \
558 .field_name = json_field_name_, \
559 .align_shift = Z_ALIGN_SHIFT(struct_), \
560 .field_name_len = sizeof(json_field_name_) - 1, \
561 .type = JSON_TOK_ARRAY_START, \
562 .offset = offsetof(struct_, struct_field_name_), \
563 .array = { \
564 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
565 JSON_TOK_OBJECT_START, \
566 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
567 .n_elements = (max_len_), \
568 }, \
569 }
570
601int64_t json_obj_parse(char *json, size_t len,
602 const struct json_obj_descr *descr, size_t descr_len,
603 void *val);
604
637int json_arr_parse(char *json, size_t len,
638 const struct json_obj_descr *descr, void *val);
639
656int json_arr_separate_object_parse_init(struct json_obj *json, char *payload, size_t len);
657
672int json_arr_separate_parse_object(struct json_obj *json, const struct json_obj_descr *descr,
673 size_t descr_len, void *val);
674
687ssize_t json_escape(char *str, size_t *len, size_t buf_size);
688
697size_t json_calc_escaped_len(const char *str, size_t len);
698
710 size_t descr_len, const void *val);
711
722 const void *val);
723
737int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len,
738 const void *val, char *buffer, size_t buf_size);
739
752int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val,
753 char *buffer, size_t buf_size);
754
768int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
769 const void *val, json_append_bytes_t append_bytes,
770 void *data);
771
784int json_arr_encode(const struct json_obj_descr *descr, const void *val,
785 json_append_bytes_t append_bytes, void *data);
786
787#ifdef __cplusplus
788}
789#endif
790
794#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:126
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:43
@ JSON_TOK_ARRAY_END
Definition json.h:36
@ JSON_TOK_INT64
Definition json.h:45
@ JSON_TOK_COLON
Definition json.h:38
@ JSON_TOK_UINT64
Definition json.h:46
@ JSON_TOK_COMMA
Definition json.h:39
@ JSON_TOK_OBJECT_START
Definition json.h:33
@ JSON_TOK_OBJECT_END
Definition json.h:34
@ JSON_TOK_TRUE
Definition json.h:47
@ JSON_TOK_FALSE
Definition json.h:48
@ JSON_TOK_NONE
Definition json.h:32
@ JSON_TOK_NULL
Definition json.h:49
@ JSON_TOK_OPAQUE
Definition json.h:42
@ JSON_TOK_ARRAY_START
Definition json.h:35
@ JSON_TOK_FLOAT
Definition json.h:41
@ JSON_TOK_STRING
Definition json.h:37
@ JSON_TOK_EOF
Definition json.h:51
@ JSON_TOK_NUMBER
Definition json.h:40
@ JSON_TOK_ENCODED_OBJ
Definition json.h:44
@ JSON_TOK_ERROR
Definition json.h:50
__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:60
char * pos
Definition json.h:63
char * start
Definition json.h:62
struct json_token tok
Definition json.h:65
char * end
Definition json.h:64
Definition json.h:78
const struct json_obj_descr * element_descr
Definition json.h:108
const char * field_name
Definition json.h:79
uint32_t align_shift
Definition json.h:86
const struct json_obj_descr * sub_descr
Definition json.h:104
uint32_t field_name_len
Definition json.h:89
uint32_t offset
Definition json.h:100
uint32_t type
Definition json.h:97
struct json_obj_descr::@176::@179 array
struct json_obj_descr::@176::@178 object
size_t n_elements
Definition json.h:109
size_t sub_descr_len
Definition json.h:105
Definition json.h:72
size_t length
Definition json.h:74
char * start
Definition json.h:73
Definition json.h:68
struct json_lexer lex
Definition json.h:69
Definition json.h:54
char * start
Definition json.h:56
enum json_tokens type
Definition json.h:55
char * end
Definition json.h:57
Misc utilities.
Macros to abstract toolchain specific capabilities.