yyjson
yyjson copied to clipboard
Public API to allow building values without allocation (no mut_doc APIs)
Is your feature request related to a problem? Please describe.
It would be nice if we could have public APIs to initialize yyjson_mut_val
stack objects that we could then pass along to yyjson_mut_obj_add
/ yyjson_mut_arr_append
. That's because most of the time, the responses are simple and static enough for this to work.
Describe the solution you'd like Say for instance I want to build:
{code: 5, params: [1, "error-msg"]};
it would be nice if I could do it with no allocations
yyjson_mut_val root, code_key, code_val, params_key, params_arr, one, msg;
/*
* start of missing API
*/
yyjson_mut_set_obj(&root);
yyjson_mut_set_str(&code_key, "code_key");
yyjson_mut_set_int(&code_val, 5);
yyjson_mut_set_str(¶ms_key, "params");
yyjson_mut_set_arr(¶ms_arr);
yyjson_mut_set_int(&one, 1);
yyjson_mut_set_str(&msg, "error-msg");
/*
* end of missing APIs
*/
// we can already do those
yyjson_mut_obj_add(&root, &code_key, &code_val);
yyjson_mut_obj_add(&root, ¶ms_key, ¶ms);
yyjson_mut_arr_append(¶ms, &one);
yyjson_mut_arr_append(¶ms, &msg);
char buf[100];
// ....
yyjson_mut_val_write_opts(&root, &alc ....);
Describe alternatives you've considered I guess tags could be set manually on the struct instances, but tags are advertised as being part of the private API.
I added some functions as you suggested: https://github.com/ibireme/yyjson/commit/ee9e6d5d3bde4a26b78e9cfd596dd28d231266fa, and here is an example:
// build JSON on stack
yyjson_mut_val root, code_key, code, msg_key, msg, arr_key, arr, n1, n2;
yyjson_mut_set_obj(&root);
yyjson_mut_set_str(&code_key, "code");
yyjson_mut_set_int(&code, 200);
yyjson_mut_set_str(&msg_key, "msg");
yyjson_mut_set_str(&msg, "success");
yyjson_mut_set_str(&arr_key, "arr");
yyjson_mut_set_arr(&arr);
yyjson_mut_set_bool(&n1, true);
yyjson_mut_set_bool(&n2, false);
yyjson_mut_obj_add(&root, &code_key, &code);
yyjson_mut_obj_add(&root, &msg_key, &msg);
yyjson_mut_obj_add(&root, &arr_key, &arr);
yyjson_mut_arr_append(&arr, &n1);
yyjson_mut_arr_append(&arr, &n2);
char buf[256];
yyjson_alc alc;
yyjson_alc_pool_init(&alc, buf, sizeof(buf));
char *json = yyjson_mut_val_write_opts(&root, 0, &alc, NULL, NULL);
// {"code":200,"msg":"success","arr":[true,false]}
Well, this is outstanding. Thanks a bunch. Will give it a spin and report back.
back that is a good idea, I am also looking forward to it.
This is working as advertised, great work @ibireme. The writing part of https://github.com/ibireme/yyjson/issues/33 can be marked as done.