decodeJsonTree method

  1. @override
T decodeJsonTree(
  1. Object? json
)
override

Converts json (any JSON tree) to an instance of T.

Throws ArgumentError if the JSON does not match.

Implementation

@override
T decodeJsonTree(Object? json) {
  final fromJson = _fromJson;
  if (fromJson != null) {
    if (fromJson is T Function(Object? json)) {
      return fromJson(json);
    }
    if (fromJson is T Function(Map<String, dynamic> json)) {
      if (json is! Map<String, dynamic>) {
        throw ArgumentError.value(json, 'json');
      }
      return fromJson(json);
    }
    if (fromJson is T Function(String json)) {
      if (json is! String) {
        throw ArgumentError.value(json, 'json');
      }
      return fromJson(json);
    }
    throw StateError('Invalid JSON function: $fromJson');
  }
  if (json is! Map) {
    throw JsonDecodingError.expectedObject(json);
  }
  final mapper = JsonObjectDecodingMapper(json);
  return map(mapper, newInstance());
}