* docs: new design for the documentation site Apply the brand to the Material for MkDocs site: Inter and JetBrains Mono served by the site (upright and real italic), brand colours, blue header band with the syntax pattern, wordmark in the header, new home page (hero, cards, animated feature tour), 404 page, polish for tabs, search, footer, focus rings, tables and code blocks, and theme features (code copy, previous/next footer, TOC follow, search suggestions). Draw the number railroad diagram, the parser callback events and the iterator illustrations as inline SVGs that follow the light and dark scheme. The generator scripts are in docs/mkdocs/scripts/figures; the feature slideshow (json-light.svg, json-dark.svg, replacing json.gif on the home page) is built by docs/mkdocs/scripts/slideshow. Decision nodes in five flowcharts become hexagons, which need far less room. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: add figures, table captions and a macro chooser - Figures with numbered captions: SAX stepper, json_view layout and edit stepper, exception map, JSON pointer and patch walkers, QA pipeline, scaling and binary size charts, roadmap lanes, release timeline, integration decision tree and namespace builder - Table captions (nj-table-caption) on 9 pages - Compile times: modules and PCH charts, filesystem section, re-measured Figure 1, links between features/modules.md and compile_times.md - Interactive macro chooser for features/arbitrary_types.md - Link check: exclude URLs that answer the plugin with 400/401/419 or are dead (customers.md) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: macro chooser generates adl_serializer for non-default-constructible types Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: cross-link concept pages and API pages in See also sections Add See also links in both directions between the concept pages (features, home, integration) and the API pages, write API links in code font, give every entry a comment after a dash, and show the callback event figure on the parser callbacks page. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: address review comments on the new design - Home: add a "Latest release" button - Sponsors: update the list and group it by tier - Features: list all feature pages on the index page - Releases: add highlights per release, link the 4.0 roadmap, add the first commit to the timeline - Exceptions: prefix all example messages with the exception name - API: no code font for format names in the See also links of the to_/from_ pages - Merge Patch: show the document before, the patch, and the result - Replace "--" with typographic dashes, mark external links with an icon, center Mermaid diagrams Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
5.5 KiB
JSON Patch and Diff
Patches
JSON Patch (RFC 6902) defines a JSON document structure for expressing a sequence
of operations to apply to a JSON document. Operations address locations in the document using
JSON Pointer paths. With the patch function, a JSON Patch is applied
to the current JSON value by executing all operations from the patch, yielding the patched document as a new value.
The following steps through one operation of each kind. The first three operations are those of the example below; the other three continue on its result.
| Operation | Before | After |
|---|---|---|
{"op": "replace", "path": "/baz", "value": "boo"} |
{"baz": "qux", "foo": "bar"} |
{"baz": "boo", "foo": "bar"} |
{"op": "add", "path": "/hello", "value": ["world"]} |
{"baz": "boo", "foo": "bar"} |
{"baz": "boo", "foo": "bar", "hello": ["world"]} |
{"op": "remove", "path": "/foo"} |
{"baz": "boo", "foo": "bar", "hello": ["world"]} |
{"baz": "boo", "hello": ["world"]} |
{"op": "move", "from": "/baz", "path": "/hello/1"} |
{"baz": "boo", "hello": ["world"]} |
{"hello": ["world", "boo"]} |
{"op": "copy", "from": "/hello/0", "path": "/greeting"} |
{"hello": ["world", "boo"]} |
{"greeting": "world", "hello": ["world", "boo"]} |
{"op": "test", "path": "/greeting", "value": "world"} |
{"greeting": "world", "hello": ["world", "boo"]} |
unchanged (a failed test throws) |
Stepper through the six operations: *Next* applies the next operation, *Back* undoes it, and *Reset* returns to the original document. The current operation is highlighted in the patch list, and the lines it changes are marked in the document.
!!! tip "Applying a patch without copying"
[`patch`](../api/basic_json/patch.md) leaves the original value unchanged and returns the patched result as a copy.
If the document is large and the original value is no longer needed,
[`patch_inplace`](../api/basic_json/patch_inplace.md) applies the same operations in place instead.
!!! note "Duplicate member names"
Duplicate member names inside an operation object are resolved by the parser before the patch is applied (the last
one is kept, see [`object_t`](../api/basic_json/object_t.md#behavior)), so
`#!json {"op": "add", "op": "remove", "path": "/a"}` is a `remove` operation.
??? example "Example: apply a JSON Patch"
The following code shows how a JSON patch is applied to a value.
```cpp
--8<-- "examples/patch.cpp"
```
Output:
```json
--8<-- "examples/patch.output"
```
Diff
The library can also calculate a JSON patch (i.e., a diff) given two JSON values with the
diff function.
flowchart LR
S["source"] -->|"diff(source, target)"| P["patch"]
S -->|"source.patch(patch)"| T["target"]
P -.->|"applied to source, yields"| T
How `diff` and `patch` relate: `diff(source, target)` yields a patch, and applying that patch to the source yields the target.
!!! success "Invariant"
For two JSON values *source* and *target*, the following code yields true:
```cpp
source.patch(diff(source, target)) == target;
```
The equality holds up to [`operator==`](../api/basic_json/operator_eq.md), with these exceptions:
- `NaN` and [discarded](../api/basic_json/is_discarded.md) values are not equal to themselves, so the invariant
cannot hold for them. `diff` emits a `replace` operation for such a value, and a `NaN` in a patch is serialized
as `#!json null`.
- Numbers that compare equal produce no operation even if their types differ: `diff(1, 1.0)`, `diff(1u, 1)`, and
`diff(0.0, -0.0)` return `#!json []`, so the patched value keeps the number type of *source*.
- A [binary value](binary_values.md) in a patch is serialized as an object
(`#!json {"bytes":[...],"subtype":null}`), so a patch that was dumped and parsed again no longer holds a binary
value.
??? example "Example: create a JSON Patch from the difference of two values"
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
- JSON Pointer - the addressing scheme used for patch paths
- JSON Merge Patch - a simpler, less expressive alternative patch format
patch- apply a JSON Patch, returning the result as a copypatch_inplace- apply a JSON Patch without copyingdiff- compute a JSON Patch from two valuesjson_pointer- type to represent JSON Pointersmerge_diff- compute a JSON Merge Patch from two values- Modifying values - simple in-place modifications