Files
json/docs/mkdocs/docs/api/basic_json/json_serializer.md
T
Niels Lohmann 52b72b5684 Improve documentation: byte layouts, release pages, See also, dark-mode images (#5906)
- Make the iterator range illustrations legible in dark mode (prefers-color-scheme
  rules inside the SVGs).
- Add a "Byte layout" section with a color-coded byte table to each binary format
  page (BJData, BON8, BSON, CBOR, MessagePack, UBJSON), and highlight the smallest
  size per file in the comparison table.
- Add "See also" sections to the overview pages that lacked one.
- Split the release notes into one page per release, grouped by minor version on
  the overview page, with an "API changes" section per release generated from the
  API pages' version histories (hooks/api_changes.py).
- Fact-check the release notes and version histories against the release tags and
  correct wrong or misleading entries (e.g., json_serializer 2.1.0, merge_patch
  3.1.0, items, count, operator[], to_bjdata, std::string_view key support).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-24 17:57:44 +02:00

1.6 KiB

nlohmann::basic_json::json_serializer

template<typename T, typename SFINAE>
using json_serializer = JSONSerializer<T, SFINAE>;

Template parameters

T
type to convert; will be used in the to_json/from_json functions
SFINAE
type to add compile type checks via SFINAE; usually #!cpp void

Notes

Default type

The default values for json_serializer is adl_serializer.

Requirements

A custom serializer must provide #!cpp static void to_json(basic_json&, T) for every type it serializes, and either #!cpp static void from_json(const basic_json&, T&) or #!cpp static T from_json(const basic_json&) for every type it deserializes. See Template Parameter Requirements.

Examples

??? example

The example below shows how a conversion of a non-default-constructible type is implemented via a specialization of
the `adl_serializer`.
    
```cpp
--8<-- "examples/from_json__non_default_constructible.cpp"
```

Output:

```json
--8<-- "examples/from_json__non_default_constructible.output"
```

See also

  • adl_serializer the default json_serializer
  • get explicit type conversion using the json_serializer's from_json() method
  • get_to explicit type conversion into a variable using the json_serializer's from_json() method
  • Arbitrary Type Conversions - the article on converting between JSON values and arbitrary types

Version history

  • Added in version 2.1.0.