Files
json/docs/mkdocs/docs/api/basic_json_view/index.md
T
Niels Lohmann 4303ba674d Add json_document and json_view: node index, parser, and document
Add json_document and json_view, a read-only, zero-copy index of a
JSON text, as the first public slice of the zero-copy view (#5295).

A parse produces a flat array of 16-byte nodes in document order,
one per value and one per object key. Strings stay in the source
text; escaped strings are decoded into an arena. Integers are
converted while their digits are in the cache; floats keep only
their digit layout and are converted on read. Containers store the
size of their subtree, so a reader can step over one in constant
time. A document makes a handful of allocations, however many
values it has.

The parser accepts exactly what json::parse accepts, with every
combination of ignore_comments and ignore_trailing_commas, with and
without a trailing NUL, and under JSON_STRICT_NUL_HANDLING. It is
portable C++11 and does not depend on byte order.

basic_json_document adds parse, parse_copy, accept, read (reuses a
document's memory), root, is_discarded, source, owns_source,
node_count, memory_usage, and shrink_to_fit. basic_json_view adds
type, the is_* queries, operator bool, size, empty, materialize,
and source_offset. A parse error throws the same exception
basic_json::parse would throw for the same input, message and
position included.

detail::abi_config keeps JSON_STRICT_NUL_HANDLING and
JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON readable after json.hpp
undefines them, in the ABI namespace so they always match the
basic_json in use.

A NUL byte that ends a // comment is the end of the input, as in
parse() since #5696.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-06 10:48:06 +02:00

3.6 KiB

nlohmann::basic_json_view

Defined in header <nlohmann/json_view.hpp>

template<typename BasicJsonType>
class basic_json_view;

A read-only handle to one value of a basic_json_document: two pointers (a pointer to the document and a pointer into its index), trivially copyable. A view is valid as long as

  • the document is alive,
  • the document has not been re-parsed with read() (or parse() into it) or shrunk with shrink_to_fit() since the view was taken, and
  • if the document borrows its source text, that text is still alive.

Moving the document itself does not invalidate its views: the index is heap-allocated independently of the basic_json_document object.

basic_json_view provides the read-only part of the BasicJsonType interface: the type-inspection functions, and materialize() to build the BasicJsonType value of a subtree on demand. It does not (yet) provide element access, iteration, get<T>(), JSON Pointer support, dump(), or comparison.

Template parameters

BasicJsonType
a specialization of basic_json, matching the basic_json_document the view was taken from.

Specializations

Member types

  • value_t - the JSON type enumeration, see basic_json::value_t
  • string_t, number_integer_t, number_unsigned_t, number_float_t, json_pointer - the corresponding member types of BasicJsonType
  • size_type - #!cpp std::size_t
  • string_view_t - #!cpp std::string_view on C++17 and newer, a minimal internal substitute otherwise

Member functions

Object inspection

  • type - return the type of the value
  • is_null - return whether the value is null
  • is_boolean - return whether the value is a boolean
  • is_number - return whether the value is a number
  • is_number_integer - return whether the value is an integer number
  • is_number_unsigned - return whether the value is an unsigned integer number
  • is_number_float - return whether the value is a floating-point number
  • is_string - return whether the value is a string
  • is_array - return whether the value is an array
  • is_object - return whether the value is an object
  • is_binary - return whether the value is a binary array (always #!cpp false)
  • is_primitive - return whether the type is primitive
  • is_structured - return whether the type is structured
  • is_discarded - return whether the view is invalid
  • operator bool - return whether the view refers to a value

Capacity

  • size - return the number of elements
  • empty - return whether the value has no elements

Conversion

  • materialize - build the BasicJsonType value of this subtree

Source access

  • source_offset - byte offset of this value in the document's source text

Version history

  • Added in version 3.13.0.