mirror of
https://github.com/nlohmann/json.git
synced 2026-10-30 17:35:42 +00:00
* docs: new design for the documentation site Apply the brand to the Material for MkDocs site: Inter and JetBrains Mono served by the site (upright and real italic), brand colours, blue header band with the syntax pattern, wordmark in the header, new home page (hero, cards, animated feature tour), 404 page, polish for tabs, search, footer, focus rings, tables and code blocks, and theme features (code copy, previous/next footer, TOC follow, search suggestions). Draw the number railroad diagram, the parser callback events and the iterator illustrations as inline SVGs that follow the light and dark scheme. The generator scripts are in docs/mkdocs/scripts/figures; the feature slideshow (json-light.svg, json-dark.svg, replacing json.gif on the home page) is built by docs/mkdocs/scripts/slideshow. Decision nodes in five flowcharts become hexagons, which need far less room. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: add figures, table captions and a macro chooser - Figures with numbered captions: SAX stepper, json_view layout and edit stepper, exception map, JSON pointer and patch walkers, QA pipeline, scaling and binary size charts, roadmap lanes, release timeline, integration decision tree and namespace builder - Table captions (nj-table-caption) on 9 pages - Compile times: modules and PCH charts, filesystem section, re-measured Figure 1, links between features/modules.md and compile_times.md - Interactive macro chooser for features/arbitrary_types.md - Link check: exclude URLs that answer the plugin with 400/401/419 or are dead (customers.md) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: macro chooser generates adl_serializer for non-default-constructible types Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: cross-link concept pages and API pages in See also sections Add See also links in both directions between the concept pages (features, home, integration) and the API pages, write API links in code font, give every entry a comment after a dash, and show the callback event figure on the parser callbacks page. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * docs: address review comments on the new design - Home: add a "Latest release" button - Sponsors: update the list and group it by tier - Features: list all feature pages on the index page - Releases: add highlights per release, link the 4.0 roadmap, add the first commit to the timeline - Exceptions: prefix all example messages with the exception name - API: no code font for format names in the See also links of the to_/from_ pages - Merge Patch: show the document before, the patch, and the result - Replace "--" with typographic dashes, mark external links with an icon, center Mermaid diagrams Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
135 lines
5.8 KiB
Markdown
135 lines
5.8 KiB
Markdown
# Serialization
|
|
|
|
Serialization is the process of turning a JSON value back into JSON text. It is the counterpart to
|
|
[parsing](parsing/index.md). The central function is [`dump`](../api/basic_json/dump.md), which returns the JSON text as
|
|
a string.
|
|
|
|
```cpp
|
|
json j = {{"pi", 3.141}, {"happy", true}};
|
|
|
|
std::string s = j.dump(); // {"happy":true,"pi":3.141}
|
|
```
|
|
|
|
To write a value directly to a stream (for example, a file or `#!cpp std::cout`), the
|
|
[`operator<<`](../api/operator_ltlt.md) is provided:
|
|
|
|
```cpp
|
|
std::cout << j << std::endl;
|
|
```
|
|
|
|
!!! note "String, not raw value"
|
|
|
|
`dump` always returns a **JSON text**. Serializing a JSON string therefore includes the surrounding quotes and
|
|
escapes special characters. To obtain the *contained* string value without quotes, use
|
|
[`get<std::string>()`](conversions.md) instead of `dump`. See the [converting values](conversions.md) page.
|
|
|
|
## Pretty-printing
|
|
|
|
By default, `dump` produces the most compact representation without any superfluous whitespace. Passing a non-negative
|
|
`indent` argument pretty-prints the output with the given number of spaces per level:
|
|
|
|
??? example "Example: pretty-print JSON values with `dump()`"
|
|
|
|
```cpp
|
|
--8<-- "examples/dump.cpp"
|
|
```
|
|
|
|
Output:
|
|
|
|
```json
|
|
--8<-- "examples/dump.output"
|
|
```
|
|
|
|
The indentation character can be changed with the second argument (e.g., a tab `#!cpp '\t'`). An `indent` of `0` inserts
|
|
newlines but no leading spaces, and the default of `#!cpp -1` selects the compact single-line form.
|
|
|
|
## Non-ASCII characters
|
|
|
|
Strings are stored and serialized as UTF-8 (see [types](types/index.md#strings)). By default, `dump` copies valid
|
|
non-ASCII characters as-is. Setting the third argument `ensure_ascii` to `#!cpp true` escapes all non-ASCII characters
|
|
with `\uXXXX` sequences, so that the output contains only ASCII characters:
|
|
|
|
```cpp
|
|
json j = "苹果";
|
|
j.dump(); // "苹果"
|
|
j.dump(-1, ' ', true); // "\u82f9\u679c"
|
|
```
|
|
|
|
## Handling invalid UTF-8
|
|
|
|
If a string contains invalid UTF-8 sequences (for example, because it holds data in another encoding such as Latin-1),
|
|
serialization fails by default. The fourth argument of `dump` selects an
|
|
[`error_handler`](../api/basic_json/error_handler_t.md):
|
|
|
|
- `strict` (default) — throw a [`type_error.316`](../home/exceptions.md#jsonexceptiontype_error316) exception.
|
|
- `replace` — replace invalid bytes with the Unicode replacement character U+FFFD (`�`).
|
|
- `ignore` — silently drop invalid bytes.
|
|
- `keep` — copy invalid bytes to the output unchanged; the result is not valid UTF-8.
|
|
|
|
??? example "Example: serialize invalid UTF-8 with different error handlers"
|
|
|
|
```cpp
|
|
--8<-- "examples/error_handler_t.cpp"
|
|
```
|
|
|
|
Output:
|
|
|
|
```json
|
|
--8<-- "examples/error_handler_t.output"
|
|
```
|
|
|
|
!!! tip "Avoiding invalid UTF-8"
|
|
|
|
The best fix is to ensure that all strings are UTF-8 encoded before storing them. See the
|
|
[FAQ on non-ASCII characters](../home/faq.md#parse-errors-reading-non-ascii-characters) for how to convert wide or
|
|
Latin-1 strings.
|
|
|
|
## Numbers, NaN, and binary values
|
|
|
|
- **Numbers** are serialized with enough precision to round-trip; see [number serialization](types/number_handling.md#number-serialization).
|
|
- **NaN and infinity** cannot be represented in JSON and are serialized as `#!json null`; see
|
|
[NaN handling](types/number_handling.md#nan-handling). The [binary formats](binary_formats/index.md) can preserve
|
|
them.
|
|
- **Binary values** have no JSON representation and are serialized as a helper object for debugging only; see
|
|
[binary values](binary_values.md#serialization).
|
|
|
|
## Using `std::format`, `std::print`, and `fmt`
|
|
|
|
Since version 3.12.0, JSON values can be formatted directly with C++20's
|
|
[`std::format`](https://en.cppreference.com/w/cpp/utility/format/format) whenever the standard library provides the
|
|
`<format>` header (controlled by [`JSON_HAS_STD_FORMAT`](../api/macros/json_has_std_format.md)). This is enabled by the
|
|
[`std::formatter<basic_json>`](../api/basic_json/std_formatter.md) specialization, which also makes JSON values work with
|
|
`std::format_to` and with C++23's `std::print`/`std::println`:
|
|
|
|
```cpp
|
|
std::print("{}", j); // compact, like j.dump()
|
|
std::print("{:2}", j); // pretty-printed with indent 2 (like j.dump(2))
|
|
std::println("{:#}", j); // pretty-printed with the default indent
|
|
```
|
|
|
|
The format spec mirrors the `dump` parameters: `#!cpp "{:#}"` pretty-prints, a width such as `#!cpp "{:2}"` sets the
|
|
indent, and a fill-and-align prefix such as `#!cpp "{:.>#}"` sets the indent character.
|
|
|
|
For the [{fmt}](https://github.com/fmtlib/fmt) library, the library ships a
|
|
[`format_as`](../api/basic_json/format_as.md) helper. Note its behavior depends on the `fmt` version; see the
|
|
[FAQ entry](../home/faq.md#using-json-values-with-stdformat-or-fmt) for the details and a recipe for a full
|
|
`fmt::formatter` specialization.
|
|
|
|
## Serializing to other formats
|
|
|
|
Besides JSON text, a value can also be serialized to the more compact [binary formats](binary_formats/index.md)
|
|
(BJData, BON8, BSON, CBOR, MessagePack, UBJSON).
|
|
|
|
## See also
|
|
|
|
- [`dump`](../api/basic_json/dump.md) - serialize to a JSON-formatted string
|
|
- [`operator<<`](../api/operator_ltlt.md) - serialize to a stream
|
|
- [`to_string`](../api/basic_json/to_string.md) - user-defined-conversion helper
|
|
- [`std::formatter<basic_json>`](../api/basic_json/std_formatter.md) - use JSON values with `std::format` and `std::print`
|
|
- [`format_as`](../api/basic_json/format_as.md) - use JSON values with the {fmt} library
|
|
- [Parsing](parsing/index.md) - the reverse operation
|
|
- [`error_handler_t`](../api/basic_json/error_handler_t.md) - how to treat invalid UTF-8 when dumping
|
|
- [`basic_json_view::dump`](../api/basic_json_view/dump.md) - serialize a view directly
|
|
- [`JSON_NO_IO`](../api/macros/json_no_io.md) - remove stream and file I/O
|
|
- [`JSON_NO_SIMD`](../api/macros/json_no_simd.md) - disable SIMD acceleration
|