Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
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 <sys/util.h>
11#include <stddef.h>
12#include <zephyr/types.h>
13#include <sys/types.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
32 /* Before changing this enum, ensure that its maximum
33 * value is still within 7 bits. See comment next to the
34 * declaration of `type` in struct json_obj_descr.
35 */
36
51};
52
54 const char *field_name;
55
56 /* Alignment can be 1, 2, 4, or 8. The macros to create
57 * a struct json_obj_descr will store the alignment's
58 * power of 2 in order to keep this value in the 0-3 range
59 * and thus use only 2 bits.
60 */
62
63 /* 127 characters is more than enough for a field name. */
65
66 /* Valid values here (enum json_tokens): JSON_TOK_STRING,
67 * JSON_TOK_NUMBER, JSON_TOK_TRUE, JSON_TOK_FALSE,
68 * JSON_TOK_OBJECT_START, JSON_TOK_LIST_START. (All others
69 * ignored.) Maximum value is '}' (125), so this has to be 7 bits
70 * long.
71 */
73
74 /* 65535 bytes is more than enough for many JSON payloads. */
76
77 union {
78 struct {
82 struct {
84 size_t n_elements;
86 };
87};
88
101typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
102 void *data);
103
104#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
105 __alignof__(type) == 2 ? 1 : \
106 __alignof__(type) == 4 ? 2 : 3)
107
128#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
129 { \
130 .field_name = (#field_name_), \
131 .align_shift = Z_ALIGN_SHIFT(struct_), \
132 .field_name_len = sizeof(#field_name_) - 1, \
133 .type = type_, \
134 .offset = offsetof(struct_, field_name_), \
135 }
136
161#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
162 { \
163 .field_name = (#field_name_), \
164 .align_shift = Z_ALIGN_SHIFT(struct_), \
165 .field_name_len = (sizeof(#field_name_) - 1), \
166 .type = JSON_TOK_OBJECT_START, \
167 .offset = offsetof(struct_, field_name_), \
168 { \
169 .object = { \
170 .sub_descr = sub_descr_, \
171 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
172 }, \
173 }, \
174 }
175
198#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
199 len_field_, elem_type_) \
200 { \
201 .field_name = (#field_name_), \
202 .align_shift = Z_ALIGN_SHIFT(struct_), \
203 .field_name_len = sizeof(#field_name_) - 1, \
204 .type = JSON_TOK_LIST_START, \
205 .offset = offsetof(struct_, field_name_), \
206 { \
207 .array = { \
208 .element_descr = (struct json_obj_descr[]) { { \
209 .align_shift = \
210 Z_ALIGN_SHIFT(struct_), \
211 .type = elem_type_, \
212 .offset = \
213 offsetof(struct_, \
214 len_field_), \
215 } }, \
216 .n_elements = (max_len_), \
217 }, \
218 }, \
219 }
220
255#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
256 len_field_, elem_descr_, elem_descr_len_) \
257 { \
258 .field_name = (#field_name_), \
259 .align_shift = Z_ALIGN_SHIFT(struct_), \
260 .field_name_len = sizeof(#field_name_) - 1, \
261 .type = JSON_TOK_LIST_START, \
262 .offset = offsetof(struct_, field_name_), \
263 { \
264 .array = { \
265 .element_descr = (struct json_obj_descr[]) { { \
266 .align_shift = \
267 Z_ALIGN_SHIFT(struct_), \
268 .type = JSON_TOK_OBJECT_START, \
269 .offset = offsetof(struct_, \
270 len_field_), \
271 { \
272 .object = { \
273 .sub_descr = \
274 elem_descr_, \
275 .sub_descr_len = \
276 elem_descr_len_, \
277 }, \
278 }, \
279 } }, \
280 .n_elements = (max_len_), \
281 }, \
282 }, \
283 }
284
328#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
329 elem_descr_, elem_descr_len_) \
330 { \
331 .field_name = (#field_name_), \
332 .align_shift = Z_ALIGN_SHIFT(struct_), \
333 .field_name_len = sizeof(#field_name_) - 1, \
334 .type = JSON_TOK_LIST_START, \
335 .offset = offsetof(struct_, field_name_), \
336 { \
337 .array = { \
338 .element_descr = (struct json_obj_descr[]) { { \
339 .align_shift = \
340 Z_ALIGN_SHIFT(struct_), \
341 .type = JSON_TOK_LIST_START, \
342 .offset = offsetof(struct_, \
343 len_field_), \
344 { \
345 .array = { \
346 .element_descr = \
347 elem_descr_, \
348 .n_elements = \
349 1 + \
350 ZERO_OR_COMPILE_ERROR( \
351 elem_descr_len_ == 1 \
352 ), \
353 }, \
354 }, \
355 } }, \
356 .n_elements = (max_len_), \
357 }, \
358 }, \
359 }
360
375#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
376 struct_field_name_, type_) \
377 { \
378 .field_name = (json_field_name_), \
379 .align_shift = Z_ALIGN_SHIFT(struct_), \
380 .field_name_len = sizeof(json_field_name_) - 1, \
381 .type = type_, \
382 .offset = offsetof(struct_, struct_field_name_), \
383 }
384
398#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
399 struct_field_name_, sub_descr_) \
400 { \
401 .field_name = (json_field_name_), \
402 .align_shift = Z_ALIGN_SHIFT(struct_), \
403 .field_name_len = (sizeof(json_field_name_) - 1), \
404 .type = JSON_TOK_OBJECT_START, \
405 .offset = offsetof(struct_, struct_field_name_), \
406 { \
407 .object = { \
408 .sub_descr = sub_descr_, \
409 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
410 }, \
411 }, \
412 }
413
430#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
431 struct_field_name_, max_len_, len_field_, \
432 elem_type_) \
433 { \
434 .field_name = (json_field_name_), \
435 .align_shift = Z_ALIGN_SHIFT(struct_), \
436 .field_name_len = sizeof(json_field_name_) - 1, \
437 .type = JSON_TOK_LIST_START, \
438 .offset = offsetof(struct_, struct_field_name_), \
439 { \
440 .array = { \
441 .element_descr = (struct json_obj_descr[]) { { \
442 .align_shift = \
443 Z_ALIGN_SHIFT(struct_), \
444 .type = elem_type_, \
445 .offset = offsetof(struct_, \
446 len_field_), \
447 } }, \
448 .n_elements = (max_len_), \
449 }, \
450 }, \
451 }
452
493#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
494 struct_field_name_, max_len_, \
495 len_field_, elem_descr_, \
496 elem_descr_len_) \
497 { \
498 .field_name = json_field_name_, \
499 .align_shift = Z_ALIGN_SHIFT(struct_), \
500 .field_name_len = sizeof(json_field_name_) - 1, \
501 .type = JSON_TOK_LIST_START, \
502 .offset = offsetof(struct_, struct_field_name_), \
503 { \
504 .array = { \
505 .element_descr = (struct json_obj_descr[]) { { \
506 .align_shift = \
507 Z_ALIGN_SHIFT(struct_), \
508 .type = JSON_TOK_OBJECT_START, \
509 .offset = offsetof(struct_, \
510 len_field_), \
511 { \
512 .object = { \
513 .sub_descr = \
514 elem_descr_, \
515 .sub_descr_len = \
516 elem_descr_len_, \
517 }, \
518 }, \
519 } }, \
520 .n_elements = (max_len_), \
521 }, \
522 }, \
523 }
524
555int json_obj_parse(char *json, size_t len,
556 const struct json_obj_descr *descr, size_t descr_len,
557 void *val);
558
571ssize_t json_escape(char *str, size_t *len, size_t buf_size);
572
581size_t json_calc_escaped_len(const char *str, size_t len);
582
594 size_t descr_len, const void *val);
595
609int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len,
610 const void *val, char *buffer, size_t buf_size);
611
624int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val,
625 char *buffer, size_t buf_size);
626
640int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
641 const void *val, json_append_bytes_t append_bytes,
642 void *data);
643
656int json_arr_encode(const struct json_obj_descr *descr, const void *val,
657 json_append_bytes_t append_bytes, void *data);
658
659#ifdef __cplusplus
660}
661#endif
662
666#endif /* ZEPHYR_INCLUDE_DATA_JSON_H_ */
json_tokens
Definition: json.h:31
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_obj_parse(char *json, size_t len, const struct json_obj_descr *descr, size_t descr_len, void *val)
Parses the JSON-encoded object pointer 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:101
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_COLON
Definition: json.h:43
@ JSON_TOK_LIST_END
Definition: json.h:41
@ JSON_TOK_COMMA
Definition: json.h:44
@ JSON_TOK_OBJECT_START
Definition: json.h:38
@ JSON_TOK_OBJECT_END
Definition: json.h:39
@ JSON_TOK_TRUE
Definition: json.h:46
@ JSON_TOK_FALSE
Definition: json.h:47
@ JSON_TOK_LIST_START
Definition: json.h:40
@ JSON_TOK_NONE
Definition: json.h:37
@ JSON_TOK_NULL
Definition: json.h:48
@ JSON_TOK_STRING
Definition: json.h:42
@ JSON_TOK_EOF
Definition: json.h:50
@ JSON_TOK_NUMBER
Definition: json.h:45
@ JSON_TOK_ERROR
Definition: json.h:49
static ZTEST_BMEM char buffer[8]
Test mailbox enhance capabilities.
Definition: test_mbox_api.c:566
__SIZE_TYPE__ ssize_t
Definition: types.h:28
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
Definition: json.h:53
const struct json_obj_descr * element_descr
Definition: json.h:83
const char * field_name
Definition: json.h:54
uint32_t align_shift
Definition: json.h:61
struct json_obj_descr::@95::@97 object
const struct json_obj_descr * sub_descr
Definition: json.h:79
uint32_t field_name_len
Definition: json.h:64
struct json_obj_descr::@95::@98 array
uint32_t offset
Definition: json.h:75
uint32_t type
Definition: json.h:72
size_t n_elements
Definition: json.h:84
size_t sub_descr_len
Definition: json.h:80
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
Misc utilities.