Skip to content

[WIP] Extension methods support #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
12 changes: 0 additions & 12 deletions example/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class Person {
Person(this.firstName, this.lastName, this.dateOfBirth,
{this.middleName, this.lastOrder, List<Order> orders})
: orders = orders ?? <Order>[];

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

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

@JsonSerializable(includeIfNull: false)
Expand All @@ -49,10 +45,6 @@ class Order {

Order(this.date);

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

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

static Duration _durationFromMilliseconds(int milliseconds) =>
milliseconds == null ? null : Duration(milliseconds: milliseconds);

Expand All @@ -73,10 +65,6 @@ class Item {
bool isRushed;

Item();

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

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

@JsonLiteral('data.json')
Expand Down
19 changes: 17 additions & 2 deletions example/lib/example.g.dart

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

12 changes: 12 additions & 0 deletions example/lib/json_converter_example.g.dart

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

2 changes: 2 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ dev_dependencies:
test: ^1.6.0

dependency_overrides:
json_annotation:
path: ../json_annotation
json_serializable:
path: ../json_serializable
2 changes: 1 addition & 1 deletion example/test/example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() {
final personJson = _encode(person);

final person2 =
Person.fromJson(json.decode(personJson) as Map<String, dynamic>);
PersonExt.fromJson(json.decode(personJson) as Map<String, dynamic>);

expect(person.firstName, person2.firstName);
expect(person.lastName, person2.lastName);
Expand Down
4 changes: 4 additions & 0 deletions json_annotation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0.2

- Added extension to class to remove boilerplace code

## 3.0.1

- Require at least Dart `2.6.0`.
Expand Down
28 changes: 28 additions & 0 deletions json_annotation/lib/src/json_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ class JsonSerializable {
/// [CheckedFromJsonException] is thrown.
final bool checked;

/// If `Ext` (the default), a named extension `ExampleExt` is created
/// in the generated part file.
///
/// ```dart
/// extension ExampleExt on Example {
/// }
/// ```
final String extensionSuffix;

/// If `true` (the default), a named extension `ExampleExt` is created
/// in the generated part file.
///
/// ```dart
/// extension ExampleExt on Example {
/// Example static fromJson(Map<String, dynamic> json) =>
/// _$ExampleFromJson(json);
///
/// Map<String, dynamic> toJson() => _$ExampleToJson(this);
/// }
/// ```
final bool createExtension;

/// If `true` (the default), a private, static `_$ExampleFromJson` method
/// is created in the generated part file.
///
Expand Down Expand Up @@ -143,6 +165,8 @@ class JsonSerializable {
const JsonSerializable({
this.anyMap,
this.checked,
this.createExtension,
this.extensionSuffix,
this.createFactory,
this.createToJson,
this.disallowUnrecognizedKeys,
Expand All @@ -161,6 +185,8 @@ class JsonSerializable {
static const defaults = JsonSerializable(
anyMap: false,
checked: false,
createExtension: true,
extensionSuffix: 'Ext',
createFactory: true,
createToJson: true,
disallowUnrecognizedKeys: false,
Expand All @@ -179,6 +205,8 @@ class JsonSerializable {
JsonSerializable withDefaults() => JsonSerializable(
anyMap: anyMap ?? defaults.anyMap,
checked: checked ?? defaults.checked,
createExtension: createExtension ?? defaults.createExtension,
extensionSuffix: extensionSuffix ?? defaults.extensionSuffix,
createFactory: createFactory ?? defaults.createFactory,
createToJson: createToJson ?? defaults.createToJson,
disallowUnrecognizedKeys:
Expand Down
43 changes: 30 additions & 13 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: 3.0.1
version: 3.0.2
description: >-
Classes and helper functions that support JSON code generation via the
`json_serializable` package.
Expand Down
4 changes: 4 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.4.1-dev

- Generated extension code to remove boilerplate code

## 3.4.0-dev

- Added support for `double` constants as default values.
Expand Down
42 changes: 22 additions & 20 deletions json_serializable/doc/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
| -------------------------- | ------------------------------------------- | --------------------------- |
| any_map | [JsonSerializable.anyMap] | |
| checked | [JsonSerializable.checked] | |
| create_extension | [JsonSerializable.createExtension] | |
| create_factory | [JsonSerializable.createFactory] | |
| create_to_json | [JsonSerializable.createToJson] | |
| disallow_unrecognized_keys | [JsonSerializable.disallowUnrecognizedKeys] | |
Expand All @@ -19,23 +20,24 @@
| | | [JsonKey.toJson] |
| | | [JsonKey.unknownEnumValue] |

[JsonSerializable.anyMap]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/anyMap.html
[JsonSerializable.checked]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/checked.html
[JsonSerializable.createFactory]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/createFactory.html
[JsonSerializable.createToJson]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/createToJson.html
[JsonSerializable.disallowUnrecognizedKeys]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/disallowUnrecognizedKeys.html
[JsonSerializable.explicitToJson]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/explicitToJson.html
[JsonSerializable.fieldRename]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/fieldRename.html
[JsonSerializable.ignoreUnannotated]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/ignoreUnannotated.html
[JsonSerializable.includeIfNull]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/includeIfNull.html
[JsonKey.includeIfNull]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/includeIfNull.html
[JsonSerializable.nullable]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonSerializable/nullable.html
[JsonKey.nullable]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/nullable.html
[JsonKey.defaultValue]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/defaultValue.html
[JsonKey.disallowNullValue]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/disallowNullValue.html
[JsonKey.fromJson]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/fromJson.html
[JsonKey.ignore]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/ignore.html
[JsonKey.name]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/name.html
[JsonKey.required]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/required.html
[JsonKey.toJson]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/toJson.html
[JsonKey.unknownEnumValue]: https://pub.dev/documentation/json_annotation/3.0.1/json_annotation/JsonKey/unknownEnumValue.html
[JsonSerializable.anyMap]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/anyMap.html
[JsonSerializable.checked]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/checked.html
[JsonSerializable.createExtension]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/createExtension.html
[JsonSerializable.createFactory]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/createFactory.html
[JsonSerializable.createToJson]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/createToJson.html
[JsonSerializable.disallowUnrecognizedKeys]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/disallowUnrecognizedKeys.html
[JsonSerializable.explicitToJson]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/explicitToJson.html
[JsonSerializable.fieldRename]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/fieldRename.html
[JsonSerializable.ignoreUnannotated]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/ignoreUnannotated.html
[JsonSerializable.includeIfNull]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/includeIfNull.html
[JsonKey.includeIfNull]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/includeIfNull.html
[JsonSerializable.nullable]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonSerializable/nullable.html
[JsonKey.nullable]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/nullable.html
[JsonKey.defaultValue]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/defaultValue.html
[JsonKey.disallowNullValue]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/disallowNullValue.html
[JsonKey.fromJson]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/fromJson.html
[JsonKey.ignore]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/ignore.html
[JsonKey.name]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/name.html
[JsonKey.required]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/required.html
[JsonKey.toJson]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/toJson.html
[JsonKey.unknownEnumValue]: https://pub.dev/documentation/json_annotation/3.0.2/json_annotation/JsonKey/unknownEnumValue.html
2 changes: 0 additions & 2 deletions json_serializable/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ class Person {
final String lastName;
final DateTime dateOfBirth;
Person({this.firstName, this.lastName, this.dateOfBirth});
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
}
5 changes: 5 additions & 0 deletions json_serializable/example/example.g.dart

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

18 changes: 18 additions & 0 deletions json_serializable/lib/src/extension_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import './helper_core.dart';

abstract class ExtensionHelper implements HelperCore {
Iterable<String> createExtension() sync* {
assert(config.createExtension);
final buffer = StringBuffer()
..writeln('extension $extensionName on $className { '
'static $className fromJson(Map<String, dynamic> json) => ${prefix}FromJson(json); '
'Map<String, dynamic> toJson() => ${prefix}ToJson(this); '
'}');

yield buffer.toString();
}

String get className => element.name;

String get extensionName => '$className${config.extensionSuffix}';
}
Loading