decodeTyped<T> function

T decodeTyped<T>(
  1. dynamic raw, {
  2. TransferCodec<T>? codec,
  3. String? name,
})

Decodes a typed value from its transfer representation. (JSON)

codec can be used to override the default codec for type T. name is used in error messages for debugging purposes.

Implementation

T decodeTyped<T>(dynamic raw, {TransferCodec<T>? codec, String? name}) {
  if (raw is T) {
    return raw;
  }

  if (raw == null) {
    throw CodecException.typeMismatch(T, raw.runtimeType, name);
  }

  codec ??= TransferCodec.find<T>();
  if (codec != null) {
    return (codec as dynamic).decode(raw, name: name) as T;
  }

  throw ApiError.invalidType(T);
}