reflection-traits.md
August 7, 2026 ยท View on GitHub
Reflection traits
jsoncons supports two kinds of reflection traits:
-
json variant traits
-
streaming traits
json variant traits
The json variant traits define a compile time template based interface for conversion between a basic_json value
and a data structure of type T. They are used in basic_json template constructors,
basic_json::is<T>, basic_json::as<T>, and basic_json::try_as<T> with the requirement that type T have
json_traits defined.
jsoncons::reflect::json_traits
Since 1.4.0, jsoncons defines an interface for traits that support non-throwing conversions and uses-allocator construction. Until 1.9.0, these were named json_conv_traits, since 1.9.0, they have been renamed to json_traits. For backwards compatibility, the old name is aliased to the new name.
json_traits defaults to the legacy json_type_traits if a type conversion is undefined,
these traits do not support non-throwing conversions and uses-allocator construction.
See Eigen::Matrix example for an example of specializing json_traits for an Eigen matrix class.
See User-allocator construction example for an example that illustrates uses-allocator construction for types with json_traits defined.
Legacy jsoncons::json_type_traits
json_type_traits defines a compile time template based interface for conversion between a basic_json value
and a value of some other type.
See Eigen::Matrix example for an example of specializing json_type_traits for an Eigen matrix class.
Streaming traits
The decode streaming traits define a compile time template based interface for converting a stream of staj_events
into a C++ data structure. They are used in the decode_<format> functions such as decode_json.
The encode streaming traits define a compile time template based interface for converting
a C++ data structure into a character buffer or an output stream. They are used in the
encode_<format> functions such as encode_json.
In the case that the jsoncons decode and encode traits have no specialization for type T, they fall back on the json variant traits.
decode_traits
encode_traits
Convenience macros
jsoncons includes some convenience macros for generating reflection traits classes. Until 1.4.0, these macros generated json_type_traits class templates. Since 1.4.0, they generate json_traits class templates, as well as some additional traits that support streaming.