Files
json/docs/mkdocs/docs/api/basic_json_document/index.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

7.6 KiB

nlohmann::basic_json_document

Defined in header <nlohmann/json_view.hpp>

template<typename BasicJsonType, view_mode Mode = view_mode::read_only>
class basic_json_document;

A parsed JSON text, held as a flat index of its values (16 bytes per value) instead of a tree of BasicJsonType values. Strings and floating-point numbers stay in the source text; integers are converted while parsing, and strings that contain escapes are decoded into one buffer owned by the document. basic_json_view is a read-only handle to one value of a basic_json_document; materialize() turns a subtree into the BasicJsonType value that BasicJsonType::parse() would have produced for it.

A document may borrow the text it was parsed from (the caller's buffer must then outlive the document) or own it (a copy, or an rvalue #!cpp std::string that was moved in); see owns_source. basic_json_document is move-only: a copy would duplicate the whole index, the decoded-string buffer, and the owned text, and a move is enough in every use we know of. The index, the decoded strings, and the owned text live in one heap block that the document holds through a #!cpp std::unique_ptr; this is also why views survive a move of the document.

With #!cpp Mode == view_mode::editable, the document also offers set, push_back, insert, and erase to change values in place, see Edits below. The source text itself is never written; a read-only document (#!cpp view_mode::read_only, the default) does not carry any of the bookkeeping edits need, and calling any of them on one fails to compile (#!cpp static_assert).

Template parameters

BasicJsonType
a specialization of basic_json, for instance json or ordered_json. Only 64-bit number_integer_t/number_unsigned_t types are supported; this is checked with a static_assert.
Mode
a view_mode: #!cpp view_mode::editable if the document supports set, push_back, insert, and erase (optional, #!cpp view_mode::read_only by default). See Edits below.

Specializations

Member types

  • view_type - the type of view returned by root() (#!cpp basic_json_view<BasicJsonType, Mode>)
  • value_t - the JSON type enumeration, see basic_json::value_t
  • image_check - how load() validates an image (full, bounds, none), see image_check

Member functions

Parsing

  • parse (static) - deserialize from a compatible input, borrowing or owning it as appropriate
  • parse_copy (static) - deserialize a copy of a compatible input
  • accept (static) - check whether the input is valid JSON
  • read - (re-)parse into this document, reusing its memory

Access

  • root - the view of the root value
  • is_discarded - return whether the last parse failed
  • source - the parsed text
  • owns_source - return whether the document holds its own copy of the text
  • node_count - the number of index entries (values plus object keys)
  • memory_usage - the number of bytes held by the document
  • shrink_to_fit - release unused index capacity

Images

  • save - the document as an image that load() reads without parsing
  • load (static) - read an image written by save()

Edits

  • set - replace a value, or set an object member, an array element, or the value a JSON pointer refers to (editable documents only)
  • push_back - append to an array (editable documents only)
  • insert - insert an element into an array before a given position (editable documents only)
  • erase - remove an object member, an array element, or the value a JSON pointer refers to (editable documents only)

Edits

An editable document (#!cpp view_mode::editable) can be changed after parsing, with set, push_back, insert, and erase; json_editable_document and ordered_json_editable_document are the corresponding specializations. A few points apply to every edit:

  • The source text is never written, and the parsed index never moves: every value keeps the node it was parsed into, so views taken before an edit stay valid, including root(). New values (and the element sequences of an edited array/object) go to storage owned by the document, allocated on demand.
  • A view keeps referring to the same value. After set replaces the value a view refers to, that view sees the new value; a view of a value that a later edit replaces or drops keeps showing what it last held. An edit of an array or object, however, invalidates the iterators taken over it (its members may now live in a different sequence), and a string obtained with get_string() stays valid even as further edits happen (earlier buffers of edited text are kept alive, not overwritten).
  • Values are accepted three ways: a basic_json_view of any document (read-only or editable; it is copied, nothing is shared with the source document), a BasicJsonType value, or anything BasicJsonType can be constructed from (numbers, strings, #!cpp bool, #!cpp nullptr, containers, ...).
  • dump() writes an edited document with members in document order, new members at the end, and, with number_format::source, keeps the spelling of every number that was not itself edited — see Editing a document for why this matters.
  • read() discards all edits, shrink_to_fit() does not move the node index once there are edits, and memory_usage() includes the memory edits use. source_offset() of a value introduced by an edit is #!cpp static_cast<std::size_t>(-1), the same value it reports for a decoded string.

See also

Version history

  • Added in version 3.13.0.