diff --git a/lib/json/json.h b/lib/json/json.h index 30edd5b618d0d..09026237395e9 100644 --- a/lib/json/json.h +++ b/lib/json/json.h @@ -67,6 +67,37 @@ struct json_obj_descr { typedef int (*json_append_bytes_t)(const u8_t *bytes, size_t len, void *data); + +/** + * @brief Helper macro to declare a descriptor for supported primitive + * values. + * + * @param struct_ Struct packing the values + * + * @param field_name_ Field name in the struct + * + * @param type_ Token type for JSON value corresponding to a primitive + * type. Must be one of: JSON_TOK_STRING for strings, JSON_TOK_NUMBER + * for numbers, JSON_TOK_TRUE (or JSON_TOK_FALSE) for booleans. + * + * Here's an example of use: + * + * struct foo { + * int some_int; + * }; + * + * struct json_obj_descr foo[] = { + * JSON_OBJ_DESCR_PRIM(struct foo, some_int, JSON_TOK_NUMBER), + * }; + */ +#define JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) \ + { \ + .field_name = (#field_name_), \ + .field_name_len = sizeof(#field_name_) - 1, \ + .offset = offsetof(struct_, field_name_), \ + .type = type_, \ + } + /** * @brief Helper macro to declare a descriptor for an object value * @@ -148,14 +179,8 @@ typedef int (*json_append_bytes_t)(const u8_t *bytes, size_t len, * * struct s { int foo; char *bar; } * struct json_obj_descr descr[] = { - * { .field_name = "foo", - * .field_name_len = 3, - * .offset = offsetof(struct s, foo), - * .type = JSON_TOK_NUMBER }, - * { .field_name = "bar", - * .field_name_len = 3, - * .offset = offsetof(struct s, bar), - * .type = JSON_TOK_STRING } + * JSON_OBJ_DESCR_PRIM(struct s, foo, JSON_TOK_NUMBER), + * JSON_OBJ_DESCR_PRIM(struct s, bar, JSON_TOK_STRING), * }; * * Since this parser is designed for machine-to-machine communications, some diff --git a/tests/lib/json/src/main.c b/tests/lib/json/src/main.c index 6100c622f074d..945d8ace52bb7 100644 --- a/tests/lib/json/src/main.c +++ b/tests/lib/json/src/main.c @@ -24,28 +24,22 @@ struct test_struct { size_t some_array_len; }; -#define FIELD(struct_, member_, type_) { \ - .field_name = #member_, \ - .field_name_len = sizeof(#member_) - 1, \ - .offset = offsetof(struct_, member_), \ - .type = type_ \ -} static const struct json_obj_descr nested_descr[] = { - FIELD(struct test_nested, nested_int, JSON_TOK_NUMBER), - FIELD(struct test_nested, nested_bool, JSON_TOK_TRUE), - FIELD(struct test_nested, nested_string, JSON_TOK_STRING), + JSON_OBJ_DESCR_PRIM(struct test_nested, nested_int, JSON_TOK_NUMBER), + JSON_OBJ_DESCR_PRIM(struct test_nested, nested_bool, JSON_TOK_TRUE), + JSON_OBJ_DESCR_PRIM(struct test_nested, nested_string, + JSON_TOK_STRING), }; static const struct json_obj_descr test_descr[] = { - FIELD(struct test_struct, some_string, JSON_TOK_STRING), - FIELD(struct test_struct, some_int, JSON_TOK_NUMBER), - FIELD(struct test_struct, some_bool, JSON_TOK_TRUE), + JSON_OBJ_DESCR_PRIM(struct test_struct, some_string, JSON_TOK_STRING), + JSON_OBJ_DESCR_PRIM(struct test_struct, some_int, JSON_TOK_NUMBER), + JSON_OBJ_DESCR_PRIM(struct test_struct, some_bool, JSON_TOK_TRUE), JSON_OBJ_DESCR_OBJECT(struct test_struct, some_nested_struct, nested_descr), JSON_OBJ_DESCR_ARRAY(struct test_struct, some_array, 16, some_array_len, JSON_TOK_NUMBER), }; -#undef FIELD static void test_json_encoding(void) {