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