support JSON Merge Patch (RFC 7396) diff creation
The JSON merge patch document format describes the set of modifications to a resource's content, that more closely mimics the syntax of the resource being modified.
However, and in contrast to JSON Patch (RFC 6902), a JSON Merge Patch cannot express certain modifications, e.g., changing an array element at a specific index, or setting a specific object value to null. The null value in a JSON Merge Patch is used to remove the key from the object.
The diff algorithm is not part of the RFC 7396, but it was tested against all examples provided, plus additional cases on how null values are handled.
JSON Merge Patch PR: https://github.com/nlohmann/json/pull/876 PR discussing the diff: https://github.com/nlohmann/json/issues/2018
If the content is approved, please let me know if/what documentation needs to be updated.
[Describe your pull request here. Please read the text below the line and make sure you follow the checklist.]
- [x] The changes are described in detail, both the what and why.
- [x] If applicable, an existing issue is referenced.
- [ ] The Code coverage remained at 100%. A test case for every new line of code.
- [ ] If applicable, the documentation is updated.
- [ ] The source code is amalgamated by running
make amalgamate.
Read the Contribution Guidelines for detailed information.
🔴 Amalgamation check failed! 🔴
The source code has not been amalgamated. @satelliteprogrammer Please read and follow the Contribution Guidelines.
coverage: 99.194% (+0.003%) from 99.191% when pulling d32edb3d51156579ba8a7e635bb59ab64c3b7c4d on satelliteprogrammer:develop into 29913ca760c663e1b1df3512f125c247681905e8 on nlohmann:develop.
I am not sure if this feature would be widely used, so I'm opening a discussion.
We had a (somewhat niche) need for this at work, that's why I've contributed here.
It's essentially a combination of 2 things:
- we log all our data structures in JSON, this makes it easier to parse the logs and pretty-format large data structures;
- we have a container that only notifies listeners on data changes.
For very large data structures, reading through a log line to find the one variable that did change is a pain. Not only that, but on some interfaces only a small amount of member variables on the entire structure are actually changing. Given that we are already serializing in JSON, on those containers that track differences, we thought why not log only those differences using one of the available JSON patch methods. So here we are.
This pull request has been marked as stale because it has had no activity for 30 days. While we won’t close it automatically, we encourage you to update or comment if it is still relevant. Keeping pull requests active and up-to-date helps us review and merge changes more efficiently. Thank you for your contributions!