mirror of
https://github.com/nlohmann/json.git
synced 2026-11-07 13:09:21 +00:00
* 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>
113 lines
5.0 KiB
Markdown
113 lines
5.0 KiB
Markdown
# Modifying values
|
|
|
|
Once a JSON value exists, its content can be changed: elements can be added, replaced, merged, and removed. This page
|
|
gives an overview of the available operations. For read access, see [element access](element_access/index.md).
|
|
|
|
## Adding to arrays
|
|
|
|
New elements are appended to an array with [`push_back`](../api/basic_json/push_back.md) or constructed in place with
|
|
[`emplace_back`](../api/basic_json/emplace_back.md). If the value is `#!json null`, it is converted to an array first, so
|
|
these functions can also be used to build an array from scratch.
|
|
|
|
```cpp
|
|
json j; // null
|
|
j.push_back(1); // [1]
|
|
j.push_back(2); // [1,2]
|
|
j.emplace_back(3); // [1,2,3]
|
|
|
|
// operator+= is a shorthand for push_back
|
|
j += 4; // [1,2,3,4]
|
|
```
|
|
|
|
## Adding to objects
|
|
|
|
The most common way to add or replace a member is [`operator[]`](element_access/unchecked_access.md), which inserts the
|
|
key if it does not exist yet:
|
|
|
|
```cpp
|
|
json j;
|
|
j["name"] = "Mary"; // {"name":"Mary"}
|
|
j["name"] = "John"; // {"name":"John"} (replaced)
|
|
```
|
|
|
|
[`emplace`](../api/basic_json/emplace.md) inserts a member only if the key is not already present, and reports whether
|
|
the insertion happened — useful for "add if absent" semantics.
|
|
|
|
## Merging objects
|
|
|
|
To merge one object into another, [`update`](../api/basic_json/update.md) copies all members from another object
|
|
(similar to Python's `dict.update`). This is the idiomatic way to combine two objects. It has two modes:
|
|
|
|
- By default, the merge is shallow: existing keys are overwritten, even if both values are objects.
|
|
- With `merge_objects = #!cpp true`, keys whose values are objects in both JSON values are merged recursively.
|
|
Everything else is overwritten. In particular, arrays are replaced, not concatenated.
|
|
|
|
??? example
|
|
|
|
```cpp
|
|
--8<-- "examples/update.cpp"
|
|
```
|
|
|
|
Output:
|
|
|
|
```json
|
|
--8<-- "examples/update.output"
|
|
```
|
|
|
|
A common use of the recursive mode is combining defaults with user settings. Nested defaults that the user did not set
|
|
are kept:
|
|
|
|
```cpp
|
|
json defaults = {{"log", {{"level", "info"}, {"file", "app.log"}}}, {"retries", 3}};
|
|
json user_settings = {{"log", {{"level", "debug"}}}};
|
|
|
|
json config = defaults;
|
|
config.update(user_settings, true);
|
|
// {"log":{"file":"app.log","level":"debug"},"retries":3}
|
|
```
|
|
|
|
[JSON Merge Patch](merge_patch.md) ([RFC 7396](https://tools.ietf.org/html/rfc7396)) also merges objects recursively,
|
|
but it is a different tool: a `#!json null` in the patch means "remove this key". It is meant for applying merge patch
|
|
documents (e.g., received via HTTP PATCH). To merge configuration-like objects, use `#!cpp update(..., true)`. To apply
|
|
a sequence of well-defined edit operations, see [JSON Patch](json_patch.md).
|
|
|
|
## Removing elements
|
|
|
|
Elements are removed with [`erase`](../api/basic_json/erase.md), which accepts an object key, an array index, or an
|
|
iterator. [`clear`](../api/basic_json/clear.md) empties a value while keeping its type, and
|
|
[`operator[]`](element_access/unchecked_access.md) combined with assignment can overwrite a value entirely.
|
|
|
|
```cpp
|
|
json j = {{"a", 1}, {"b", 2}, {"c", 3}};
|
|
j.erase("b"); // {"a":1,"c":3}
|
|
|
|
json a = {1, 2, 3, 4};
|
|
a.erase(1); // [1,3,4] (erase by index)
|
|
```
|
|
|
|
## Editing a document
|
|
|
|
The operations above work on a `basic_json` value. A JSON text that was parsed into a
|
|
[document](json_view.md#editing-a-document) can also be changed in place, without building a `basic_json` tree for the
|
|
parts that are not touched: [`set`](../api/basic_json_document/set.md) replaces a value, adds or replaces an object
|
|
member, or replaces an array element (also through a JSON pointer),
|
|
[`push_back`](../api/basic_json_document/push_back.md) appends to an array,
|
|
[`insert`](../api/basic_json_document/insert.md) inserts into an array (unlike `basic_json::insert`, it has no object
|
|
overload; members are added with `set`), and [`erase`](../api/basic_json_document/erase.md) removes a member or an
|
|
element. These functions are available on the
|
|
editable document types, such as [`json_editable_document`](../api/json_editable_document.md).
|
|
|
|
## See also
|
|
|
|
- [`push_back`](../api/basic_json/push_back.md) - / [`emplace_back`](../api/basic_json/emplace_back.md) - append to an array
|
|
- [`emplace`](../api/basic_json/emplace.md) - insert into an object if the key is absent
|
|
- [`update`](../api/basic_json/update.md) - merge objects (shallow, or recursive with `merge_objects`)
|
|
- [`merge_patch`](../api/basic_json/merge_patch.md) - apply an RFC 7396 merge patch
|
|
- [`erase`](../api/basic_json/erase.md) - / [`clear`](../api/basic_json/clear.md) - remove elements
|
|
- [JSON Patch and Diff](json_patch.md) - and [JSON Merge Patch](merge_patch.md) - structured modifications
|
|
- [Editing a document](json_view.md#editing-a-document) - change a parsed document in place
|
|
- [`insert`](../api/basic_json/insert.md) - insert elements at an iterator position or merge a range
|
|
- [`operator+=`](../api/basic_json/operator+=.md) - shorthand for `push_back`
|
|
- [Element Access](element_access/index.md) - read elements
|
|
- [Iterators](iterators.md) - iterate over elements
|