@@ -256,6 +256,53 @@ customize the encoding/decoding of any type, you have a few options.
256256 }
257257 ```
258258
259+ # Sealed classes
260+
261+ As of [`json_serializable`] version 6.10.0 and [`json_annotation`]
262+ version 4.10.0, sealed classes can be serialized to json unions and json unions
263+ can be deserialized to sealed classes.
264+
265+ To achieve this, both the sealed class and its subclasses should be annotated
266+ with `JsonSerializable`. Only the sealed class should have `fromJson` factory
267+ and `toJson` functions. To customize the sealed class behavior, use the fields
268+ `unionRename` and `unionDiscriminator` in `JsonSerializable` or adjust the
269+ default behavior by changing the corresponding fields in `build.yaml`. For
270+ more complex examples, please see [example]:
271+
272+ ```dart
273+ import 'package:json_annotation/json_annotation.dart';
274+
275+ part 'sealed_example.g.dart';
276+
277+ @JsonSerializable()
278+ sealed class SealedBase {
279+ const SealedBase();
280+
281+ factory SealedBase.fromJson(Map<String, dynamic> json) =>
282+ _$SealedBaseFromJson(json);
283+
284+ Map<String, dynamic> toJson() => _$SealedBaseToJson(this);
285+ }
286+
287+ @JsonSerializable()
288+ class SealedSub1 extends SealedBase {
289+ final String exampleField1;
290+
291+ SealedSub1({
292+ required this.exampleField1,
293+ });
294+ }
295+
296+ @JsonSerializable()
297+ class SealedSub2 extends SealedBase {
298+ final String exampleField2;
299+
300+ SealedSub2({
301+ required this.exampleField2,
302+ });
303+ }
304+ ```
305+
259306# Build configuration
260307
261308Aside from setting arguments on the associated annotation classes, you can also
@@ -312,15 +359,17 @@ targets:
312359[`Enum`] : https://api.dart.dev/dart-core/Enum-class.html
313360[`int`] : https://api.dart.dev/dart-core/int-class.html
314361[`Iterable`] : https://api.dart.dev/dart-core/Iterable-class.html
315- [`JsonConverter`] : https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
316- [`JsonEnum.valueField`] : https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonEnum/valueField.html
317- [`JsonEnum`] : https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonEnum-class.html
318- [`JsonKey.fromJson`] : https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
319- [`JsonKey.toJson`] : https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
320- [`JsonKey`] : https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey-class.html
321- [`JsonLiteral`] : https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonLiteral-class.html
322- [`JsonSerializable`] : https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable-class.html
323- [`JsonValue`] : https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonValue-class.html
362+ [`json_annotation`] : https://unknown.com/package/json_annotation
363+ [`json_serializable`] : https://unknown.com/package/json_serializable
364+ [`JsonConverter`] : https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonConverter-class.html
365+ [`JsonEnum.valueField`] : https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonEnum/valueField.html
366+ [`JsonEnum`] : https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonEnum-class.html
367+ [`JsonKey.fromJson`] : https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonKey/fromJson.html
368+ [`JsonKey.toJson`] : https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonKey/toJson.html
369+ [`JsonKey`] : https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonKey-class.html
370+ [`JsonLiteral`] : https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonLiteral-class.html
371+ [`JsonSerializable`] : https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonSerializable-class.html
372+ [`JsonValue`] : https://pub.dev/documentation/json_annotation/4.10.0/json_annotation/JsonValue-class.html
324373[`List`] : https://api.dart.dev/dart-core/List-class.html
325374[`Map`] : https://api.dart.dev/dart-core/Map-class.html
326375[`num`] : https://api.dart.dev/dart-core/num-class.html
0 commit comments