| # <small>nlohmann::basic_json::</small>diff | |
| ```cpp | |
| static basic_json diff(const basic_json& source, | |
| const basic_json& target); | |
| ``` | |
| Creates a [JSON Patch](http://jsonpatch.com) so that value `source` can be changed into the value `target` by calling | |
| [`patch`](patch.md) function. | |
| For two JSON values `source` and `target`, the following code always yields `#!cpp true`: | |
| ```cpp | |
| 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 | |
| #include <iostream> | |
| #include <iomanip> | |
| #include <nlohmann/json.hpp> | |
| using json = nlohmann::json; | |
| using namespace nlohmann::literals; | |
| int main() | |
| { | |
| // the source document | |
| json source = R"( | |
| { | |
| "baz": "qux", | |
| "foo": "bar" | |
| } | |
| )"_json; | |
| // the target document | |
| json target = R"( | |
| { | |
| "baz": "boo", | |
| "hello": [ | |
| "world" | |
| ] | |
| } | |
| )"_json; | |
| // create the patch | |
| json patch = json::diff(source, target); | |
| // roundtrip | |
| json patched_source = source.patch(patch); | |
| // output patch and roundtrip result | |
| std::cout << std::setw(4) << patch << "\n\n" | |
| << std::setw(4) << patched_source << std::endl; | |
| } | |
| ``` | |
| Output: | |
| ```json | |
| [ | |
| { | |
| "op": "replace", | |
| "path": "/baz", | |
| "value": "boo" | |
| }, | |
| { | |
| "op": "remove", | |
| "path": "/foo" | |
| }, | |
| { | |
| "op": "add", | |
| "path": "/hello", | |
| "value": [ | |
| "world" | |
| ] | |
| } | |
| ] | |
| { | |
| "baz": "boo", | |
| "hello": [ | |
| "world" | |
| ] | |
| } | |
| ``` | |
| ## See also | |
| - [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902) | |
| - [patch](patch.md) applies a JSON Patch | |
| - [patch_inplace](patch_inplace.md) applies a JSON Patch in place | |
| - [merge_patch](merge_patch.md) applies a JSON Merge Patch | |
| - [merge_diff](merge_diff.md) creates a diff as a JSON Merge Patch | |
| ## Version history | |
| - Added in version 2.0.0. |