Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public API to allow building values without allocation (no mut_doc APIs) #87

Closed
andrei-datcu opened this issue Aug 6, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@andrei-datcu
Copy link

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(&params_key, "params");
 yyjson_mut_set_arr(&params_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, &params_key, &params);
 yyjson_mut_arr_append(&params, &one);
 yyjson_mut_arr_append(&params, &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.

@andrei-datcu andrei-datcu changed the title mut_doc less public API Public API to allow building values without allocation (no mut_doc APIs) Aug 6, 2022
@ibireme ibireme added the enhancement New feature or request label Aug 7, 2022
@ibireme
Copy link
Owner

ibireme commented Aug 7, 2022

I added some functions as you suggested: ee9e6d5, 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]}

@andrei-datcu
Copy link
Author

Well, this is outstanding. Thanks a bunch. Will give it a spin and report back.

@zestzone007
Copy link

back
that is a good idea, I am also looking forward to it.

@andrei-datcu
Copy link
Author

This is working as advertised, great work @ibireme.
The writing part of #33 can be marked as done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants