readAs<T> method
Reads a value of type T
from value
.
Implementation
T readAs<T>(dynamic value, DogEngine engine, [T? defaultValue]) {
if (value == null) {
if (defaultValue != null) return defaultValue;
throw DogSerializerException(message: "Value is null", converter: this);
}
if (value is T) return value;
if (engine.codec.isNative(T)) {
try {
return engine.codec.primitiveCoercion.coerce(TypeToken<T>(), value, null);
} catch (e) {
throw DogSerializerException(
message: "Failed to coerce value to type $T: $e",
converter: this,
cause: e,
);
}
}
try {
return engine.fromNative<T>(value);
} catch (e) {
throw DogSerializerException(
message: "Failed to deserialize value to type $T: $e",
converter: this,
cause: e,
);
}
}