* 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. Signed-off-by: Luís Murta <luis@murta.dev> * improve algorithm efficiency Signed-off-by: Luís Murta <luis@murta.dev> * fix inconsistent nulls and empty arrays Also add CHECKs for the patch, documentation and run `make amalgamate`. Signed-off-by: Luís Murta <luis@murta.dev> * documentation cleanups Signed-off-by: Luís Murta <luis@murta.dev> * add an iterative fallback to merge_diff Bound the recursion depth like diff() and merge_patch(), and test values nested past the bound. Signed-off-by: Luís Murta <luis@murta.dev> * address the remaining review comments Signed-off-by: Luís Murta <luis@murta.dev> * move multi-line raw strings out of CHECK macros GCC 4.8/5, ICPC and NVHPC mis-tokenize multi-line raw string literals inside macro arguments, breaking the build or the parsed JSON. Signed-off-by: Luís Murta <luis@murta.dev> --------- Signed-off-by: Luís Murta <luis@murta.dev>
1.4 KiB
1.4 KiB
nlohmann::basic_json::diff
static basic_json diff(const basic_json& source,
const basic_json& target);
Creates a JSON Patch so that value source can be changed into the value target by calling
patch function.
For two JSON values source and target, the following code always yields #!cpp true:
source.patch(diff(source, target)) == target;
Parameters
source(in)- JSON value to compare from
target(in)- JSON value to compare against
Return value
a JSON patch to convert the source to target
Exception safety
Strong guarantee: source and target are never modified.
Complexity
Linear in the lengths of source and target.
Notes
Currently, only remove, add, and replace operations are generated.
Examples
??? example
The following code shows how a JSON patch is created as a diff for two JSON values.
```cpp
--8<-- "examples/diff.cpp"
```
Output:
```json
--8<-- "examples/diff.output"
```
See also
- RFC 6902 (JSON Patch)
- patch applies a JSON Patch
- patch_inplace applies a JSON Patch in place
- merge_patch applies a JSON Merge Patch
- merge_diff creates a diff as a JSON Merge Patch
Version history
- Added in version 2.0.0.