Files
json/docs/mkdocs/docs/api/basic_json_document/source.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.1 KiB

nlohmann::basic_json_document::source

view_type::string_view_t source() const noexcept;

Returns the parsed text, whether it is borrowed from the caller or owned by the document.

Return value

A #!cpp string_view_t (#!cpp std::string_view on C++17 and newer) over the parsed text, or an empty one if the document is discarded.

Exception safety

No-throw guarantee: this function never throws exceptions.

Complexity

Constant.

Notes

For a borrowed document, source() points directly into the caller's buffer, so it is only valid while that buffer is; see owns_source.

Examples

??? example

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

Output:

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

See also

  • owns_source - return whether the document holds its own copy of the text
  • source_offset - byte offset of a value in the source text

Version history

  • Added in version 3.13.0.