ibireme/yyjson

Create or replace value at JSON Pointer

Closed this issue · 4 comments

Thank you for this excellent library!

I am implementing functionality to create or replace a value specified using a JSON Pointer in an existing yyjson_mut_doc and would appreciate knowing if I've missed discovering some functionality that addresses this.

Initially I thought the merge patch functionality was a good fit. But my understanding is using merge patch for this would require reallocating the document each call, which is not ideal in terms of efficiency.

Next I considered yyjson_mut_obj_replace() (and arr version), and other approaches but these all require a fair bit of code to discover the path that exists, create path that is needed, and then replace or create the final value.

What I have wished for is an interface like:

yyjson_mut_doc_set_pointer(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *val);

w/ convenience wrappers

yyjson_mut_doc_set_bool_pointer(yyjson_mut_doc *doc, const char *ptr, size_t len, bool num);
yyjson_mut_doc_set_uint_pointer(yyjson_mut_doc *doc, const char *ptr, size_t len, uint64_t num);
yyjson_mut_doc_set_sint_pointer(yyjson_mut_doc *doc, const char *ptr, size_t len, int64_t num);
yyjson_mut_doc_set_real_pointer(yyjson_mut_doc *doc, const char *ptr, size_t len, double num);
yyjson_mut_doc_set_str_pointer(yyjson_mut_doc *doc, const char *ptr, size_t len, const char *str);

which create the path context as needed and then create or replace the final value.
Edit: and remove/free any structures that are removed.

Any hints would be appreciated. It is entirely possible I've missed some functionality that would accomplish this.

This functionality would be satisfy the functionality described here I believe: #84

and likely address many of the setting usage cases of JSON Patch: #12

Implementing the full JSON Patch requires a lot of work...
I’ll try to implement #84 soon, which looks easier.

The new functions have been added to the master branch.

Excellent, thank you!

Just a note to say that I'm now using these new functions and they are all working great. Thank you!