Closed kay54068 closed 1 year ago
yyjson does not support automatic struct mapping, you need to handle it manually, for example:
// your struct definition
struct user_t {
uint64_t uid;
const char *name;
bool enabled;
};
// your array
struct user_t *users = ...;
size_t user_count = ...;
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
yyjson_mut_val *arr = yyjson_mut_arr(doc);
for (size_t i = 0; i < user_count; i++) {
struct user_t *user = &users[i];
yyjson_mut_val *obj = yyjson_mut_obj(doc);
yyjson_mut_obj_add_uint(doc, obj, "uid", user->uid);
yyjson_mut_obj_add_str(doc, obj, "name", user->name);
yyjson_mut_obj_add_bool(doc, obj, "enabled", user->enabled);
yyjson_mut_arr_append(arr, obj);
}
yyjson_mut_doc_set_root(doc, arr);
// create JSON string
char *json = yyjson_mut_write(doc, 0, NULL);
yyjson_mut_doc_free(doc);
free(json);
My main use for this library would be to dump arrays of C structs. Can you support the yyjson c struct mapping example ?