mirror of
https://github.com/nlohmann/json.git
synced 2026-10-02 20:50:32 +00:00
Adds error_handler_t::keep (invalid UTF-8 sequences are left unchanged) as a fourth error_handler_t value, and threads an error_handler parameter through the binary writers and readers: - to_cbor/to_ubjson/to_bjdata/to_bson gain a trailing error_handler parameter (default strict, matching their existing type_error.316 behavior); keep writes a string value or object key's bytes as is instead of throwing, and replace/ignore sanitize it exactly like dump() would, including for the BSON length prefix. to_msgpack and to_bon8 are unchanged. - from_cbor/from_msgpack/from_ubjson/from_bjdata/from_bson gain a trailing error_handler parameter (default keep, i.e. the lenient behavior every binary reader had in 3.12.0 and still has after #5741); strict checks every string value and object key and raises parse_error.113 for ill-formed UTF-8, honoring allow_exceptions; replace/ignore sanitize it like dump() would. from_bon8 is unchanged, since UTF-8 lead bytes are structural there. dump()'s own keep support writes ill-formed bytes as is, even with ensure_ascii, while still \u-escaping well-formed characters around them as usual. The UTF-8 validity check (is_valid_utf8) and the replace/ignore sanitizing logic (sanitize_utf8) now live in string_utils.hpp, shared by the serializer and the binary reader/writer; error_handler_t itself moved to its own header (detail/output/error_handler.hpp) so that string_utils.hpp does not need to depend on serializer.hpp. See #5529 and #5741. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
3.8 KiB
3.8 KiB
nlohmann::basic_json::dump
string_t dump(const int indent = -1,
const char indent_char = ' ',
const bool ensure_ascii = false,
const error_handler_t error_handler = error_handler_t::strict) const;
Serialization function for JSON values. The function tries to mimic Python's
json.dumps() function, and currently supports its indent
and ensure_ascii parameters.
Parameters
indent(in)- If
indentis nonnegative, then array elements and object members will be pretty-printed with that indent level. An indent level of0will only insert newlines.-1(the default) selects the most compact representation. indent_char(in)- The character to use for indentation if
indentis greater than0. The default is(space). ensure_ascii(in)- If
ensure_asciiis true, all non-ASCII characters in the output are escaped with\uXXXXsequences, and the result consists of ASCII characters only. error_handler(in)- how to react on decoding errors; there are four possible values (see
error_handler_t:strict(throws an exception in case a decoding error occurs; default),replace(replace invalid UTF-8 sequences with U+FFFD),ignore(ignore invalid UTF-8 sequences during serialization; all valid bytes are copied to the output unchanged, and invalid bytes are dropped), andkeep(write the ill-formed bytes to the output as is, without escaping them, even ifensure_asciiis#!cpp true; the result is then not valid UTF-8, but equals the input bytes exactly, and well-formed characters around the ill-formed bytes are still escaped as usual)).
Return value
string containing the serialization of the JSON value
Exception safety
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
Exceptions
Throws type_error.316 if a string stored inside the JSON value
is not UTF-8 encoded and error_handler is set to strict
!!! warning "Serializing untrusted input"
When serializing values that may contain invalid or untrusted UTF-8 (e.g., bytes taken directly from network
input), `dump()` throws [`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) in the default
`strict` mode. To serialize such data without throwing, pass
[`error_handler_t::replace`](error_handler_t.md) (substitutes U+FFFD) or
[`error_handler_t::ignore`](error_handler_t.md). Callers that serialize untrusted input on a crash-sensitive path
should either choose a non-strict error handler or wrap `dump()` in a `#!cpp try`/`#!cpp catch`.
See the [FAQ](../../home/faq.md#serializing-untrusted-or-invalid-utf-8) for details.
Complexity
Linear.
Notes
Binary values are serialized as an object containing two keys:
- "bytes": an array of bytes as integers
- "subtype": the subtype as integer or
#!json nullif the binary has no subtype
Examples
??? example
The following example shows the effect of different `indent`, `indent_char`, and `ensure_ascii` parameters to the
result of the serialization.
```cpp
--8<-- "examples/dump.cpp"
```
Output:
```json
--8<-- "examples/dump.output"
```
See also
- to_string returns a string representation of a JSON value
- operator<< serialize to stream
- Serialization - the serialization article
Version history
- Added in version 1.0.0.
- Indentation character
indent_char, optionensure_asciiand exceptions added in version 3.0.0. - Error handlers added in version 3.4.0.
- Serialization of binary values added in version 3.8.0.
- Error handler
keepadded in version 3.13.0.