Skip to content

Commit fbbb160

Browse files
committed
docs: bump version number and update documentation
1 parent 048fb3d commit fbbb160

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.6

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.4
27

38
- Fix extra line being generated when targetting Dart 3.7 package.

json_serializable/README.md

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

261308
Aside 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
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.4
2+
version: 6.10.0
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.
@@ -24,7 +24,7 @@ dependencies:
2424

2525
# Use a tight version constraint to ensure that a constraint on
2626
# `json_annotation` properly constrains all features it provides.
27-
json_annotation: '>=4.9.0 <4.10.0'
27+
json_annotation: '>=4.10.0 <4.11.0'
2828
meta: ^1.14.0
2929
path: ^1.9.0
3030
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
@@ -25,6 +25,7 @@ class _ReadmeBuilder extends Builder {
2525
...await buildStep.getExampleContent('example/example.dart'),
2626
...await buildStep.getExampleContent('example/example.g.dart'),
2727
...await buildStep.getExampleContent('tool/readme/readme_examples.dart'),
28+
...await buildStep.getExampleContent('example/sealed_example.dart'),
2829
'supported_types': _classCleanAndSort(supportedTypes()),
2930
'collection_types': _classCleanAndSort(collectionTypes()),
3031
'map_key_types': _classCleanAndSort(mapKeyTypes),

0 commit comments

Comments
 (0)