mirror of
https://github.com/nlohmann/json.git
synced 2026-10-06 22:47:13 +00:00
Add basic_json_view::dump() and the comparison operators, and read floats from the parser's digit layout instead of rescanning the token. dump(indent, indent_char, ensure_ascii, number_format) writes a value the way ordered_json::parse(text).dump() writes it for the same arguments: members in document order, all of them should a key occur more than once; strings escaped by the same rules, using the library's scanning kernels; floats written with the library's to_chars conversion, so the output equals basic_json's byte for byte; integers copied from the source, where they are already canonical, except -0, which parse() reads as 0. There is no error_handler argument, because the view only holds valid UTF-8. number_format::source copies numbers exactly as they appear in the source (e.g. "1.50", "1E2", "-0"), which basic_json cannot provide. operator<< takes the indentation from the stream width, as for basic_json. The writer walks iteratively, so nesting depth is limited by memory only. operator== and operator!= compare two views, or a view and a basic_json value in either order, by the rules basic_json's operator== uses: numbers compare by value across their types, objects compare by their members with duplicate keys resolved as parse() resolves them, member order matters only where the object type keeps one, and discarded views compare as discarded basic_json values do, including under JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON. Nothing is materialized except single scalars. While parsing, the view now records where the integer digits, the fraction digits, and the exponent of a float token are, so floats and doubles with at most 19 digits are read from that layout with the library's decimal_to_float() instead of rescanning the token. Both round correctly, so the values are those of parse(). get<double>(), materialize(), dump(), and the comparisons all use it. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
3.2 KiB
3.2 KiB
nlohmann::operator<<(basic_json), nlohmann::operator<<(json_pointer)
std::ostream& operator<<(std::ostream& o, const basic_json& j); // (1)
std::ostream& operator<<(std::ostream& o, const json_pointer& ptr); // (2)
- Serialize the given JSON value
jto the output streamo. The JSON value will be serialized using thedumpmember function.- The indentation of the output can be controlled with the member variable
widthof the output streamo. For instance, using the manipulatorstd::setw(4)onosets the indentation level to4and the serialization result is the same as callingdump(4). - The indentation character can be controlled with the member variable
fillof the output streamo. For instance, the manipulatorstd::setfill('\\t')sets indentation to use a tab character rather than the default space character.
- The indentation of the output can be controlled with the member variable
- Write a string representation of the given JSON pointer
ptrto the output streamo. The string representation is obtained using theto_stringmember function.
Parameters
o(in, out)- stream to write to
j(in)- JSON value to serialize
ptr(in)- JSON pointer to write
Return value
the stream o
Exceptions
- Throws
type_error.316if a string stored inside the JSON value is not UTF-8 encoded. Note that unlike thedumpmember functions, noerror_handlercan be set. - None.
Complexity
Linear.
Notes
!!! warning "Deprecation"
Function `#!cpp std::ostream& operator<<(std::ostream& o, const basic_json& j)` 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;`.
See the [migration guide](../integration/migration_guide.md#miscellaneous-functions) for how to update existing code.
Examples
??? example "Example: (1) serialize JSON value to stream"
The example below shows the serialization with different parameters to `width` to adjust the indentation level.
```cpp
--8<-- "examples/operator_ltlt__basic_json.cpp"
```
Output:
```json
--8<-- "examples/operator_ltlt__basic_json.output"
```
??? example "Example: (2) write JSON pointer to stream"
The example below shows how to write a JSON pointer to a stream.
```cpp
--8<-- "examples/operator_ltlt__json_pointer.cpp"
```
Output:
```json
--8<-- "examples/operator_ltlt__json_pointer.output"
```
See also
- dump - serialize to a JSON-formatted string
basic_json_view::operator<<- the corresponding operator forbasic_json_view- Serialization - the serialization article
Version history
- Added in version 1.0.0. Added support for indentation character and deprecated
#!cpp std::ostream& operator>>(const basic_json& j, std::ostream& o)in version 3.0.0. - Added in version 3.11.0.