2020-07-25 20:40:50 +08:00
|
|
|
# basic_json::dump
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
string_t dump(const int indent = -1,
|
|
|
|
const char indent_char = ' ',
|
|
|
|
const bool ensure_ascii = false,
|
2020-08-12 19:41:59 +08:00
|
|
|
const error_handler_t error_handler = error_handler_t::strict) const;
|
2020-07-25 20:40:50 +08:00
|
|
|
```
|
|
|
|
|
2020-08-12 19:41:59 +08:00
|
|
|
Serialization function for JSON values. The function tries to mimic Python's `json.dumps()` function, and currently
|
|
|
|
supports its `indent` and `ensure_ascii` parameters.
|
2020-07-25 20:40:50 +08:00
|
|
|
|
|
|
|
## Parameters
|
|
|
|
|
|
|
|
`indent` (in)
|
2020-08-12 19:41:59 +08:00
|
|
|
: If `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.
|
2020-07-25 20:40:50 +08:00
|
|
|
|
|
|
|
`indent_char` (in)
|
2020-08-12 19:41:59 +08:00
|
|
|
: The character to use for indentation if `indent` is greater than `0`. The default is ` ` (space).
|
2020-07-25 20:40:50 +08:00
|
|
|
|
|
|
|
`ensure_ascii` (in)
|
2020-08-12 19:41:59 +08:00
|
|
|
: If `ensure_ascii` is true, all non-ASCII characters in the output are escaped with `\uXXXX` sequences, and the
|
|
|
|
result consists of ASCII characters only.
|
2020-07-25 20:40:50 +08:00
|
|
|
|
|
|
|
`error_handler` (in)
|
2020-08-12 19:41:59 +08:00
|
|
|
: how to react on decoding errors; there are three possible values (see [`error_handler_t`](error_handler_t.md):
|
|
|
|
`strict` (throws and exception in case a decoding error occurs; default), `replace` (replace invalid UTF-8 sequences
|
|
|
|
with U+FFFD), and `ignore` (ignore invalid UTF-8 sequences during serialization; all bytes are copied to the output
|
|
|
|
unchanged).
|
2020-07-25 20:40:50 +08:00
|
|
|
|
|
|
|
## Return value
|
|
|
|
|
|
|
|
string containing the serialization of the JSON value
|
|
|
|
|
|
|
|
## Exception safety
|
|
|
|
|
2020-08-12 19:41:59 +08:00
|
|
|
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
2020-07-25 20:40:50 +08:00
|
|
|
|
|
|
|
## Complexity
|
|
|
|
|
|
|
|
Linear.
|
|
|
|
|
|
|
|
## Notes
|
|
|
|
|
|
|
|
Binary values are serialized as object containing two keys:
|
|
|
|
|
|
|
|
- "bytes": an array of bytes as integers
|
|
|
|
- "subtype": the subtype as integer or `#!json null` if the binary has no subtype
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
2020-07-27 20:07:13 +08:00
|
|
|
??? example
|
|
|
|
|
2020-08-12 19:41:59 +08:00
|
|
|
The following example shows the effect of different `indent`, `indent_char`, and `ensure_ascii` parameters to the
|
|
|
|
result of the serialization.
|
2020-07-25 20:40:50 +08:00
|
|
|
|
2020-07-27 20:07:13 +08:00
|
|
|
```cpp
|
|
|
|
--8<-- "examples/dump.cpp"
|
|
|
|
```
|
|
|
|
|
|
|
|
Output:
|
|
|
|
|
|
|
|
```json
|
|
|
|
--8<-- "examples/dump.output"
|
|
|
|
```
|