google / json_serializable.dart

Generates utilities to aid in serializing to/from JSON.
https://pub.dev/packages/json_serializable
BSD 3-Clause "New" or "Revised" License
1.54k stars 393 forks source link

Default value for record fields #1340

Open Johannes5000 opened 12 months ago

Johannes5000 commented 12 months ago

Since Version 6.7.0 Records are supported.

If I am right, there is currently no possibility to set a default value for a field inside the record.

For example:

@JsonSerializable()
class MyClass {
  final ({
    double numberWithDefault,
    String someOtherText,
  }) myRecord;

  @JsonKey(defaultValue: 0.0)
  final double anotherNumberWithDefault;

  MyClass({
    required this.myRecord,
    required this.anotherNumberWithDefault,
  });

  factory MyClass.fromJson(Map<String, dynamic> json) => _$MyClassFromJson(json);
  Map<String, dynamic> toJson() => _$MyClassToJson(this);
}

It would be nice to be able to set numberWithDefault to a default value, if the json value is null (just like anotherNumberWithDefault).