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) {
  if (json == null) {
    if (json is T) {
      return json;
    }
  }
  if (json is Map) {
    final type = json[jsonDiscriminator];
    if (type is! String) {
      if (json.containsKey(jsonDiscriminator)) {
        throw ArgumentError(
          'JSON object field "$jsonDiscriminator" has invalid value of type ${type.runtimeType}.',
        );
      } else {
        throw ArgumentError(
          'JSON object is missing discriminator "$jsonDiscriminator".',
        );
      }
    }
    final kind = findKindByName(type);
    if (kind.isPrimitive && json.length == 2) {
      final value = json['value'];
      return kind.decodeJsonTree(value);
    }
    return kind.decodeJsonTree(json);
  } else {
    throw ArgumentError('Expected JSON object, got ${json.runtimeType}');
  }
}