Json.to
template<typename T, typename D = Nul>
requires convertible<Json, T> || convertible_map<Json, T, D> || convertible_array<Json, T, D>
T to( D default_range_elem = D{} ) const;
Performs a type conversion (copy) of internal data to type T
, throwing an exception if conversion fails.
Extensive compile-time type checking is performed - incompatible branches are eliminated during compilation.
However, conversion may still fail at runtime due to JSON's dynamic typing nature.
Conversion Rules
- Nul → Nul
- Obj → Obj
- Arr → Arr
- Str → Str
- Bol → Bol
- Num → Enum (rounded to nearest integer)
- Num → Integer types (rounded)
- Num → Floating-point types
- Any → Types with Json constructors
- Obj → Implicitly convertible types
- Arr → Implicitly convertible types
- Str → Implicitly convertible types
- Num → Implicitly convertible types
- Bol → Implicitly convertible types
- Nul → Implicitly convertible types (except bool)
- Obj → Convertible key-value types (element-wise)
- Arr → Convertible container types (element-wise)
- Throws std::runtime_error
Parameters
T
: Target typedefault_range_elem
: Fallback value for failed element conversions (array/object only)
Exception Safety
Throws on conversion failure
Complexity
Linear time O(n) (full copy required)
Version
Since v3.0.0 .