Files
json/docs/mkdocs/docs/api/basic_json_document/index.md
T
Niels Lohmann 8a0679558f Apply the review's wording fixes to the json_view documentation
- Copying is disabled because of its cost, not because of two documents
  borrowing the same buffer; the unique_ptr follows from move-only.
- Inputs: streams, FILE*, and wide characters (strings, arrays, pointers)
  are all supported and owned; point to parse_copy() for owned copies.
- The error replay only runs for failing input, builds no values, and is
  skipped with allow_exceptions = false.
- read() writes copied inputs into the previous owned copy.
- Views can dangle like std::string_view, and why there is no
  shared/weak pointer design.
- The 4 GiB limit (16-byte nodes, 16-byte scanner steps), streams being
  read to the end first, which macros apply, why materialize() has no
  source positions, and wording nits (accept() vs basic_json::accept(),
  is_binary, is_discarded, see-also lists, size table, "back into").

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-16 08:14:21 +02:00

3.0 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 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.

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.