json_patch: add first implementation only with patch application
Initially I wanted to also do a function that generates the JSON patch from two JSON documents, but even just applying the JSON patch was a bit of work, especially when needing to satisfy all the test-cases.
This change defines all the operation in the RFC6902. The addition isn't too big (for the json_patch_apply() function), as part of the heavy lifting is also done by JSON pointer logic.
All the ops were tested with the test-cases defined at: https://github.com/json-patch/json-patch-tests
RFC6902: https://tools.ietf.org/html/rfc6902
However, in order to properly support JSON patch:
- json_object_array_insert_idx() is needed in the JSON object API
- this changeset also changes [slightly] the behavior of
json_object_set{f}(); I figured, it may be still doable; I can revert that behavior change [if needed]; the behavior ofjson_object_set{f}()wasn't clarified initially; - the
json_pointer_set_with_array_cb()mechanism should be useful to support various JSON array behavior types
Signed-off-by: Alexandru Ardelean [email protected]
I can split this up in smaller PRs to reduce the amount of review effort. Some changes in this set can be moved independently, if that helps.
Coverage increased (+0.7%) to 88.064% when pulling 5ff17b986defe4fc0f604eb141a876fdf67e46be on commodo:json_patch into cd7109f767a6d9f20caa9d10fbf4eab9c5262e7a on json-c:master.
it will take me a while to re-spin everything; i came back from a short vacation; would it be ok to do a PR with just the first 3 patches? i'll probably do a PR with just those 3 in the meantime;
Changelog v1 -> v2:
- only first 3 commits changed [for now] ; also moved to PR https://github.com/json-c/json-c/pull/704 in case that simplifies things; i'm also fine to close that
- https://github.com/json-c/json-c/compare/a0c7225cd907a1cf634a30584c3bb2a1531170fa..5ff17b986defe4fc0f604eb141a876fdf67e46be
- changed
idx > arr->length->idx >= arr->length - removed
array_list_insert_idxfromJSONC_PRIVATE - removed
The reference count of a replaced object will be decremented.docstring fromjson_object_array_insert_idx(); the patch may show up like it'sjson_object_array_put_idx() - updated test-case for json_object_array_insert_idx() to insert last element as well
Fyi, I adjusted some of these changes, and I'm adding additional fixes over at https://github.com/hawicz/json-c/tree/json_patch
Fyi, I adjusted some of these changes, and I'm adding additional fixes over at https://github.com/hawicz/json-c/tree/json_patch
Awesome :) Thanks
Apologies I did not get a chance to do any updates on this. Hopefully, all the work (I did) helps. I'm having a bit of a tough time remembering implementation details on this; but I'll try to take a look.
Hang tight for a bit, I've got a number of local changes that I'll be pushing to my fork which address my earlier review comments. (it might take a day or two)
@commodo, I believe this is in a pretty good shape to merge now. Would you care to give it a once over before I do so?
Wooo Thanks :)
I know this sounds a bit ¯\_(ツ)_/¯ , but with this, I'm now finished with json-c (for a while).
Contextually:
- json-c is used by OpenWrt (as a core lib)
- I was working for a company a few years ago, and we were using OpenWrt in a project
- We were experimenting with solutions to address issues of large JSON files (50-100 MB JSON files).
- Someone might ask whether JSON is the best format for large data-sets
- For us, that was the situation between configuration coming from the cloud and having many network devices getting their configs (with routing info and stuff)
- JSON patch was a solution for this, but we needed something fast
- I don't know what they ended up doing (I left at some point)
- I only managed to implement JSON pointer (while still working on that project)
- I never thought I'd finish JSON patch; I started it sometime after I left (since it seemed fun to do)
Thanks again :) Alex