Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions example/lib/complex_sealed_class_examples.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:json_annotation/json_annotation.dart';

part 'complex_sealed_class_examples.g.dart';

@JsonSerializable(unionDiscriminator: 'organization')
sealed class Organization {
final String name;

Organization({required this.name});

factory Organization.fromJson(Map<String, dynamic> json) =>
_$OrganizationFromJson(json);

Map<String, dynamic> toJson() => _$OrganizationToJson(this);
}

@JsonSerializable(unionDiscriminator: 'department')
sealed class Department extends Organization {
final String departmentHead;

Department({required this.departmentHead, required super.name});

factory Department.fromJson(Map<String, dynamic> json) =>
_$DepartmentFromJson(json);
}

@JsonSerializable()
class Team extends Department {
final String teamLead;

Team({
required this.teamLead,
required super.departmentHead,
required super.name,
});
}
52 changes: 52 additions & 0 deletions example/lib/complex_sealed_class_examples.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions example/lib/sealed_class_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:json_annotation/json_annotation.dart';

part 'sealed_class_example.g.dart';

@JsonSerializable(
unionDiscriminator: 'vehicle_type',
unionRename: FieldRename.snake,
)
sealed class Vehicle {
final String vehicleID;

Vehicle({required this.vehicleID});

factory Vehicle.fromJson(Map<String, dynamic> json) =>
_$VehicleFromJson(json);

Map<String, dynamic> toJson() => _$VehicleToJson(this);
}

@JsonSerializable()
class Car extends Vehicle {
final int numberOfDoors;

Car({required this.numberOfDoors, required super.vehicleID});
}

@JsonSerializable()
class Bicycle extends Vehicle {
final bool hasBell;

Bicycle({required this.hasBell, required super.vehicleID});
}
46 changes: 46 additions & 0 deletions example/lib/sealed_class_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ environment:
resolution: workspace

dependencies:
json_annotation: ^4.9.0
json_annotation: ^4.11.0-wip

dev_dependencies:
# Used by tests. Not required to use `json_serializable`.
Expand Down
6 changes: 5 additions & 1 deletion json_annotation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## 4.11.0
## 4.11.0-wip

- Add `JsonSerializable.dateTimeUtc` configuration option.
([#1371](https://github.com/google/json_serializable.dart/issues/1371))
- Add `JsonSerializable.unionRename` and `JsonSerializable.unionDiscriminator`
to support sealed class serialization.

## 4.10.0

Expand All @@ -10,6 +12,8 @@
(Huge thanks to [Chris Sells](https://sellsbrothers.com/) for the PR!)
- Require `meta: ^1.16.0`
- Require `sdk: ^3.9.0`
- Require Dart 3.8
- Deprecated `FieldRename` in favor of `RenameType`

## 4.9.0

Expand Down
18 changes: 18 additions & 0 deletions json_annotation/lib/src/allowed_keys_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ class UnrecognizedKeysException extends BadKeyException {
: super._(map);
}

/// Exception thrown if there is an unrecognized union type in a JSON map
/// that was provided during deserialization.
class UnrecognizedUnionTypeException extends BadKeyException {
/// The discriminator that was not recognized.
final String unrecognizedType;

/// The type of the union that was being deserialized.
final Type unionType;

@override
String get message =>
'Unrecognized type: $unrecognizedType '
'for union: $unionType.';

UnrecognizedUnionTypeException(this.unrecognizedType, this.unionType, Map map)
: super._(map);
}

/// Exception thrown if there are missing required keys in a JSON map that was
/// provided during deserialization.
class MissingRequiredKeysException extends BadKeyException {
Expand Down
22 changes: 22 additions & 0 deletions json_annotation/lib/src/json_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,20 @@ class JsonSerializable {
/// `includeIfNull`, that value takes precedent.
final bool? includeIfNull;

/// The discriminator key used to identify the union type.
///
/// Defaults to `type`.
final String? unionDiscriminator;

/// Defines the automatic naming strategy when converting class names
/// to union type names.
///
/// With a value [FieldRename.none] (the default), the name of the class is
/// used without modification.
///
/// See [FieldRename] for details on the other options.
final FieldRename? unionRename;

/// A list of [JsonConverter] to apply to this class.
///
/// Writing:
Expand Down Expand Up @@ -291,6 +305,8 @@ class JsonSerializable {
this.genericArgumentFactories,
this.createPerFieldToJson,
this.dateTimeUtc,
this.unionDiscriminator,
this.unionRename,
});

factory JsonSerializable.fromJson(Map<String, dynamic> json) =>
Expand All @@ -311,6 +327,9 @@ class JsonSerializable {
ignoreUnannotated: false,
includeIfNull: true,
genericArgumentFactories: false,
dateTimeUtc: false,
unionDiscriminator: 'type',
unionRename: FieldRename.none,
);

/// Returns a new [JsonSerializable] instance with fields equal to the
Expand All @@ -333,6 +352,9 @@ class JsonSerializable {
includeIfNull: includeIfNull ?? defaults.includeIfNull,
genericArgumentFactories:
genericArgumentFactories ?? defaults.genericArgumentFactories,
dateTimeUtc: dateTimeUtc ?? defaults.dateTimeUtc,
unionDiscriminator: unionDiscriminator ?? defaults.unionDiscriminator,
unionRename: unionRename ?? defaults.unionRename,
);

Map<String, dynamic> toJson() => _$JsonSerializableToJson(this);
Expand Down
20 changes: 17 additions & 3 deletions json_annotation/lib/src/json_serializable.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion json_annotation/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_annotation
version: 4.11.0
version: 4.11.0-wip
description: >-
Classes and helper functions that support JSON code generation via the
`json_serializable` package.
Expand Down
25 changes: 5 additions & 20 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 6.14.0-wip

- Add support for deserializing union json to sealed class
- Add support for serializing sealed class to union json

## 6.13.1

- Improve error message when generating code for `Record` types with unsupported
Expand All @@ -9,24 +14,6 @@
- Require `source_gen: ^4.1.2`
- Require `source_helper: ^1.3.10`

## 6.13.0

- Fix schema generation for many different types of fields.
([#1549](https://github.com/google/json_serializable.dart/issues/1549))
- Support `JsonSerializable.dateTimeUtc` configuration option.
([#1371](https://github.com/google/json_serializable.dart/issues/1371))
- Remove `json_schema` from `dependencies` (it is only used in tests).

## 6.12.0

- Support `JsonKey` annotation on constructor parameters.
- Add support for `JsonSerializable(createJsonSchema: true)`.
(Huge thanks to [Chris Sells](https://sellsbrothers.com/) for the PR!)

## 6.11.4

- Allow `analyzer: >=9.0.0 <11.0.0`

## 6.11.3

- Require `analyzer: ^9.0.0`
Expand Down Expand Up @@ -61,8 +48,6 @@

## 6.10.0

- Required `analyzer: ^7.4.0`.
- Switch to analyzer element2 model and `build: ^3.0.0-dev`.
- Move `package:collection` to a dev dependency.
- Use new `null-aware element` feature in generated code.
- Require Dart 3.8
Expand Down
Loading
Loading