* 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>
9.5 KiB
JSON Pointer
Introduction
The library supports JSON Pointer (RFC 6901) as an alternative means to address structured values. A JSON Pointer is a string that identifies a specific value within a JSON document.
Consider the following JSON document
{
"array": ["A", "B", "C"],
"nested": {
"one": 1,
"two": 2,
"three": [true, false]
}
}
Select a pointer (or a node of the tree) to walk it through this document; each segment of the pointer goes one level deeper. The table below lists the same pointers.
Highlighted: the path of the pointer /nested/three/1, which identifies false.
- root object
- "array" array
- 0: "A"
- 1: "B"
- 2: "C"
- "nested" object
- "one": 1
- "two": 2
- "three" array
- 0: true
- 1: false
- "array" array
The highlighted nodes are the path from the root to the selected level, and the line below the tree shows the value the pointer resolves to. Select a segment in the pointer bar to stop at a shallower level, or a node of the tree to select its pointer.
Then every value inside the JSON document can be identified as follows:
| JSON Pointer | JSON value |
|---|---|
| `` | #!json {"array":["A","B","C"],"nested":{"one":1,"two":2,"three":[true,false]}} |
/array |
#!json ["A","B","C"] |
/array/0 |
#!json A |
/array/1 |
#!json B |
/array/2 |
#!json C |
/nested |
#!json {"one":1,"two":2,"three":[true,false]} |
/nested/one |
#!json 1 |
/nested/two |
#!json 2 |
/nested/three |
#!json [true,false] |
/nested/three/0 |
#!json true |
/nested/three/1 |
#!json false |
Note / does not identify the root (i.e., the whole document), but an object entry with empty key "". See
RFC 6901 for more information.
RFC 6901, section 4 states that evaluation fails if an object has
non-unique member names. A basic_json object holds one value per key (see object_t
for how duplicate keys are handled while parsing), so evaluating a JSON Pointer never encounters duplicates.
JSON Pointer creation
JSON Pointers can be created from a string:
json::json_pointer p("/nested/one");
Furthermore, a user-defined string literal can be used to achieve the same result:
auto p = "/nested/one"_json_pointer;
The escaping rules of RFC 6901 are implemented. See the constructor documentation for more information.
!!! note "URI fragment representation"
[RFC 6901, section 6](https://tools.ietf.org/html/rfc6901#section-6) also defines a URI fragment identifier
representation (`#/a%20b`). It is not supported: `#!cpp json::json_pointer("#/a")` throws
[`parse_error.107`](../home/exceptions.md#jsonexceptionparse_error107). Strip the leading `#` and
percent-decode the remainder before creating the JSON pointer.
Value access
JSON Pointers can be used in the at, operator[],
and value functions just like object keys or array indices.
// the JSON value from above
auto j = json::parse(R"({
"array": ["A", "B", "C"],
"nested": {
"one": 1,
"two": 2,
"three": [true, false]
}
})");
// access values
auto val = j[""_json_pointer]; // {"array":["A","B","C"],...}
auto val1 = j["/nested/one"_json_pointer]; // 1
auto val2 = j.at(json::json_pointer("/nested/three/1")); // false
auto val3 = j.value(json::json_pointer("/nested/four"), 0); // 0
To read a value with a fallback, use value with a JSON Pointer; to test for existence,
use contains. Neither needs intermediate checks, see
Nested values.
!!! note "Creating intermediate levels that don't exist"
See the [`operator[]` notes](../api/basic_json/operator%5B%5D.md#return-value) for how array vs. object is
decided when a pointer creates intermediate levels that don't exist yet.
Flatten / unflatten
The library implements a function flatten to convert any JSON document into a JSON
object where each key is a JSON Pointer and each value is a primitive JSON value (i.e., a string, boolean, number, or
null).
// the JSON value from above
auto j = json::parse(R"({
"array": ["A", "B", "C"],
"nested": {
"one": 1,
"two": 2,
"three": [true, false]
}
})");
// create flattened value
auto j_flat = j.flatten();
The resulting value j_flat is:
{
"/array/0": "A",
"/array/1": "B",
"/array/2": "C",
"/nested/one": 1,
"/nested/two": 2,
"/nested/three/0": true,
"/nested/three/1": false
}
The reverse function, unflatten recreates the original value.
auto j_original = j_flat.unflatten();
Each leaf of the document becomes one key of the flat object, and unflatten() reverses the mapping:
Left: the example document as a tree; right: the flat object it becomes. `flatten()` converts from left to right, `unflatten()` from right to left.
See also
json_pointer- the JSON Pointer class- Nested values -
value,contains, andataccept JSON Pointers flatten- convert a value to an object of JSON Pointers and primitive valuesunflatten- restore a value from a flattened object- JSON Patch - paths inside a patch are JSON Pointers
- JSON Merge Patch - an alternative patch format that does not use JSON Pointer
operator""_json_pointer- user-defined literal to create a JSON pointer- Element Access - other ways to access elements