Closed andrei-datcu closed 2 years ago
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.
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 toyyjson_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:
it would be nice if I could do it with no allocations
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.