Zephyr Project API 3.7.0
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
50};
51
52struct json_token {
54 char *start;
55 char *end;
56};
57
58struct json_lexer {
59 void *(*state)(struct json_lexer *lex);
60 char *start;
61 char *pos;
62 char *end;
64};
65
66struct json_obj {
68};
69
71 char *start;
72 size_t length;
73};
74
75
77 const char *field_name;
78
79 /* Alignment can be 1, 2, 4, or 8. The macros to create
80 * a struct json_obj_descr will store the alignment's
81 * power of 2 in order to keep this value in the 0-3 range
82 * and thus use only 2 bits.
83 */
85
86 /* 127 characters is more than enough for a field name. */
88
89 /* Valid values here (enum json_tokens): JSON_TOK_STRING,
90 * JSON_TOK_NUMBER, JSON_TOK_TRUE, JSON_TOK_FALSE,
91 * JSON_TOK_OBJECT_START, JSON_TOK_ARRAY_START. (All others
92 * ignored.) Maximum value is '}' (125), so this has to be 7 bits
93 * long.
94 */
96
97 /* 65535 bytes is more than enough for many JSON payloads. */
99
100 union {
101 struct {
105 struct {
109 };
110};
111
124typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
125 void *data);
126
127#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
128 __alignof__(type) == 2 ? 1 : \
129 __alignof__(type) == 4 ? 2 : 3)
130
151#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
152 { \
153 .field_name = (#field_name_), \
154 .align_shift = Z_ALIGN_SHIFT(struct_), \
155 .field_name_len = sizeof(#field_name_) - 1, \
156 .type = type_, \
157 .offset = offsetof(struct_, field_name_), \
158 }
159
184#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
185 { \
186 .field_name = (#field_name_), \
187 .align_shift = Z_ALIGN_SHIFT(struct_), \
188 .field_name_len = (sizeof(#field_name_) - 1), \
189 .type = JSON_TOK_OBJECT_START, \
190 .offset = offsetof(struct_, field_name_), \
191 .object = { \
192 .sub_descr = sub_descr_, \
193 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
194 }, \
195 }
196
206#define Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_, union_) \
207 (const struct json_obj_descr[]) \
208 { \
209 { \
210 .align_shift = Z_ALIGN_SHIFT(struct_), \
211 .type = elem_type_, \
212 .offset = offsetof(struct_, len_field_), \
213 union_ \
214 } \
215 }
216
223#define Z_JSON_DESCR_ARRAY(elem_descr_, elem_descr_len_) \
224 { \
225 .array = { \
226 .element_descr = elem_descr_, \
227 .n_elements = elem_descr_len_, \
228 }, \
229 }
230
237#define Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_) \
238 { \
239 .object = { \
240 .sub_descr = elem_descr_, \
241 .sub_descr_len = elem_descr_len_, \
242 }, \
243 }
244
267#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
268 len_field_, elem_type_) \
269 { \
270 .field_name = (#field_name_), \
271 .align_shift = Z_ALIGN_SHIFT(struct_), \
272 .field_name_len = sizeof(#field_name_) - 1, \
273 .type = JSON_TOK_ARRAY_START, \
274 .offset = offsetof(struct_, field_name_), \
275 .array = { \
276 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
277 elem_type_,), \
278 .n_elements = (max_len_), \
279 }, \
280 }
281
316#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
317 len_field_, elem_descr_, elem_descr_len_) \
318 { \
319 .field_name = (#field_name_), \
320 .align_shift = Z_ALIGN_SHIFT(struct_), \
321 .field_name_len = sizeof(#field_name_) - 1, \
322 .type = JSON_TOK_ARRAY_START, \
323 .offset = offsetof(struct_, field_name_), \
324 .array = { \
325 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
326 JSON_TOK_OBJECT_START, \
327 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
328 .n_elements = (max_len_), \
329 }, \
330 }
331
375#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
376 elem_descr_, elem_descr_len_) \
377 { \
378 .field_name = (#field_name_), \
379 .align_shift = Z_ALIGN_SHIFT(struct_), \
380 .field_name_len = sizeof(#field_name_) - 1, \
381 .type = JSON_TOK_ARRAY_START, \
382 .offset = offsetof(struct_, field_name_), \
383 .array = { \
384 .element_descr = Z_JSON_ELEMENT_DESCR( \
385 struct_, len_field_, JSON_TOK_ARRAY_START, \
386 Z_JSON_DESCR_ARRAY( \
387 elem_descr_, \
388 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
389 .n_elements = (max_len_), \
390 }, \
391 }
392
410#define JSON_OBJ_DESCR_ARRAY_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, \
411 max_len_, len_field_, elem_descr_, elem_descr_len_) \
412 { \
413 .field_name = (#json_field_name_), \
414 .align_shift = Z_ALIGN_SHIFT(struct_), \
415 .field_name_len = sizeof(#json_field_name_) - 1, \
416 .type = JSON_TOK_ARRAY_START, \
417 .offset = offsetof(struct_, struct_field_name_), \
418 .array = { \
419 .element_descr = Z_JSON_ELEMENT_DESCR( \
420 struct_, len_field_, JSON_TOK_ARRAY_START, \
421 Z_JSON_DESCR_ARRAY( \
422 elem_descr_, \
423 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
424 .n_elements = (max_len_), \
425 }, \
426 }
427
442#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
443 struct_field_name_, type_) \
444 { \
445 .field_name = (json_field_name_), \
446 .align_shift = Z_ALIGN_SHIFT(struct_), \
447 .field_name_len = sizeof(json_field_name_) - 1, \
448 .type = type_, \
449 .offset = offsetof(struct_, struct_field_name_), \
450 }
451
465#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
466 struct_field_name_, sub_descr_) \
467 { \
468 .field_name = (json_field_name_), \
469 .align_shift = Z_ALIGN_SHIFT(struct_), \
470 .field_name_len = (sizeof(json_field_name_) - 1), \
471 .type = JSON_TOK_OBJECT_START, \
472 .offset = offsetof(struct_, struct_field_name_), \
473 .object = { \
474 .sub_descr = sub_descr_, \
475 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
476 }, \
477 }
478
495#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
496 struct_field_name_, max_len_, len_field_, \
497 elem_type_) \
498 { \
499 .field_name = (json_field_name_), \
500 .align_shift = Z_ALIGN_SHIFT(struct_), \
501 .field_name_len = sizeof(json_field_name_) - 1, \
502 .type = JSON_TOK_ARRAY_START, \
503 .offset = offsetof(struct_, struct_field_name_), \
504 .array = { \
505 .element_descr = \
506 Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_,), \
507 .n_elements = (max_len_), \
508 }, \
509 }
510
551#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
552 struct_field_name_, max_len_, \
553 len_field_, elem_descr_, \
554 elem_descr_len_) \
555 { \
556 .field_name = json_field_name_, \
557 .align_shift = Z_ALIGN_SHIFT(struct_), \
558 .field_name_len = sizeof(json_field_name_) - 1, \
559 .type = JSON_TOK_ARRAY_START, \
560 .offset = offsetof(struct_, struct_field_name_), \
561 .array = { \
562 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
563 JSON_TOK_OBJECT_START, \
564 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
565 .n_elements = (max_len_), \
566 }, \
567 }
568
599int64_t json_obj_parse(char *json, size_t len,
600 const struct json_obj_descr *descr, size_t descr_len,
601 void *val);
602
635int json_arr_parse(char *json, size_t len,
636 const struct json_obj_descr *descr, void *val);
637
654int json_arr_separate_object_parse_init(struct json_obj *json, char *payload, size_t len);
655
670int json_arr_separate_parse_object(struct json_obj *json, const struct json_obj_descr *descr,
671 size_t descr_len, void *val);
672
685ssize_t json_escape(char *str, size_t *len, size_t buf_size);
686
695size_t json_calc_escaped_len(const char *str, size_t len);
696
708 size_t descr_len, const void *val);
709
720 const void *val);
721
735int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len,
736 const void *val, char *buffer, size_t buf_size);
737
750int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val,
751 char *buffer, size_t buf_size);
752
766int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
767 const void *val, json_append_bytes_t append_bytes,
768 void *data);
769
782int json_arr_encode(const struct json_obj_descr *descr, const void *val,
783 json_append_bytes_t append_bytes, void *data);
784
785#ifdef __cplusplus
786}
787#endif
788
792#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:124
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_COLON
Definition json.h:38
@ 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:45
@ JSON_TOK_FALSE
Definition json.h:46
@ JSON_TOK_NONE
Definition json.h:32
@ JSON_TOK_NULL
Definition json.h:47
@ 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:49
@ JSON_TOK_NUMBER
Definition json.h:40
@ JSON_TOK_ENCODED_OBJ
Definition json.h:44
@ JSON_TOK_ERROR
Definition json.h:48
__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:58
char * pos
Definition json.h:61
char * start
Definition json.h:60
struct json_token tok
Definition json.h:63
char * end
Definition json.h:62
Definition json.h:76
const struct json_obj_descr * element_descr
Definition json.h:106
const char * field_name
Definition json.h:77
uint32_t align_shift
Definition json.h:84
const struct json_obj_descr * sub_descr
Definition json.h:102
uint32_t field_name_len
Definition json.h:87
uint32_t offset
Definition json.h:98
uint32_t type
Definition json.h:95
struct json_obj_descr::@126::@128 object
size_t n_elements
Definition json.h:107
size_t sub_descr_len
Definition json.h:103
struct json_obj_descr::@126::@129 array
Definition json.h:70
size_t length
Definition json.h:72
char * start
Definition json.h:71
Definition json.h:66
struct json_lexer lex
Definition json.h:67
Definition json.h:52
char * start
Definition json.h:54
enum json_tokens type
Definition json.h:53
char * end
Definition json.h:55
static fdata_t data[2]
Definition test_fifo_contexts.c:15
static ZTEST_BMEM char buffer[8]
Definition test_mbox_api.c:433
Macros to abstract toolchain specific capabilities.
Misc utilities.