Skip to content

Commit a5e295c

Browse files
cjwinklhoferkartben
authored andcommitted
json: improve parsing and serializing of integers
Add support for parsing and serializing of following integer types: 'int8_t', 'uint8_t', 'int16_t', 'uint16_t' and 'uint32_t'. The generic integer token JSON_TOK_INT and JSON_TOK_UINT, in combination with the field size (set by JSON_OBJ_DESCR_PRIM) allows to parse different integer types, for example: struct foo { int64_t i64; uint32_t u32; int16_t i16; uint8_t u8; }; struct json_obj_descr foo_descr[] = { JSON_OBJ_DESCR_PRIM(struct foo, i64, JSON_TOK_INT), JSON_OBJ_DESCR_PRIM(struct foo, u32, JSON_TOK_UINT), JSON_OBJ_DESCR_PRIM(struct foo, i16, JSON_TOK_INT), JSON_OBJ_DESCR_PRIM(struct foo, u8, JSON_TOK_UINT), }; These tokens also support parsing and serializing enums: enum unsigned_enum { UA=0, UB=1, UC=2 }; enum signed_enum { SA=-1, SB=0, SC=1 }; struct foo { enum unsigned_enum u; enum signed_enum s; }; struct json_obj_descr foo_descr[] = { JSON_OBJ_DESCR_PRIM(struct foo, u, JSON_TOK_UINT), JSON_OBJ_DESCR_PRIM(struct foo, s, JSON_TOK_INT), }; Signed-off-by: Christoph Winklhofer <[email protected]>
1 parent 63d33e6 commit a5e295c

File tree

4 files changed

+465
-13
lines changed

4 files changed

+465
-13
lines changed

include/zephyr/data/json.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ enum json_tokens {
4747
JSON_TOK_UINT64 = '6',
4848
JSON_TOK_FLOAT_FP = '7',
4949
JSON_TOK_DOUBLE_FP = '8',
50+
JSON_TOK_INT = 'i',
51+
JSON_TOK_UINT = 'u',
5052
JSON_TOK_TRUE = 't',
5153
JSON_TOK_FALSE = 'f',
5254
JSON_TOK_NULL = 'n',
@@ -251,6 +253,19 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
251253
.sub_descr_len = elem_descr_len_, \
252254
}, \
253255

256+
/**
257+
* @internal @brief Helper macro to declare a field descriptor
258+
*
259+
* @param struct_ Struct packing the values
260+
* @param field_name_ Field name in the struct
261+
*/
262+
#define Z_JSON_DESCR_FIELD(struct_, field_name_) \
263+
{ \
264+
.field = { \
265+
.size = SIZEOF_FIELD(struct_, field_name_), \
266+
}, \
267+
}
268+
254269
/**
255270
* @brief Helper macro to declare a descriptor for an array of primitives
256271
*
@@ -283,7 +298,8 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
283298
.offset = offsetof(struct_, field_name_), \
284299
.array = { \
285300
.element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
286-
elem_type_,), \
301+
elem_type_, \
302+
Z_JSON_DESCR_FIELD(struct_, field_name_[0])), \
287303
.n_elements = (max_len_), \
288304
}, \
289305
}
@@ -514,8 +530,9 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
514530
.type = JSON_TOK_ARRAY_START, \
515531
.offset = offsetof(struct_, struct_field_name_), \
516532
.array = { \
517-
.element_descr = \
518-
Z_JSON_ELEMENT_DESCR(struct_, len_field_, elem_type_,), \
533+
.element_descr = Z_JSON_ELEMENT_DESCR(struct_, len_field_, \
534+
elem_type_, \
535+
Z_JSON_DESCR_FIELD(struct_, struct_field_name_[0])), \
519536
.n_elements = (max_len_), \
520537
}, \
521538
}

0 commit comments

Comments
 (0)