-
Notifications
You must be signed in to change notification settings - Fork 425
Closed
Description
Starting with Dart 2.6, json_serializable
has the option to create the toJson()
method for the user. Although extension methods don't support constructors, json_serializable
could create a static generative method instead. This means that user code would look like this:
import 'package:json_annotation/json_annotation.dart';
part 'example.g.dart';
@JsonSerializable(nullable: false)
class Person {
final String firstName;
final String lastName;
final DateTime dateOfBirth;
Person({this.firstName, this.lastName, this.dateOfBirth});
}
The generated extension might look like this:
// in example.g.dart
extension on Person {
static Person fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
}
// ...
Rest of the code can keep the same.
trymnilsen, stargazing-dino, nerder, bsutton, jyardin and 61 moretrymnilsen, nerder, comerc, vanlooverenkoen, vitorTheDev and 6 morek-paxian and felipe-fontesmikuhl-dev and zhongwen-unnoo