• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# JSON Patch and Diff
2
3## Patches
4
5JSON Patch ([RFC 6902](https://tools.ietf.org/html/rfc6902)) defines a JSON document structure for expressing a sequence of operations to apply to a JSON) document. With the `patch` function, a JSON Patch is applied to the current JSON value by executing all operations from the patch.
6
7??? example
8
9    The following code shows how a JSON patch is applied to a value.
10
11    ```cpp
12    --8<-- "examples/patch.cpp"
13    ```
14
15    Output:
16
17    ```json
18    --8<-- "examples/patch.output"
19    ```
20
21## Diff
22
23The library can also calculate a JSON patch (i.e., a **diff**) given two JSON values.
24
25!!! success "Invariant"
26
27    For two JSON values *source* and *target*, the following code yields always true:
28
29    ```cüü
30    source.patch(diff(source, target)) == target;
31    ```
32
33??? example
34
35    The following code shows how a JSON patch is created as a diff for two JSON values.
36
37    ```cpp
38    --8<-- "examples/diff.cpp"
39    ```
40
41    Output:
42
43    ```json
44    --8<-- "examples/diff.output"
45    ```
46