diff --git a/api/basic_json/dump.md b/api/basic_json/dump.md index 210cb91fb..73c47b0cf 100644 --- a/api/basic_json/dump.md +++ b/api/basic_json/dump.md @@ -58,7 +58,9 @@ is not UTF-8 encoded and `error_handler` is set to `strict` ## Complexity -Linear. +Linear in the size of the result. With an `indent`, every line starts with up to `indent` characters per nesting level, +so the result can be larger than the value by a factor of its nesting depth (see +[Parsing untrusted input](../../features/parsing/untrusted_input.md#nesting-depth)). ## Notes diff --git a/api/basic_json/dump/index.html b/api/basic_json/dump/index.html index 418f218ef..de9e88525 100644 --- a/api/basic_json/dump/index.html +++ b/api/basic_json/dump/index.html @@ -2,7 +2,7 @@ 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.
indent (in)indent is nonnegative, then array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. -1 (the default) selects the most compact representation.indent_char (in)indent is greater than 0. The default is (space).ensure_ascii (in)ensure_ascii is true, all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result consists of ASCII characters only.error_handler (in)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), and keep (write the ill-formed bytes to the output as is, without escaping them, even if ensure_ascii is 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)).string containing the serialization of the JSON value
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
Throws type_error.316 if a string stored inside the JSON value is not UTF-8 encoded and error_handler is set to strict
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 in the default strict mode. To serialize such data without throwing, pass error_handler_t::replace (substitutes U+FFFD) or error_handler_t::ignore. Callers that serialize untrusted input on a crash-sensitive path should either choose a non-strict error handler or wrap dump() in a try/catch.
See the FAQ for details.
Linear.
Floating-point numbers are written with the fewest digits that read back as the same value (for double; see number handling).
Binary values are serialized as an object containing two keys:
null if the binary has no subtypeThe following example shows the effect of different indent, indent_char, and ensure_ascii parameters to the result of the serialization.
#include <iostream>
+Serialization function for JSON values. The function tries to mimic Python's json.dumps() function, and currently supports its indent and ensure_ascii parameters.
indent (in)indent is nonnegative, then array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. -1 (the default) selects the most compact representation.indent_char (in)indent is greater than 0. The default is (space).ensure_ascii (in)ensure_ascii is true, all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result consists of ASCII characters only.error_handler (in)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), and keep (write the ill-formed bytes to the output as is, without escaping them, even if ensure_ascii is 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)).string containing the serialization of the JSON value
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
Throws type_error.316 if a string stored inside the JSON value is not UTF-8 encoded and error_handler is set to strict
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 in the default strict mode. To serialize such data without throwing, pass error_handler_t::replace (substitutes U+FFFD) or error_handler_t::ignore. Callers that serialize untrusted input on a crash-sensitive path should either choose a non-strict error handler or wrap dump() in a try/catch.
See the FAQ for details.
Linear in the size of the result. With an indent, every line starts with up to indent characters per nesting level, so the result can be larger than the value by a factor of its nesting depth (see Parsing untrusted input).
Floating-point numbers are written with the fewest digits that read back as the same value (for double; see number handling).
Binary values are serialized as an object containing two keys:
null if the binary has no subtypeThe following example shows the effect of different indent, indent_char, and ensure_ascii parameters to the result of the serialization.
#include <iostream>
#include <nlohmann/json.hpp>
using nlohmann::json;
@@ -105,4 +105,4 @@
[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9
string with replaced invalid characters: "ä�ü"
string with ignored invalid characters: "äü"
-to_string - returns a string representation of a JSON valueoperator<< - serialize to streambasic_json_view::dump - the corresponding function of basic_json_view, serializing directly from a flat index without building a basic_json valueindent_char, option ensure_ascii and exceptions added in version 3.0.0.keep added in version 3.13.0 unreleased.