Files
json/docs/mkdocs/docs/api/basic_json_document/read.md
Niels Lohmann 2d3492dccb 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 15:07:21 +02:00

2.3 KiB

nlohmann::basic_json_document::read

template<typename InputType>
void read(InputType&& input,
         const bool allow_exceptions = true,
         const bool ignore_comments = false,
         const bool ignore_trailing_commas = false);

(Re-)parses input into #!cpp *this, discarding the document's previous value and reusing its memory (the node index, the decoded-string buffer, and, if applicable, the owned copy of the text) rather than allocating a fresh document. parse() is implemented in terms of this function, applied to a default-constructed document.

Template parameters

InputType
A compatible input; see parse.

Parameters

input (in)
Input to parse from.
allow_exceptions (in)
whether to throw exceptions in case of a parse error (optional, #!cpp true by default)
ignore_comments (in)
whether comments should be ignored and treated like whitespace (#!cpp true) or yield a parse error (#!cpp false); (optional, #!cpp false by default)
ignore_trailing_commas (in)
whether trailing commas in arrays or objects should be ignored and treated like whitespace (#!cpp true) or yield a parse error (#!cpp false); (optional, #!cpp false by default)

Exceptions

Same as parse.

Complexity

Linear in the length of the input.

Notes

Every view taken from #!cpp *this before the call -- including the previous root() -- is invalidated, whether or not the new parse succeeds; take fresh views from root() afterward.

input is borrowed or owned by the same rules as parse(); a document can borrow on one call and own on the next, since ownership is decided freshly each time.

Examples

??? example

The example below parses a sequence of messages into the same document, reusing its memory instead of allocating
a new document for each one.

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

Output:

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

See also

  • parse - deserialize from a compatible input
  • root - the view of the root value

Version history

  • Added in version 3.13.0.