Skip to content

Commit f083787

Browse files
Marti BolivarAnas Nashif
authored andcommitted
lib: json: add JSON_OBJ_DESCR_*_NAMED variants
The set of valid JSON field names is larger than the set of C identifiers. This can result in structure field names which pack decoded JSON values which necessarily differ from the field names in the JSON. Support this by adding _NAMED variants to each of the JSON_OBJ_DESCR_* helper macros. For example, JSON_OBJ_DESCR_PRIM_NAMED allows users to declare a descriptor field for a primitive type, whose structure field name is different from the JSON field name. Signed-off-by: Marti Bolivar <[email protected]>
1 parent e2b8a7b commit f083787

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

lib/json/json.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,95 @@ typedef int (*json_append_bytes_t)(const u8_t *bytes, size_t len,
172172
.n_elements = (max_len_), \
173173
}
174174

175+
/**
176+
* @brief Variant of JSON_OBJ_DESCR_PRIM that can be used when the
177+
* structure and JSON field names differ.
178+
*
179+
* This is useful when the JSON field is not a valid C identifier.
180+
*
181+
* @param struct_ Struct packing the values.
182+
*
183+
* @param json_field_name_ String, field name in JSON strings
184+
*
185+
* @param struct_field_name_ Field name in the struct
186+
*
187+
* @param type_ Token type for JSON value corresponding to a primitive
188+
* type.
189+
*
190+
* @see JSON_OBJ_DESCR_PRIM
191+
*/
192+
#define JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, \
193+
struct_field_name_, type_) \
194+
{ \
195+
.field_name = (json_field_name_), \
196+
.field_name_len = sizeof(json_field_name_) - 1, \
197+
.offset = offsetof(struct_, struct_field_name_), \
198+
.type = type_, \
199+
}
200+
201+
/**
202+
* @brief Variant of JSON_OBJ_DESCR_OBJECT that can be used when the
203+
* structure and JSON field names differ.
204+
*
205+
* This is useful when the JSON field is not a valid C identifier.
206+
*
207+
* @param struct_ Struct packing the values
208+
*
209+
* @param json_field_name_ String, field name in JSON strings
210+
*
211+
* @param struct_field_name_ Field name in the struct
212+
*
213+
* @param sub_descr_ Array of json_obj_descr describing the subobject
214+
*
215+
* @see JSON_OBJ_DESCR_OBJECT
216+
*/
217+
#define JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, \
218+
struct_field_name_, sub_descr_) \
219+
{ \
220+
.field_name = (json_field_name_), \
221+
.field_name_len = (sizeof(json_field_name_) - 1), \
222+
.offset = offsetof(struct_, struct_field_name_), \
223+
.type = JSON_TOK_OBJECT_START, \
224+
.sub_descr = sub_descr_, \
225+
.sub_descr_len = ARRAY_SIZE(sub_descr_) \
226+
}
227+
228+
/**
229+
* @brief Variant of JSON_OBJ_DESCR_ARRAY that can be used when the
230+
* structure and JSON field names differ.
231+
*
232+
* This is useful when the JSON field is not a valid C identifier.
233+
*
234+
* @param struct_ Struct packing the values
235+
*
236+
* @param json_field_name_ String, field name in JSON strings
237+
*
238+
* @param struct_field_name_ Field name in the struct
239+
*
240+
* @param max_len_ Maximum number of elements in array
241+
*
242+
* @param len_field_ Field name in the struct for the number of elements
243+
* in the array
244+
*
245+
* @param elem_type_ Element type
246+
*
247+
* @see JSON_OBJ_DESCR_ARRAY
248+
*/
249+
#define JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_,\
250+
struct_field_name_, max_len_, len_field_, \
251+
elem_type_) \
252+
{ \
253+
.field_name = (json_field_name_), \
254+
.field_name_len = sizeof(json_field_name_) - 1, \
255+
.offset = offsetof(struct_, struct_field_name_), \
256+
.type = JSON_TOK_LIST_START, \
257+
.element_descr = &(struct json_obj_descr) { \
258+
.type = elem_type_, \
259+
.offset = offsetof(struct_, len_field_), \
260+
}, \
261+
.n_elements = (max_len_), \
262+
}
263+
175264
/**
176265
* @brief Parses the JSON-encoded object pointer to by @param json, with
177266
* size @param len, according to the descriptor pointed to by @param descr.

0 commit comments

Comments
 (0)