Files
json/docs/mkdocs/docs/api/basic_json_document/push_back.md
T
Niels Lohmann a47170346a docs: new design for the documentation site (#5934)
* 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>
2026-10-27 07:41:42 +01:00

5.1 KiB

nlohmann::basic_json_document::push_back

template<typename V>
view_type push_back(view_type array, V&& value);

Appends value as a new last element of array. A null array first becomes an empty array, the same way set turns a null object into an empty object.

value is accepted three ways: a basic_json_view of any document — read-only or editable, and it does not have to be array's own document — which is copied so that nothing is shared with the source document afterward; a BasicJsonType value; or anything BasicJsonType can be constructed from (numbers, strings, #!cpp bool, #!cpp nullptr, containers, ...).

Only an editable document (#!cpp view_mode::editable, e.g. json_editable_document) has push_back; calling it on a read-only basic_json_document fails to compile (#!cpp static_assert).

Template parameters

V
the type of value, deduced; see above for what is accepted.

Parameters

array (in)
the array (or null value) to append to
value (in)
the value to append

Return value

a view of the new last element of array, now holding value

Exception safety

Basic exception safety: value is fully encoded — including the checks below — into storage owned by the document before anything already reachable from root() is touched, so a failure while encoding value (an invalid argument, or #!cpp std::bad_alloc) leaves the document completely unchanged, other than memory allocated for the encoding that is not reclaimed. A failure of a later allocation — while array switches from its parsed layout to a growable block, or while that block grows, see Notes — can still leave a partial effect, such as a null array argument already turned into an empty array even though value itself was not appended.

Exceptions

Throws type_error.308 if array is neither an array nor null — the same message BasicJsonType::push_back throws for the same type. Throws invalid_iterator.202 ("view does not belong to this document") if array is a discarded view or a view of a different document. Throws type_error.302 if value is a discarded view or a discarded BasicJsonType value, and type_error.319 if value is (or contains) a binary value — BasicJsonType can hold one, but a json_document cannot. Throws type_error.316 if value is (or contains) a string that is not valid UTF-8, with the same message BasicJsonType::dump() gives for that string.

Complexity

Amortized constant, plus time linear in the size of value to encode it into the document's storage (constant for a scalar, linear in the number of nested values for an array or object): the elements of array move to a growable block of links the first time it is appended to (or set on), and that block itself grows — doubling its capacity, so the cost of growing it amortizes to constant per element — only once it runs out of room. See Notes.

Notes

Like set on a member or an element, push_back never moves an existing element itself — only where array's links to its elements live — so a view of an existing element of array stays valid across a push_back, but any iterator already taken over array is invalidated, since it was walking the old layout. See Edits for what stays valid across an edit in general.

Examples

??? example

The example below appends records to an array one at a time, as they might arrive from a stream of events,
without ever building a `BasicJsonType` value for the array or for the records already in it, and shows that a
view taken from an earlier `push_back` still refers to the same element once later ones have run.

```cpp
--8<-- "examples/basic_json_document__push_back.cpp"
```

Output:

```json
--8<-- "examples/basic_json_document__push_back.output"
```

See also

  • set - replace a value, or set an object member, an array element, or the value a JSON pointer refers to
  • insert - insert an element into an array before a given position
  • erase - remove an object member, an array element, or the value a JSON pointer refers to
  • root - the view of the root value
  • BasicJsonType::push_back - the corresponding function of basic_json
  • Edits - what an edit guarantees, for every overload
  • Zero-copy JSON views - what an edit guarantees and costs

Version history

  • Added in version 3.13.0.