
use type

@ignore_access

+ fn to_type[T](data: Value) T {

    #if is_nullable_type(T)
    if data.is_null() : return null
    #end

    #if type_has_method(T, from_json_value)
    return T.from_json_value(data)
    #elif type_has_method(T, from_json_value_auto)
    return T.from_json_value_auto[Value](data)
    #elif is_integer_type(T)
    return data.int().@cast(T)
    #elif is_float_type(T)
    return data.float().@cast(T)
    #elif is_bool_type(T)
    return data.bool().@cast(T)
    #elif !is_structural_type(T)
    #print_type(T)
    #error "Cannot convert json to this value type"
    #end

    // Structural
    let result = T {
        #loop properties T as prop
        prop: to_type[prop](data.get(prop.name))
        #end // End loop
    }
    return result
}
