Files
json/docs/mkdocs/docs/api/basic_json_document/index.md
T
Niels Lohmann 274be40921 Document insert() and erase() of editable json_documents
Add API reference pages for basic_json_document::insert and
basic_json_document::erase, matching the style of set.md and
push_back.md: signatures, parameters, return values, exception safety,
exceptions with their exact ids and messages, complexity, notes on
duplicate keys and view/iterator validity, and an example.

Add example programs basic_json_document__insert.cpp and
basic_json_document__erase.cpp with their expected output, each
comparing an edit on an editable document with the same edit on a
plain json value to show what is preserved: member order, the
spelling of untouched numbers, and, for insert, that a view taken
before the insert keeps referring to the same element after its index
shifts.

Register both new pages in mkdocs.yml, docSet.sql, and the member list
of basic_json_document/index.md, add cross-references to them from
set.md and push_back.md, and mention insert/erase in the "Editing a
document" section of features/json_view.md.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-09-30 15:09:14 +02:00

6.6 KiB

nlohmann::basic_json_document

Defined in header <nlohmann/json_view.hpp>

template<typename BasicJsonType, bool Editable = false>
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 numbers stay in the source text; only 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 back 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: copying a document would either duplicate a potentially large index and text, or leave two documents claiming to borrow the same buffer, so it is disabled.

With #!cpp Editable == true, 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 Editable == false, 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.
Editable
whether the document supports set, push_back, insert, and erase (optional, #!cpp false by default). See Edits below.

Specializations

Member types

  • view_type - the type of view returned by root() (#!cpp basic_json_view<BasicJsonType, Editable>)
  • value_t - the JSON type enumeration, see basic_json::value_t

Member functions

  • (constructor)
  • 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
  • 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
  • set - replace a value, or set an object member, an array element, or the value a JSON pointer refers to (#!cpp Editable documents only)
  • push_back - append to an array (#!cpp Editable documents only)
  • insert - insert an element into an array before a given position (#!cpp Editable documents only)
  • erase - remove an object member, an array element, or the value a JSON pointer refers to (#!cpp Editable documents only)

Edits

An editable document (#!cpp Editable == true) 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.

Version history

  • Added in version 3.13.0.