Files
json/api/basic_json_view/basic_json_view.md
T
2026-10-27 12:14:00 +00:00

2.6 KiB

nlohmann::basic_json_view::basic_json_view

// (1)
basic_json_view() noexcept = default;

// (2) only for Mode == view_mode::editable
basic_json_view(const basic_json_view<BasicJsonType, view_mode::read_only>& other) noexcept;
  1. Creates an invalid (discarded) view: type() is #!cpp value_t::discarded and is_discarded() is #!cpp true.
  2. Converts a view of a read-only document into a view of an editable document (implicitly), so that a function taking a json_editable_view accepts both. The result is a view of the same value of the same read-only document. See view_mode for why there is no conversion the other way.

Parameters

other (in)
a view of a read-only document with the same BasicJsonType

Apart from these, a view is obtained from a basic_json_document, via root() or by navigating into a container with operator[], at, front, back, find, or iteration.

Exception safety

No-throw guarantee: these constructors never throw exceptions.

Complexity

Constant.

Notes

basic_json_view is trivially copyable (it holds two pointers), so a default-constructed view can be used as a placeholder for "no value yet" and later be assigned a real view.

Examples

??? example

The example below shows the default constructor and that a `basic_json_view` is a small, copyable handle.

```cpp
#include <iostream>
#include <nlohmann/json_view.hpp>

int main()
{
    std::cout << std::boolalpha;

    // the default constructor is the only public one: it creates an invalid
    // (discarded) view, useful as a "no value yet" placeholder
    nlohmann::json_view v;
    std::cout << v.is_discarded() << '\n';

    // views are trivially copyable handles (two pointers); the document owns
    // the actual data
    nlohmann::json_view copy = v;
    std::cout << copy.is_discarded() << '\n';
}

```

Output:

```json
true
true

```

See also

Version history

  • Added in version 3.13.0.