• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# basic_json::dump
2
3```cpp
4string_t dump(const int indent = -1,
5              const char indent_char = ' ',
6              const bool ensure_ascii = false,
7              const error_handler_t error_handler = error_handler_t::strict) const;
8```
9
10Serialization function for JSON values. The function tries to mimic Python's `json.dumps()` function, and currently
11supports its `indent` and `ensure_ascii` parameters.
12
13## Parameters
14
15`indent` (in)
16:   If `indent` is nonnegative, then array elements and object members will be pretty-printed with that indent level. An
17    indent level of `0` will only insert newlines. `-1` (the default) selects the most compact representation.
18
19`indent_char` (in)
20:   The character to use for indentation if `indent` is greater than `0`. The default is ` ` (space).
21
22`ensure_ascii` (in)
23:   If `ensure_ascii` is true, all non-ASCII characters in the output are escaped with `\uXXXX` sequences, and the
24    result consists of ASCII characters only.
25
26`error_handler` (in)
27:   how to react on decoding errors; there are three possible values (see [`error_handler_t`](error_handler_t.md):
28    `strict` (throws and exception in case a decoding error occurs; default), `replace` (replace invalid UTF-8 sequences
29    with U+FFFD), and `ignore` (ignore invalid UTF-8 sequences during serialization; all bytes are copied to the output
30    unchanged).
31
32## Return value
33
34string containing the serialization of the JSON value
35
36## Exception safety
37
38Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
39
40## Complexity
41
42Linear.
43
44## Notes
45
46Binary values are serialized as object containing two keys:
47
48- "bytes": an array of bytes as integers
49- "subtype": the subtype as integer or `#!json null` if the binary has no subtype
50
51## Example
52
53??? example
54
55    The following example shows the effect of different `indent`, `indent_char`, and `ensure_ascii` parameters to the
56    result of the serialization.
57
58    ```cpp
59    --8<-- "examples/dump.cpp"
60    ```
61
62    Output:
63
64    ```json
65    --8<-- "examples/dump.output"
66    ```
67