Files
json/docs/mkdocs/docs/api/basic_json_document/root.md
T
Niels Lohmann 061c30310e Document json_document and json_view
- API pages for basic_json_document and basic_json_view, one per member,
  and for the four aliases, each with an example
- features/json_view.md: the problem the view solves, ownership and
  lifetime, what matches basic_json::parse() and what differs, and when
  to choose json, ordered_json, SAX, or the view
- the examples show why one would use the view, not only how: borrowed
  vs. owned input, reading a few fields and materializing one subtree,
  reusing a document across many messages
- registered in the mkdocs navigation, llms.txt, the docset, the
  exceptions page (out_of_range.416), architecture.md, the integration
  page, and the README; the yyjson credit is added to the README and
  license.md

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

1.2 KiB

nlohmann::basic_json_document::root

view_type root() const noexcept;

Returns a view of the root value of the document.

Return value

A view_type (i.e. #!cpp basic_json_view<BasicJsonType>) for the root value, or a discarded view if the document is discarded.

Exception safety

No-throw guarantee: this function never throws exceptions.

Complexity

Constant.

Notes

root() is a cheap handle into the document's index, not a copy of anything; call it as often as needed. The returned view is valid under the same conditions as any other view of the document -- see Object inspection -- in particular, it is invalidated by the next read() or shrink_to_fit() on this document.

Examples

??? example

```cpp
--8<-- "examples/basic_json_document__root.cpp"
```

Output:

```json
--8<-- "examples/basic_json_document__root.output"
```

See also

Version history

  • Added in version 3.13.0.