Skip to content

Prepare for v0.2 release #27

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

Merged
merged 1 commit into from
Jul 25, 2017
Merged
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
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

* **BREAKING** Types are now segmented into their own libraries.

* `package:json_serializable/generators.dart` contains the `Generator`
* `package:json_serializable/generators.dart` contains `Generator`
implementations.

* `package:json_serializable/annotations.dart` contains the annotations.
* `package:json_serializable/annotations.dart` contains annotations.
This library should be imported with your target classes.

* `package:json_serializable/type_helpers.dart` contains `TypeHelper` classes
which allows custom generation for specific types.
and related helpers which allow custom generation for specific types.

* **BREAKING** Fail generation for types that are not a JSON primitive or that
* **BREAKING** Generation fails for types that are not a JSON primitive or that
do not explicitly supports JSON serialization.

* **BREAKING** `TypeHelper`:
Expand All @@ -20,7 +20,7 @@
type is not supported.

* Added `(de)serializeNested` arguments to `(de)serialize` methods allowing
generic types. This is (now) how support for `Iterable`, `List`, and `Map`
generic types. This is how support for `Iterable`, `List`, and `Map`
is implemented.

* **BREAKING** `JsonKey.jsonName` was renamed to `name` and is now a named
Expand Down
62 changes: 44 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,65 @@ part 'example.g.dart';

@JsonSerializable()
class Person extends Object with _$PersonSerializerMixin {
final String firstName, middleName, lastName;
final String firstName;
@JsonKey(includeIfNull: false)
final String middleName;
final String lastName;

@JsonKey('date-of-birth')
@JsonKey(name: 'date-of-birth', nullable: false)
final DateTime dateOfBirth;

Person(this.firstName, this.lastName, {this.middleName, this.dateOfBirth});
@JsonKey(name: 'last-order')
final DateTime lastOrder;

factory Person.fromJson(json) => _$PersonFromJson(json);
@JsonKey(nullable: false)
List<Order> orders;

Person(this.firstName, this.lastName, this.dateOfBirth,
{this.middleName, this.lastOrder, List<Order> orders})
: this.orders = orders ?? <Order>[];

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

`source_gen` creates the corresponding part `example.g.dart`:

```dart
part of json_serializable.example;

Person _$PersonFromJson(Map json) =>
new Person(json['firstName'] as String, json['lastName'] as String,
middleName: json['middleName'] as String,
dateOfBirth: json['date-of-birth'] == null
? null
: DateTime.parse(json['date-of-birth']));
Person _$PersonFromJson(Map<String, dynamic> json) => new Person(
json['firstName'] as String,
json['lastName'] as String,
DateTime.parse(json['date-of-birth'] as String),
middleName: json['middleName'] as String,
lastOrder: json['last-order'] == null
? null
: DateTime.parse(json['last-order'] as String),
orders: (json['orders'] as List)
.map((e) => new Order.fromJson(e as Map<String, dynamic>))
.toList());

abstract class _$PersonSerializerMixin {
String get firstName;
String get middleName;
String get lastName;
DateTime get dateOfBirth;
Map<String, dynamic> toJson() => <String, dynamic>{
'firstName': firstName,
'middleName': middleName,
'lastName': lastName,
'date-of-birth': dateOfBirth?.toIso8601String(),
};
DateTime get lastOrder;
List<Order> get orders;
Map<String, dynamic> toJson() {
var $map = <String, dynamic>{};
void $writeNotNull(String key, dynamic value) {
if (value != null) {
$map[key] = value;
}
}

$map['firstName'] = firstName;
$writeNotNull('middleName', middleName);
$map['lastName'] = lastName;
$map['date-of-birth'] = dateOfBirth.toIso8601String();
$map['last-order'] = lastOrder?.toIso8601String();
$map['orders'] = orders;
return $map;
}
}
```
7 changes: 5 additions & 2 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ part 'example.g.dart';

@JsonSerializable()
class Person extends Object with _$PersonSerializerMixin {
final String firstName, middleName, lastName;
final String firstName;
@JsonKey(includeIfNull: false)
final String middleName;
final String lastName;

@JsonKey(name: 'date-of-birth', nullable: false)
final DateTime dateOfBirth;
Expand All @@ -28,7 +31,7 @@ class Person extends Object with _$PersonSerializerMixin {
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
}

@JsonSerializable()
@JsonSerializable(includeIfNull: false)
class Order extends Object with _$OrderSerializerMixin {
int count;
int itemNumber;
Expand Down
44 changes: 30 additions & 14 deletions example/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 pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 0.2.0-dev
version: 0.2.0
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serializable
Expand Down