From 458bc5e414d8300dcc3c6976209501f529d86f47 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Wed, 3 May 2017 10:20:50 -0400 Subject: [PATCH 1/2] lib: json: add JSON_OBJ_DESCR_PRIM There are already helper macros for declaring descriptor fields of object and array type. Add one for primitive types as well. The fact that the JSON test code defines one proves that it's useful, so there should be one provided for other users. Signed-off-by: Marti Bolivar --- lib/json/json.h | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) 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 From 578a6ddd841bfb503574d2f902aa1aefe0248352 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Wed, 3 May 2017 10:26:56 -0400 Subject: [PATCH 2/2] tests: json: use JSON_OBJ_DESCR_PRIM Signed-off-by: Marti Bolivar --- tests/lib/json/src/main.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) 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) {