google / google/json_serializable.dart

genericArgumentFactories: support any variable name for fromJsonT/toJsonT

Open
#870 7 comments 9 reactions 0 assignees View on GitHub
State: help wanted Type: enhancement
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

Currently, it appears that `genericArgumentFactories` works only if the mappers use a very specific name format

Consider the following example:

```dart
@JsonSerializable(genericArgumentFactories: true)
class Either {
Either(this.first, this.second);

factory Either.fromJson(
Map json,
First Function(Object?) fromJsonFirst,
Second Function(Object?) fromJsonSecond,
) {
throw UnimplementedError();
}

First first;
Second second;
}

@JsonSerializable()
class Another {
Another(this.value);

factory Another.fromJson(Map json) =>
_$AnotherFromJson(json);

final Either value;
}
```

If we renamed `fromJsonFirst` to anything else, then json_serializable would fail with:

```
Expecting a `fromJson` constructor with exactly one positional parameter. The only extra parameters allowed are functions of the form `T Function(Object?) fromJsonT` where `T` is a type parameter of the target type.
package:generic_argument_factory_integration/models.dart:46:18

46 │ factory Either.fromJson(
```

It would be great if we could use any other variable name instead, such as:

```dart
factory Either.fromJson(
Map json,
First Function(Object?) first,
Second Function(Object?) second,
) {
throw UnimplementedError();
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.