|
◆ diff()
Creates a JSON Patch so that value source can be changed into the value target by calling patch function.
- Invariant
- For two JSON values source and target, the following code yields always
true : source.patch( diff(source, target)) == target;
- Note
- Currently, only
remove , add , and replace operations are generated.
- Parameters
-
[in] | source | JSON value to compare from |
[in] | target | JSON value to compare against |
[in] | path | helper value to create JSON pointers |
- Returns
- a JSON patch to convert the source to target
- Complexity
- Linear in the lengths of source and target.
- Example
- The following code shows how a JSON patch is created as a diff for two JSON values.
3 #include <nlohmann/json.hpp>
31 json patched_source = source.patch( patch);
34 std::cout << std::setw(4) << patch << "\n\n"
35 << std::setw(4) << patched_source << std::endl;
Output (play with this example online): [
{
"op": "replace",
"path": "/baz",
"value": "boo"
},
{
"op": "remove",
"path": "/foo"
},
{
"op": "add",
"path": "/hello",
"value": [
"world"
]
}
]
{
"baz": "boo",
"hello": [
"world"
]
}
The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/diff.cpp -o diff
- See also
- patch – apply a JSON patch
-
merge_patch – apply a JSON Merge Patch
-
RFC 6902 (JSON Patch)
- Since
- version 2.0.0
Definition at line 22416 of file json.hpp.
|