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