Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 <toolchain.h>
13#include <zephyr/types.h>
14#include <sys/types.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
33 /* Before changing this enum, ensure that its maximum
34 * value is still within 7 bits. See comment next to the
35 * declaration of `type` in struct json_obj_descr.
36 */
37
41 /* JSON_TOK_LIST_START will be removed use JSON_TOK_ARRAY_START */
42 JSON_TOK_LIST_START __deprecated = '[',
44 /* JSON_TOK_LIST_END will be removed use JSON_TOK_ARRAY_END */
45 JSON_TOK_LIST_END __deprecated = ']',
56};
57
59 const char *field_name;
60
61 /* Alignment can be 1, 2, 4, or 8. The macros to create
62 * a struct json_obj_descr will store the alignment's
63 * power of 2 in order to keep this value in the 0-3 range
64 * and thus use only 2 bits.
65 */
67
68 /* 127 characters is more than enough for a field name. */
70
71 /* Valid values here (enum json_tokens): JSON_TOK_STRING,
72 * JSON_TOK_NUMBER, JSON_TOK_TRUE, JSON_TOK_FALSE,
73 * JSON_TOK_OBJECT_START, JSON_TOK_ARRAY_START. (All others
74 * ignored.) Maximum value is '}' (125), so this has to be 7 bits
75 * long.
76 */
78
79 /* 65535 bytes is more than enough for many JSON payloads. */
81
82 union {
83 struct {
87 struct {
89 size_t n_elements;
91 };
92};
93
106typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
107 void *data);
108
109#define Z_ALIGN_SHIFT(type) (__alignof__(type) == 1 ? 0 : \
110 __alignof__(type) == 2 ? 1 : \
111 __alignof__(type) == 4 ? 2 : 3)
112
133#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \
134 { \
135 .field_name = (#field_name_), \
136 .align_shift = Z_ALIGN_SHIFT(struct_), \
137 .field_name_len = sizeof(#field_name_) - 1, \
138 .type = type_, \
139 .offset = offsetof(struct_, field_name_), \
140 }
141
166#define JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) \
167 { \
168 .field_name = (#field_name_), \
169 .align_shift = Z_ALIGN_SHIFT(struct_), \
170 .field_name_len = (sizeof(#field_name_) - 1), \
171 .type = JSON_TOK_OBJECT_START, \
172 .offset = offsetof(struct_, field_name_), \
173 { \
174 .object = { \
175 .sub_descr = sub_descr_, \
176 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
177 }, \
178 }, \
179 }
180
190#define Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_, union_) \
191 (const struct json_obj_descr[]) \
192 { \
193 { \
194 .align_shift = Z_ALIGN_SHIFT(struct_), \
195 .type = elem_type_, \
196 .offset = offsetof(struct_, len_field_), \
197 union_ \
198 } \
199 }
200
207#define Z_JSON_DESCR_ARRAY(elem_descr_, elem_descr_len_) \
208 { \
209 .array = { \
210 .element_descr = elem_descr_, \
211 .n_elements = elem_descr_len_, \
212 }, \
213 }
214
221#define Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_) \
222 { \
223 .object = { \
224 .sub_descr = elem_descr_, \
225 .sub_descr_len = elem_descr_len_, \
226 }, \
227 }
228
251#define JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, \
252 len_field_, elem_type_) \
253 { \
254 .field_name = (#field_name_), \
255 .align_shift = Z_ALIGN_SHIFT(struct_), \
256 .field_name_len = sizeof(#field_name_) - 1, \
257 .type = JSON_TOK_ARRAY_START, \
258 .offset = offsetof(struct_, field_name_), \
259 { \
260 .array = { \
261 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
262 elem_type_,), \
263 .n_elements = (max_len_), \
264 }, \
265 }, \
266 }
267
302#define JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, \
303 len_field_, elem_descr_, elem_descr_len_) \
304 { \
305 .field_name = (#field_name_), \
306 .align_shift = Z_ALIGN_SHIFT(struct_), \
307 .field_name_len = sizeof(#field_name_) - 1, \
308 .type = JSON_TOK_ARRAY_START, \
309 .offset = offsetof(struct_, field_name_), \
310 { \
311 .array = { \
312 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
313 JSON_TOK_OBJECT_START, \
314 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
315 .n_elements = (max_len_), \
316 }, \
317 }, \
318 }
319
363#define JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, \
364 elem_descr_, elem_descr_len_) \
365 { \
366 .field_name = (#field_name_), \
367 .align_shift = Z_ALIGN_SHIFT(struct_), \
368 .field_name_len = sizeof(#field_name_) - 1, \
369 .type = JSON_TOK_ARRAY_START, \
370 .offset = offsetof(struct_, field_name_), \
371 { \
372 .array = { \
373 .element_descr = Z_JSON_ELEMENT_DESCR( \
374 struct_, len_field_, JSON_TOK_ARRAY_START, \
375 Z_JSON_DESCR_ARRAY( \
376 elem_descr_, \
377 1 + ZERO_OR_COMPILE_ERROR(elem_descr_len_ == 1))), \
378 .n_elements = (max_len_), \
379 }, \
380 }, \
381 }
382
397#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
398 struct_field_name_, type_) \
399 { \
400 .field_name = (json_field_name_), \
401 .align_shift = Z_ALIGN_SHIFT(struct_), \
402 .field_name_len = sizeof(json_field_name_) - 1, \
403 .type = type_, \
404 .offset = offsetof(struct_, struct_field_name_), \
405 }
406
420#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
421 struct_field_name_, sub_descr_) \
422 { \
423 .field_name = (json_field_name_), \
424 .align_shift = Z_ALIGN_SHIFT(struct_), \
425 .field_name_len = (sizeof(json_field_name_) - 1), \
426 .type = JSON_TOK_OBJECT_START, \
427 .offset = offsetof(struct_, struct_field_name_), \
428 { \
429 .object = { \
430 .sub_descr = sub_descr_, \
431 .sub_descr_len = ARRAY_SIZE(sub_descr_), \
432 }, \
433 }, \
434 }
435
452#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
453 struct_field_name_, max_len_, len_field_, \
454 elem_type_) \
455 { \
456 .field_name = (json_field_name_), \
457 .align_shift = Z_ALIGN_SHIFT(struct_), \
458 .field_name_len = sizeof(json_field_name_) - 1, \
459 .type = JSON_TOK_ARRAY_START, \
460 .offset = offsetof(struct_, struct_field_name_), \
461 { \
462 .array = { \
463 .element_descr = \
464 Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_,), \
465 .n_elements = (max_len_), \
466 }, \
467 }, \
468 }
469
510#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, \
511 struct_field_name_, max_len_, \
512 len_field_, elem_descr_, \
513 elem_descr_len_) \
514 { \
515 .field_name = json_field_name_, \
516 .align_shift = Z_ALIGN_SHIFT(struct_), \
517 .field_name_len = sizeof(json_field_name_) - 1, \
518 .type = JSON_TOK_ARRAY_START, \
519 .offset = offsetof(struct_, struct_field_name_), \
520 { \
521 .array = { \
522 .element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
523 JSON_TOK_OBJECT_START, \
524 Z_JSON_DESCR_OBJ(elem_descr_, elem_descr_len_)), \
525 .n_elements = (max_len_), \
526 }, \
527 }, \
528 }
529
560int json_obj_parse(char *json, size_t len,
561 const struct json_obj_descr *descr, size_t descr_len,
562 void *val);
563
596int json_arr_parse(char *json, size_t len,
597 const struct json_obj_descr *descr, void *val);
598
611ssize_t json_escape(char *str, size_t *len, size_t buf_size);
612
621size_t json_calc_escaped_len(const char *str, size_t len);
622
634 size_t descr_len, const void *val);
635
649int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len,
650 const void *val, char *buffer, size_t buf_size);
651
664int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val,
665 char *buffer, size_t buf_size);
666
680int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
681 const void *val, json_append_bytes_t append_bytes,
682 void *data);
683
696int json_arr_encode(const struct json_obj_descr *descr, const void *val,
697 json_append_bytes_t append_bytes, void *data);
698
699#ifdef __cplusplus
700}
701#endif
702
706#endif /* ZEPHYR_INCLUDE_DATA_JSON_H_ */
json_tokens
Definition: json.h:32
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 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:106
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_ARRAY_END
Definition: json.h:46
@ JSON_TOK_COLON
Definition: json.h:48
@ JSON_TOK_LIST_END
Definition: json.h:45
@ JSON_TOK_COMMA
Definition: json.h:49
@ JSON_TOK_OBJECT_START
Definition: json.h:39
@ JSON_TOK_OBJECT_END
Definition: json.h:40
@ JSON_TOK_TRUE
Definition: json.h:51
@ JSON_TOK_FALSE
Definition: json.h:52
@ JSON_TOK_LIST_START
Definition: json.h:42
@ JSON_TOK_NONE
Definition: json.h:38
@ JSON_TOK_NULL
Definition: json.h:53
@ JSON_TOK_ARRAY_START
Definition: json.h:43
@ JSON_TOK_STRING
Definition: json.h:47
@ JSON_TOK_EOF
Definition: json.h:55
@ JSON_TOK_NUMBER
Definition: json.h:50
@ JSON_TOK_ERROR
Definition: json.h:54
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:58
const struct json_obj_descr * element_descr
Definition: json.h:88
const char * field_name
Definition: json.h:59
struct json_obj_descr::@101::@103 object
uint32_t align_shift
Definition: json.h:66
const struct json_obj_descr * sub_descr
Definition: json.h:84
uint32_t field_name_len
Definition: json.h:69
uint32_t offset
Definition: json.h:80
struct json_obj_descr::@101::@104 array
uint32_t type
Definition: json.h:77
size_t n_elements
Definition: json.h:89
size_t sub_descr_len
Definition: json.h:85
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
Macros to abstract toolchain specific capabilities.
Misc utilities.