Zephyr Project API 4.2.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
58};
59
60struct json_token {
62 char *start;
63 char *end;
64};
65
66struct json_lexer {
67 void *(*state)(struct json_lexer *lex);
68 char *start;
69 char *pos;
70 char *end;
72};
73
74struct json_obj {
76};
77
79 char *start;
80 size_t length;
81};
82
83
85 const char *field_name;
86
87 /* Alignment can be 1, 2, 4, or 8. The macros to create
88 * a struct json_obj_descr will store the alignment's
89 * power of 2 in order to keep this value in the 0-3 range
90 * and thus use only 2 bits.
91 */
93
94 /* 127 characters is more than enough for a field name. */
96
97 /* Valid values here (enum json_tokens): JSON_TOK_STRING,
98 * JSON_TOK_NUMBER, JSON_TOK_TRUE, JSON_TOK_FALSE,
99 * JSON_TOK_OBJECT_START, JSON_TOK_ARRAY_START. (All others
100 * ignored.) Maximum value is '}' (125), so this has to be 7 bits
101 * long.
102 */
104
105 /* 65535 bytes is more than enough for many JSON payloads. */
107
108 union {
109 struct {
113 struct {
117 struct {
118 size_t size;
120 };
121};
122
135typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
136 void *data);
137
138#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
139 __alignof__(type) == 2 ? 1 : \
140 __alignof__(type) == 4 ? 2 : 3)
141
162#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
163 { \
164 .field_name = (#field_name_), \
165 .align_shift = Z_ALIGN_SHIFT(struct_), \
166 .field_name_len = sizeof(#field_name_) - 1, \
167 .type = type_, \
168 .offset = offsetof(struct_, field_name_), \
169 .field = { \
170 .size = SIZEOF_FIELD(struct_, field_name_) \
171 }, \
172 }
173
198#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
199 { \
200 .field_name = (#field_name_), \
201 .align_shift = Z_ALIGN_SHIFT(struct_), \
202 .field_name_len = (sizeof(#field_name_) - 1), \
203 .type = JSON_TOK_OBJECT_START, \
204 .offset = offsetof(struct_, field_name_), \
205 .object = { \
206 .sub_descr = sub_descr_, \
207 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
208 }, \
209 }
210
220#define Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_, union_) \
221 (const struct json_obj_descr[]) \
222 { \
223 { \
224 .align_shift = Z_ALIGN_SHIFT(struct_), \
225 .type = elem_type_, \
226 .offset = offsetof(struct_, len_field_), \
227 union_ \
228 } \
229 }
230
237#define Z_JSON_DESCR_ARRAY(elem_descr_, elem_descr_len_) \
238 { \
239 .array = { \
240 .element_descr = elem_descr_, \
241 .n_elements = elem_descr_len_, \
242 }, \
243 }
244
251#define Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_) \
252 .object = { \
253 .sub_descr = elem_descr_, \
254 .sub_descr_len = elem_descr_len_, \
255 }, \
256
263#define Z_JSON_DESCR_FIELD(struct_, field_name_) \
264 { \
265 .field = { \
266 .size = SIZEOF_FIELD(struct_, field_name_), \
267 }, \
268 }
269
292#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
293 len_field_, elem_type_) \
294 { \
295 .field_name = (#field_name_), \
296 .align_shift = Z_ALIGN_SHIFT(struct_), \
297 .field_name_len = sizeof(#field_name_) - 1, \
298 .type = JSON_TOK_ARRAY_START, \
299 .offset = offsetof(struct_, field_name_), \
300 .array = { \
301 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
302 elem_type_, \
303 Z_JSON_DESCR_FIELD(struct_, field_name_[0])), \
304 .n_elements = (max_len_), \
305 }, \
306 }
307
342#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
343 len_field_, elem_descr_, elem_descr_len_) \
344 { \
345 .field_name = (#field_name_), \
346 .align_shift = Z_ALIGN_SHIFT(struct_), \
347 .field_name_len = sizeof(#field_name_) - 1, \
348 .type = JSON_TOK_ARRAY_START, \
349 .offset = offsetof(struct_, field_name_), \
350 .array = { \
351 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
352 JSON_TOK_OBJECT_START, \
353 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
354 .n_elements = (max_len_), \
355 }, \
356 }
357
401#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
402 elem_descr_, elem_descr_len_) \
403 { \
404 .field_name = (#field_name_), \
405 .align_shift = Z_ALIGN_SHIFT(struct_), \
406 .field_name_len = sizeof(#field_name_) - 1, \
407 .type = JSON_TOK_ARRAY_START, \
408 .offset = offsetof(struct_, field_name_), \
409 .array = { \
410 .element_descr = Z_JSON_ELEMENT_DESCR( \
411 struct_, len_field_, JSON_TOK_ARRAY_START, \
412 Z_JSON_DESCR_ARRAY( \
413 elem_descr_, \
414 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
415 .n_elements = (max_len_), \
416 }, \
417 }
418
436#define JSON_OBJ_DESCR_ARRAY_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, \
437 max_len_, len_field_, elem_descr_, elem_descr_len_) \
438 { \
439 .field_name = (#json_field_name_), \
440 .align_shift = Z_ALIGN_SHIFT(struct_), \
441 .field_name_len = sizeof(#json_field_name_) - 1, \
442 .type = JSON_TOK_ARRAY_START, \
443 .offset = offsetof(struct_, struct_field_name_), \
444 .array = { \
445 .element_descr = Z_JSON_ELEMENT_DESCR( \
446 struct_, len_field_, JSON_TOK_ARRAY_START, \
447 Z_JSON_DESCR_ARRAY( \
448 elem_descr_, \
449 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
450 .n_elements = (max_len_), \
451 }, \
452 }
453
468#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
469 struct_field_name_, type_) \
470 { \
471 .field_name = (json_field_name_), \
472 .align_shift = Z_ALIGN_SHIFT(struct_), \
473 .field_name_len = sizeof(json_field_name_) - 1, \
474 .type = type_, \
475 .offset = offsetof(struct_, struct_field_name_), \
476 .field = { \
477 .size = SIZEOF_FIELD(struct_, struct_field_name_) \
478 }, \
479 }
480
494#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
495 struct_field_name_, sub_descr_) \
496 { \
497 .field_name = (json_field_name_), \
498 .align_shift = Z_ALIGN_SHIFT(struct_), \
499 .field_name_len = (sizeof(json_field_name_) - 1), \
500 .type = JSON_TOK_OBJECT_START, \
501 .offset = offsetof(struct_, struct_field_name_), \
502 .object = { \
503 .sub_descr = sub_descr_, \
504 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
505 }, \
506 }
507
524#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
525 struct_field_name_, max_len_, len_field_, \
526 elem_type_) \
527 { \
528 .field_name = (json_field_name_), \
529 .align_shift = Z_ALIGN_SHIFT(struct_), \
530 .field_name_len = sizeof(json_field_name_) - 1, \
531 .type = JSON_TOK_ARRAY_START, \
532 .offset = offsetof(struct_, struct_field_name_), \
533 .array = { \
534 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
535 elem_type_, \
536 Z_JSON_DESCR_FIELD(struct_, struct_field_name_[0])), \
537 .n_elements = (max_len_), \
538 }, \
539 }
540
581#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
582 struct_field_name_, max_len_, \
583 len_field_, elem_descr_, \
584 elem_descr_len_) \
585 { \
586 .field_name = json_field_name_, \
587 .align_shift = Z_ALIGN_SHIFT(struct_), \
588 .field_name_len = sizeof(json_field_name_) - 1, \
589 .type = JSON_TOK_ARRAY_START, \
590 .offset = offsetof(struct_, struct_field_name_), \
591 .array = { \
592 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
593 JSON_TOK_OBJECT_START, \
594 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
595 .n_elements = (max_len_), \
596 }, \
597 }
598
629int64_t json_obj_parse(char *json, size_t len,
630 const struct json_obj_descr *descr, size_t descr_len,
631 void *val);
632
665int json_arr_parse(char *json, size_t len,
666 const struct json_obj_descr *descr, void *val);
667
684int json_arr_separate_object_parse_init(struct json_obj *json, char *payload, size_t len);
685
700int json_arr_separate_parse_object(struct json_obj *json, const struct json_obj_descr *descr,
701 size_t descr_len, void *val);
702
715ssize_t json_escape(char *str, size_t *len, size_t buf_size);
716
725size_t json_calc_escaped_len(const char *str, size_t len);
726
738 size_t descr_len, const void *val);
739
750 const void *val);
751
765int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len,
766 const void *val, char *buffer, size_t buf_size);
767
780int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val,
781 char *buffer, size_t buf_size);
782
796int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
797 const void *val, json_append_bytes_t append_bytes,
798 void *data);
799
812int json_arr_encode(const struct json_obj_descr *descr, const void *val,
813 json_append_bytes_t append_bytes, void *data);
814
833
834 union {
835 struct {
836 size_t offset;
837 size_t size;
839
840 struct {
843 size_t offset;
845
846 struct {
849 size_t offset;
851
852 struct {
854 size_t sub_descr_len;
855 size_t offset;
857 };
858};
859
874#define JSON_MIXED_ARR_DESCR_PRIM(struct_, field_name_, type_, count_field_) \
875{ \
876 .type = type_, \
877 .count_offset = offsetof(struct_, count_field_), \
878 .primitive = { \
879 .offset = offsetof(struct_, field_name_), \
880 .size = SIZEOF_FIELD(struct_, field_name_) \
881 } \
882}
883
900#define JSON_MIXED_ARR_DESCR_OBJECT(struct_, field_name_, sub_descr_, count_field_) \
901{ \
902 .type = JSON_TOK_OBJECT_START, \
903 .count_offset = offsetof(struct_, count_field_), \
904 .object = { \
905 .sub_descr = sub_descr_, \
906 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
907 .offset = offsetof(struct_, field_name_) \
908 } \
909}
910
932#define JSON_MIXED_ARR_DESCR_ARRAY(struct_, field_name_, max_len_, elem_descr_, count_field_) \
933{ \
934 .type = JSON_TOK_ARRAY_START, \
935 .count_offset = offsetof(struct_, count_field_), \
936 .array = { \
937 .element_descr = elem_descr_, \
938 .n_elements = (max_len_), \
939 .offset = offsetof(struct_, field_name_) \
940 } \
941}
942
958#define JSON_MIXED_ARR_DESCR_MIXED_ARRAY(struct_, field_name_, sub_descr_, count_field_) \
959{ \
960 .type = JSON_TOK_MIXED_ARRAY, \
961 .count_offset = offsetof(struct_, count_field_), \
962 .mixed_array = { \
963 .sub_descr = sub_descr_, \
964 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
965 .offset = offsetof(struct_, field_name_) \
966 } \
967}
968
984int json_mixed_arr_parse(char *json, size_t len,
985 const struct json_mixed_arr_descr *descr,
986 size_t descr_len, void *val);
987
1004 size_t descr_len, void *val,
1005 json_append_bytes_t append_bytes,
1006 void *data);
1007
1023 size_t descr_len, void *val,
1024 char *buffer, size_t buf_size);
1025
1039 size_t descr_len, void *val);
1040
1041#ifdef __cplusplus
1042}
1043#endif
1044
1048#endif /* ZEPHYR_INCLUDE_DATA_JSON_H_ */
json_tokens
Definition json.h:26
ssize_t json_calc_mixed_arr_len(const struct json_mixed_arr_descr *descr, size_t descr_len, void *val)
Calculate the length of the encoded JSON mixed array.
int json_mixed_arr_encode(const struct json_mixed_arr_descr *descr, size_t descr_len, void *val, json_append_bytes_t append_bytes, void *data)
Encode a C structure as a JSON mixed array.
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.
int json_mixed_arr_parse(char *json, size_t len, const struct json_mixed_arr_descr *descr, size_t descr_len, void *val)
Parse a JSON mixed array into a C structure.
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.
int json_mixed_arr_encode_buf(const struct json_mixed_arr_descr *descr, size_t descr_len, void *val, char *buffer, size_t buf_size)
Encode a C structure as a JSON mixed array into a buffer.
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:135
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_STRING_BUF
Definition json.h:38
@ JSON_TOK_OBJ_ARRAY
Definition json.h:44
@ JSON_TOK_UINT
Definition json.h:51
@ JSON_TOK_ARRAY_END
Definition json.h:36
@ JSON_TOK_INT64
Definition json.h:46
@ JSON_TOK_COLON
Definition json.h:39
@ JSON_TOK_UINT64
Definition json.h:47
@ JSON_TOK_MIXED_ARRAY
Definition json.h:54
@ JSON_TOK_DOUBLE_FP
Definition json.h:49
@ JSON_TOK_COMMA
Definition json.h:40
@ JSON_TOK_INT
Definition json.h:50
@ JSON_TOK_OBJECT_START
Definition json.h:33
@ JSON_TOK_OBJECT_END
Definition json.h:34
@ JSON_TOK_TRUE
Definition json.h:52
@ JSON_TOK_FALSE
Definition json.h:53
@ JSON_TOK_NONE
Definition json.h:32
@ JSON_TOK_NULL
Definition json.h:55
@ JSON_TOK_OPAQUE
Definition json.h:43
@ JSON_TOK_ARRAY_START
Definition json.h:35
@ JSON_TOK_FLOAT_FP
Definition json.h:48
@ JSON_TOK_FLOAT
Definition json.h:42
@ JSON_TOK_STRING
Definition json.h:37
@ JSON_TOK_EOF
Definition json.h:57
@ JSON_TOK_NUMBER
Definition json.h:41
@ JSON_TOK_ENCODED_OBJ
Definition json.h:45
@ JSON_TOK_ERROR
Definition json.h:56
__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:66
char * pos
Definition json.h:69
char * start
Definition json.h:68
struct json_token tok
Definition json.h:71
char * end
Definition json.h:70
Descriptor for a mixed-type JSON array.
Definition json.h:830
struct json_mixed_arr_descr::@174::@179 mixed_array
struct json_mixed_arr_descr::@174::@177 object
struct json_mixed_arr_descr::@174::@176 primitive
const struct json_obj_descr * sub_descr
Definition json.h:841
size_t count_offset
Definition json.h:832
size_t size
Definition json.h:837
size_t offset
Definition json.h:836
size_t n_elements
Definition json.h:848
const struct json_mixed_arr_descr * sub_descr
Definition json.h:853
struct json_mixed_arr_descr::@174::@178 array
size_t sub_descr_len
Definition json.h:842
const struct json_obj_descr * element_descr
Definition json.h:847
uint32_t type
Definition json.h:831
Definition json.h:84
const struct json_obj_descr * element_descr
Definition json.h:114
const char * field_name
Definition json.h:85
uint32_t align_shift
Definition json.h:92
const struct json_obj_descr * sub_descr
Definition json.h:110
struct json_obj_descr::@169::@173 field
uint32_t field_name_len
Definition json.h:95
struct json_obj_descr::@169::@171 object
uint32_t offset
Definition json.h:106
uint32_t type
Definition json.h:103
size_t size
Definition json.h:118
size_t n_elements
Definition json.h:115
size_t sub_descr_len
Definition json.h:111
struct json_obj_descr::@169::@172 array
Definition json.h:78
size_t length
Definition json.h:80
char * start
Definition json.h:79
Definition json.h:74
struct json_lexer lex
Definition json.h:75
Definition json.h:60
char * start
Definition json.h:62
enum json_tokens type
Definition json.h:61
char * end
Definition json.h:63
Misc utilities.
Macros to abstract toolchain specific capabilities.