Files
json/docs/mkdocs/docs/api/basic_json/json_serializer.md
T
Niels LohmannandClaude Opus 5 599bb1b68c docs: document the implicit requirements on basic_json's template parameters
The requirements that basic_json places on its eleven template parameters
were only implied by how the library uses the resulting object_t, array_t,
string_t, etc. Consumers had to discover them by trial and error.

Add "Template Parameter Requirements" collecting them, split into what is
always required and what is only required when a particular part of the API
is instantiated. Notable findings that were previously undocumented:

- ObjectType must provide a key_compare member type (actual_object_comparator
  names object_t::key_compare in both arms of a std::conditional), and its
  third template parameter is used as a comparator, so std::unordered_map
  cannot be used without a wrapper.
- ArrayType must provide capacity() -- push_back(), emplace_back(),
  operator+=(), and operator[](size_type) call it unconditionally -- and
  needs random-access iterators, so std::deque and std::list do not work.
- StringType needs contiguous, null-terminated data(), a one-byte value_type,
  and either assignability from std::to_string or an ADL int_to_string().
- NumberFloatType must be float, double, or long double for parsing and
  serialization; the integer types must satisfy std::is_integral.
- AllocatorType must be stateless, support incomplete types, and use plain
  pointers.
- BooleanType and the number types are union members and must be trivial.

Link the new page from the basic_json overview, the types feature page, and
the individual type alias pages, and correct the container examples given for
ObjectType (std::unordered_map) and ArrayType (std::list), which do not work.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018hxZxz8svM54c6ATEvXp5E
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-08-28 17:38:55 +00:00

1.2 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"
```

Version history

  • Since version 2.0.0.