-
Notifications
You must be signed in to change notification settings - Fork 421
Open
Labels
Description
Thank you for this package!
I failed to find any discussions about this and it feels like essential feture at this moment.
Do you have any plan to implement union-like sealed class serialization, so we can avoid using freezed?
We could use this feature something like this (inspired by freezed):
@JsonSerializable(unionKey: 'subtype') // dafualt 'runtimeType'
sealed class RootSealedClass {
const RootSealedClass();
factory RootSealedClass.fromJson(Map<String, dynamic> json) =>
_$RootSealedClassFromJson(json);
@JsonValue('first') // or literal class name as default
const factory RootSealedClass.first(String someAtribute) =
FirstSealedClassSubtype;
@JsonValue('second') // or literal class name as default
const factory RootSealedClass.second(int someOtherAtribute) =
SecondSealedClassSubtype;
Map<String, dynamic> toJson();
}
@JsonSerializable()
class FirstSealedClassSubtype extends RootSealedClass {
const FirstSealedClassSubtype(this.someAtribute);
factory FirstSealedClassSubtype.fromJson(Map<String, dynamic> json) =>
_$FirstSealedClassSubtypeFromJson(json);
final String someAtribute;
@override
Map<String, dynamic> toJson() => _$FirstSealedClassSubtypeToJson(this);
}
@JsonSerializable()
class SecondSealedClassSubtype extends RootSealedClass {
const SecondSealedClassSubtype(this.someOtherAtribute);
factory SecondSealedClassSubtype.fromJson(Map<String, dynamic> json) =>
_$SecondSealedClassSubtypeFromJson(json);
final int someOtherAtribute;
@override
Map<String, dynamic> toJson() => _$SecondSealedClassSubtypeToJson(this);
}
mateusfccp, tomwyr, SharbelOkzan, micycle8778, petrnymsa and 27 morek-paxian, tomwyr, erksch, yuruyuri16, ISL270 and 2 more