Json.move_if
template<typename T, typename D = Nul>
requires convertible<Json, T> || convertible_map<Json, T, D> || convertible_array<Json, T, D>
std::optional<T> move_if( D default_range_elem = D{} ) noexcept;
Attempts to move-convert internal data to type T
, returning std::nullopt
on failure.
Extensive compile-time checks eliminate incompatible conversions during compilation.
Runtime conversion may still fail due to JSON's dynamic typing nature.
Warning: Successful move operations will leave the source object empty (set to Nul), even if the result isn't used.
Recommended practice: Reset source object after moving.
Move Conversion Rules
- Nul → Nul
- Obj → Obj (moved)
- Arr → Arr (moved)
- Str → Str (moved)
- Bol → Bol (copied)
- Num → Enum (rounded to nearest integer)
- Num → Integer types (rounded)
- Num → Floating-point
- Any → Json-constructible types (move preferred)
- Obj → Implicitly convertible (move preferred)
- Arr → Implicitly convertible (move preferred)
- Str → Implicitly convertible (move preferred)
- Num → Implicitly convertible
- Bol → Implicitly convertible
- Nul → Implicitly convertible (except bool)
- Obj → Convertible key-value types (element-wise, move preferred)
- Arr → Convertible container types (element-wise, move preferred)
- Returns
std::nullopt
Parameters
T
: Target typedefault_range_elem
: Fallback for failed element conversions (array/object only)
Exception Safety
No-throw guarantee.
Complexity
- O(1) for movable types
- O(n) for element-wise conversions
Key characteristics: - Never throws for type mismatches (uses nullopt) - Source object becomes Nul after move - Preserves move semantics optimization opportunities
Version
Since v3.0.0 .