genericArgumentFactories property

bool? genericArgumentFactories
final

When true on classes with type parameters (generic types), extra "helper" parameters will be generated for fromJson and/or toJson to support serializing values of those types.

For example, the generated code for

@JsonSerializable(genericArgumentFactories: true)
class Response<T> {
  int status;
  T value;
}

Looks like

Response<T> _$ResponseFromJson<T>(
  Map<String, dynamic> json,
  T Function(Object json) fromJsonT,
) {
  return Response<T>()
    ..status = (json['status'] as num).toInt()
    ..value = fromJsonT(json['value']);
}

Map<String, dynamic> _$ResponseToJson<T>(
  Response<T> instance,
  Object Function(T value) toJsonT,
) =>
    <String, dynamic>{
      'status': instance.status,
      'value': toJsonT(instance.value),
    };

Notes:

  1. This option has no effect on classes without type parameters. If used on such a class, a warning is echoed in the build log.
  2. If this option is set for all classes in a package via build.yaml it is only applied to classes with type parameters – so no warning is echoed.

Implementation

final bool? genericArgumentFactories;