mirror of
https://github.com/nlohmann/json.git
synced 2025-07-25 13:41:56 +03:00
* 🔥 consolidate documentation * ♻️ overwork std specializations * 🚚 move images files to mkdocs * ♻️ fix URLs * 🔧 tweak MkDocs configuration * 🔧 add namespaces * 📝 document deprecations * 📝 document documentation generation * 🚸 improve search * 🚸 add examples * 🚧 start adding documentation for macros * 📝 add note for https://github.com/nlohmann/json/issues/874#issuecomment-1001699139 * 📝 overwork example handling * 📝 fix Markdown tables
1.8 KiB
1.8 KiB
operator<<(basic_json)
std::ostream& operator<<(std::ostream& o, const basic_json& j);
Serialize the given JSON value j
to the output stream o
. The JSON value will be serialized using the
dump
member function.
- The indentation of the output can be controlled with the member variable
width
of the output streamo
. For instance, using the manipulatorstd::setw(4)
ono
sets the indentation level to4
and the serialization result is the same as callingdump(4)
. - The indentation character can be controlled with the member variable
fill
of the output streamo
. For instance, the manipulatorstd::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
if a string stored inside the JSON value
is not UTF-8 encoded. Note that unlike the dump
member functions, no error_handler
can be set.
Complexity
Linear.
Examples
??? 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.
!!! warning "Deprecation"
This function replaces function `#!cpp std::ostream& operator>>(const basic_json& j, std::ostream& o)` which has
been deprecated in version 3.0.0. It will be removed in version 4.0.0. Please replace calls like `#!cpp j >> o;`
with `#!cpp o << j;`.