Files
json/docs/mkdocs/docs/api/basic_json_view/get_string.md
T
Niels Lohmann 94331d1050 Document that read() must not be given a view of the document itself (#6086)
* Document that read() must not be given a view of the document itself

basic_json_document::read() releases the previous text, the decoded
strings and the edits before it parses its input. An input that points
into them (source(), get_string(), number_token(), or a string of an
edited document) is read after its memory was cleared or freed.

A guard that copies such input costs about 1 ns per read() (5 % of a
read of a 7-byte input), which this API exists to avoid, so state the
precondition in read.md, lifetime.md and the pages of the views that
hand out such strings instead, and show how to copy the string first.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* Assert in debug builds that read() is not given its own text

read() releases the document's text, decoded strings, owned image and
edits before it parses, so an input that points into them is read after
release. Without NDEBUG, read() now asserts that a borrowed input, a
copied contiguous input and (before C++17) a string_ref input are not
held by the document: its owned text, its decoded strings, an owned
image, and the current buffer of edited strings. Release builds keep
the documented precondition without a check, which would cost 2-4.5 %
per read() of a small message.

JSON_ASSERT is undefined at the end of json.hpp, so the view uses
assert() through a new internal NLOHMANN_VIEW_ASSERT.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-11-11 22:36:17 +01:00

3.7 KiB

nlohmann::basic_json_view::get_string

string_view_t get_string() const;

Returns the string value as a string_view_t, without copying it.

Return value

The string, as a string_view_t that points either into the document's source() text (a string with no escape sequences), or into the document's own buffer of decoded strings (a string that contains escape sequences, such as #!json "\n" or #!json "\u00e9", which had to be decoded once when the document was parsed).

Exception safety

Strong guarantee: there are no changes to the view or the document it refers to.

Exceptions

Throws type_error.302 if the value is not a string; example: "type must be string, but is array".

As for BasicJsonType, with JSON_DIAGNOSTICS the exception names the JSON Pointer path of the value within the document, and with JSON_DIAGNOSTIC_POSITIONS its bytes in the source text (when materialize() sets them).

Complexity

Constant.

Notes

basic_json_view has no BasicJsonType value stored anywhere, so unlike BasicJsonType, it has no get_ref() to hand out a reference to a stored string_t. get_string() (equivalently, get<string_view_t>()) is the zero-copy alternative: BasicJsonType::get_ref<const string_t&>() is its closest counterpart, except that it returns a view instead of a reference to a value that must already exist.

get_string() borrows the string: nothing is copied, whereas get<std::string>() copies it. For a borrowed document, the returned string_view_t is valid as long as the caller's buffer; for an owned one, as long as the document.

The returned string_view_t is valid exactly as long as the view that produced it — see the validity rules of basic_json_view — and, for a string with no escapes, for as long as the document's source text, which for a borrowed document must remain valid and unmodified for as long as the document exists, since floating-point values and unescaped strings are read from the source lazily.

A string with escapes, a string of a document that owns its text, and an edited string must not be passed to read() of the document it came from: read() releases them before it parses its input. Copy the string first. (A string read unchanged from a borrowed text is not affected.)

Examples

??? example

The example below pulls one field out of a JSON text that stands in for a large API response, and shows that no
`#!cpp std::string` was allocated for it: the returned view still points inside the original buffer. A field that
contains an escape sequence cannot point into the original text — it was decoded once into the document's own
buffer instead — but still avoids a per-field allocation.

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

Output:

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

See also

Version history

  • Added in version 3.13.0.