json/doc/mkdocs/docs/api/basic_json/operator_ltlt.md

57 lines
1.5 KiB
Markdown
Raw Normal View History

2021-11-02 00:28:53 +03:00
# operator<<(basic_json)
```cpp
std::ostream& operator<<(std::ostream& o, const basic_json& j)
```
2021-11-03 16:51:19 +03:00
2021-11-02 00:28:53 +03:00
Serialize the given JSON value `j` to the output stream `o`. The JSON value will be serialized using the
[`dump`](dump.md) member function.
- The indentation of the output can be controlled with the member variable `width` of the output stream `o`. For
instance, using the manipulator `std::setw(4)` on `o` sets the indentation level to `4` and the serialization result
is the same as calling `dump(4)`.
- The indentation character can be controlled with the member variable `fill` of the output stream `o`. For instance,
the manipulator `std::setfill('\\t')` sets indentation to use a tab character rather than the default space character.
## Parameters
`o` (in, out)
: stream to serialize to
`j` (in)
: JSON value to serialize
## Return value
the stream `o`
## Exceptions
Throws [`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) if a string stored inside the JSON value
is not UTF-8 encoded. Note that unlike the [`dump`](dump.md) member functions, no `error_handler` can be set.
## Complexity
Linear.
2021-11-05 16:26:19 +03:00
## Examples
2021-11-02 00:28:53 +03:00
??? example
The example below shows the serialization with different parameters to `width` to adjust the indentation level.
```cpp
--8<-- "examples/operator_serialize.cpp"
```
Output:
```json
--8<-- "examples/operator_serialize.output"
```
## Version history
- Added in version 1.0.0
- Support for indentation character added in version 3.0.0.