Files
json/docs/mkdocs/docs/api/basic_json_document/index.md
Niels Lohmann d9a71e7bc5 Document the node index of json_view on the architecture page
A new section describes the 16-byte node: its fields, how integers,
floats, and object members are stored, how views navigate without
pointers, and a worked example. The feature page and the pages of
basic_json_document and node_count link to it where they mention the
16 bytes.

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

2.9 KiB

nlohmann::basic_json_document

Defined in header <nlohmann/json_view.hpp>

template<typename BasicJsonType>
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.

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.

Specializations

Member types

  • view_type - the type of view returned by root() (#!cpp basic_json_view<BasicJsonType>)
  • 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

Version history

  • Added in version 3.13.0.