fromEJson<T> function

T fromEJson<T>(
  1. EJsonValue? ejson, {
  2. T? defaultValue,
})

Converts ejson to type T.

defaultValue is returned if set, and ejson is null.

Throws InvalidEJson if ejson is not valid for T. Throws MissingDecoder if no decoder is registered for T.

Implementation

T fromEJson<T>(EJsonValue ejson, {T? defaultValue}) {
  final type = T;
  final nullable = type.isNullable;
  if (ejson == null && defaultValue != null) return defaultValue;
  final decoder = nullable ? _decodeNullable : _decoders[type.base];
  if (decoder == null) {
    throw MissingDecoder._(ejson, type);
  }
  final args = nullable ? [type.nonNull] : type.args;
  if (args.isEmpty) {
    return decoder(ejson) as T; // minor optimization
  }
  return decoder.callWith(typeArguments: args, parameters: [ejson]) as T;
}