Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions lib/json/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
Expand Down
20 changes: 7 additions & 13 deletions tests/lib/json/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down