deserialize<T> method
Deserialize the provided json data
to an object of type t
or T
.
Implementation
T deserialize<T>(dynamic data, [Type? t]) {
t ??= T;
//TODO: all the "dart native" types should be listed here
if (_isNullableType<int>(t)) {
return data;
} else if (_isNullableType<double>(t)) {
return (data as num?)?.toDouble() as T;
} else if (_isNullableType<String>(t)) {
return data;
} else if (_isNullableType<bool>(t)) {
return data;
} else if (_isNullableType<DateTime>(t)) {
if (data is DateTime) return data as T;
if (data == null) return null as T;
return DateTime.tryParse(data)?.toUtc() as T;
} else if (_isNullableType<ByteData>(t)) {
if (data is Uint8List) {
var byteData = ByteData.view(
data.buffer,
data.offsetInBytes,
data.lengthInBytes,
);
return byteData as T;
} else {
return (data as String?)?.base64DecodedByteData() as T;
}
} else if (_isNullableType<Duration>(t)) {
return data == null ? data : Duration(milliseconds: (data as int)) as T;
} else if (_isNullableType<UuidValue>(t)) {
return (data == null ? null : UuidValue.fromString(data as String)) as T;
}
throw FormatException('No deserialization found for type $t');
}