deserialize<T> method

T deserialize<T>(
  1. dynamic data, [
  2. Type? t
])

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 (t == int || t == getType<int?>()) {
    return data;
  } else if (t == double || t == getType<double?>()) {
    return (data as num?)?.toDouble() as T;
  } else if (t == String || t == getType<String?>()) {
    return data;
  } else if (t == bool || t == getType<bool?>()) {
    return data;
  } else if (t == DateTime) {
    return DateTime.parse(data).toUtc() as T;
  } else if (t == getType<DateTime?>()) {
    return DateTime.tryParse(data ?? '')?.toUtc() as T;
  } else if (t == ByteData) {
    return (data as String).base64DecodedByteData()! as T;
  } else if (t == getType<ByteData?>()) {
    return (data as String?)?.base64DecodedByteData() as T;
  } else if (t == Duration) {
    return Duration(milliseconds: (data as int)) as T;
  } else if (t == getType<Duration?>()) {
    return data == null ? data : Duration(milliseconds: (data as int)) as T;
  }
  throw FormatException('No deserialization found for type $t');
}