Skip to content

Commit 30847d5

Browse files
committed
docs: bump version number and update documentation
1 parent 3b22672 commit 30847d5

File tree

10 files changed

+170
-14
lines changed

10 files changed

+170
-14
lines changed

example/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ environment:
77
resolution: workspace
88

99
dependencies:
10-
json_annotation: ^4.9.0
10+
json_annotation: ^4.10.0
1111

1212
dev_dependencies:
1313
# Used by tests. Not required to use `json_serializable`.
@@ -21,7 +21,7 @@ dev_dependencies:
2121
build_verify: ^3.0.0
2222

2323
# REQUIRED!
24-
json_serializable: ^6.8.0
24+
json_serializable: ^6.10.0
2525

2626
# Not required to use `json_serializable`.
2727
path: ^1.8.0

json_annotation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.10.0
2+
3+
- Add `JsonSerializable.unionRename`
4+
- Add `JsonSerializable.unionDiscriminator`
5+
16
## 4.9.1-wip
27

38
- Require Dart 3.8

json_annotation/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_annotation
2-
version: 4.9.1-wip
2+
version: 4.10.0
33
description: >-
44
Classes and helper functions that support JSON code generation via the
55
`json_serializable` package.

json_serializable/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 6.10.0
2+
3+
- Add support for deserializing union json to sealed class
4+
- Add support for serializing sealed class to union json
5+
16
## 6.9.6-wip
27

38
- Move `package:collection` to a dev dependency.

json_serializable/README.md

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,53 @@ customize the encoding/decoding of any type, you have a few options.
253253
}
254254
```
255255
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+
256303
# Build configuration
257304

258305
Aside from setting arguments on the associated annotation classes, you can also
@@ -309,15 +356,17 @@ targets:
309356
[`Enum`]: https://api.dart.dev/dart-core/Enum-class.html
310357
[`int`]: https://api.dart.dev/dart-core/int-class.html
311358
[`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
321370
[`List`]: https://api.dart.dev/dart-core/List-class.html
322371
[`Map`]: https://api.dart.dev/dart-core/Map-class.html
323372
[`num`]: https://api.dart.dev/dart-core/num-class.html
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
part 'sealed_example.g.dart';
4+
5+
@JsonSerializable()
6+
sealed class SealedBase {
7+
const SealedBase();
8+
9+
factory SealedBase.fromJson(Map<String, dynamic> json) =>
10+
_$SealedBaseFromJson(json);
11+
12+
Map<String, dynamic> toJson() => _$SealedBaseToJson(this);
13+
}
14+
15+
@JsonSerializable()
16+
class SealedSub1 extends SealedBase {
17+
final String exampleField1;
18+
19+
SealedSub1({
20+
required this.exampleField1,
21+
});
22+
}
23+
24+
@JsonSerializable()
25+
class SealedSub2 extends SealedBase {
26+
final String exampleField2;
27+
28+
SealedSub2({
29+
required this.exampleField2,
30+
});
31+
}

json_serializable/example/sealed_example.g.dart

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 6.9.6-wip
2+
version: 6.10.0
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.
@@ -23,7 +23,7 @@ dependencies:
2323

2424
# Use a tight version constraint to ensure that a constraint on
2525
# `json_annotation` properly constrains all features it provides.
26-
json_annotation: '>=4.9.0 <4.10.0'
26+
json_annotation: '>=4.10.0 <4.11.0'
2727
meta: ^1.14.0
2828
path: ^1.9.0
2929
pub_semver: ^2.1.4

json_serializable/tool/readme/readme_template.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ customize the encoding/decoding of any type, you have a few options.
121121

122122
<!-- REPLACE tool/readme/readme_examples.dart-json_converter -->
123123

124+
# Sealed classes
125+
126+
As of `package:json_serializable` version 6.10.0 and `package:json_annotation`
127+
version 4.10.0, sealed classes can be serialized to json unions and json unions
128+
can be deserialized to sealed classes.
129+
130+
To achieve this, both the sealed class and its subclasses should be annotated
131+
with `JsonSerializable`. Only the sealed class should have `fromJson` factory
132+
or `toJson` function. To customize the sealed class behavior, use the fields
133+
`unionRename` and `unionDiscriminator` in `JsonSerializable` or adjust the
134+
default behavior by changing the corresponding fields in `build.yaml`. For
135+
more complex examples, please see [example]:
136+
137+
<!-- REPLACE example/sealed_example.dart -->
138+
124139
# Build configuration
125140

126141
Aside from setting arguments on the associated annotation classes, you can also

json_serializable/tool/readme_builder.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class _ReadmeBuilder extends Builder {
2626
...await buildStep.getExampleContent('example/example.dart'),
2727
...await buildStep.getExampleContent('example/example.g.dart'),
2828
...await buildStep.getExampleContent('tool/readme/readme_examples.dart'),
29+
...await buildStep.getExampleContent('example/sealed_example.dart'),
2930
'supported_types': _classCleanAndSort(supportedTypes()),
3031
'collection_types': _classCleanAndSort(collectionTypes()),
3132
'map_key_types': _classCleanAndSort(mapKeyTypes),

0 commit comments

Comments
 (0)