Skip to content

Commit de541b1

Browse files
committed
Prepare for v0.2 release
1 parent 86098bc commit de541b1

File tree

5 files changed

+85
-40
lines changed

5 files changed

+85
-40
lines changed

CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

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

5-
* `package:json_serializable/generators.dart` contains the `Generator`
5+
* `package:json_serializable/generators.dart` contains `Generator`
66
implementations.
77

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

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

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

1717
* **BREAKING** `TypeHelper`:
@@ -20,7 +20,7 @@
2020
type is not supported.
2121

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

2626
* **BREAKING** `JsonKey.jsonName` was renamed to `name` and is now a named

README.md

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,65 @@ part 'example.g.dart';
1616
1717
@JsonSerializable()
1818
class Person extends Object with _$PersonSerializerMixin {
19-
final String firstName, middleName, lastName;
19+
final String firstName;
20+
@JsonKey(includeIfNull: false)
21+
final String middleName;
22+
final String lastName;
2023
21-
@JsonKey('date-of-birth')
24+
@JsonKey(name: 'date-of-birth', nullable: false)
2225
final DateTime dateOfBirth;
2326
24-
Person(this.firstName, this.lastName, {this.middleName, this.dateOfBirth});
27+
@JsonKey(name: 'last-order')
28+
final DateTime lastOrder;
2529
26-
factory Person.fromJson(json) => _$PersonFromJson(json);
30+
@JsonKey(nullable: false)
31+
List<Order> orders;
32+
33+
Person(this.firstName, this.lastName, this.dateOfBirth,
34+
{this.middleName, this.lastOrder, List<Order> orders})
35+
: this.orders = orders ?? <Order>[];
36+
37+
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
2738
}
2839
```
2940

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

3243
```dart
33-
part of json_serializable.example;
34-
35-
Person _$PersonFromJson(Map json) =>
36-
new Person(json['firstName'] as String, json['lastName'] as String,
37-
middleName: json['middleName'] as String,
38-
dateOfBirth: json['date-of-birth'] == null
39-
? null
40-
: DateTime.parse(json['date-of-birth']));
44+
Person _$PersonFromJson(Map<String, dynamic> json) => new Person(
45+
json['firstName'] as String,
46+
json['lastName'] as String,
47+
DateTime.parse(json['date-of-birth'] as String),
48+
middleName: json['middleName'] as String,
49+
lastOrder: json['last-order'] == null
50+
? null
51+
: DateTime.parse(json['last-order'] as String),
52+
orders: (json['orders'] as List)
53+
.map((e) => new Order.fromJson(e as Map<String, dynamic>))
54+
.toList());
4155
4256
abstract class _$PersonSerializerMixin {
4357
String get firstName;
4458
String get middleName;
4559
String get lastName;
4660
DateTime get dateOfBirth;
47-
Map<String, dynamic> toJson() => <String, dynamic>{
48-
'firstName': firstName,
49-
'middleName': middleName,
50-
'lastName': lastName,
51-
'date-of-birth': dateOfBirth?.toIso8601String(),
52-
};
61+
DateTime get lastOrder;
62+
List<Order> get orders;
63+
Map<String, dynamic> toJson() {
64+
var $map = <String, dynamic>{};
65+
void $writeNotNull(String key, dynamic value) {
66+
if (value != null) {
67+
$map[key] = value;
68+
}
69+
}
70+
71+
$map['firstName'] = firstName;
72+
$writeNotNull('middleName', middleName);
73+
$map['lastName'] = lastName;
74+
$map['date-of-birth'] = dateOfBirth.toIso8601String();
75+
$map['last-order'] = lastOrder?.toIso8601String();
76+
$map['orders'] = orders;
77+
return $map;
78+
}
5379
}
5480
```

example/example.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ part 'example.g.dart';
1010

1111
@JsonSerializable()
1212
class Person extends Object with _$PersonSerializerMixin {
13-
final String firstName, middleName, lastName;
13+
final String firstName;
14+
@JsonKey(includeIfNull: false)
15+
final String middleName;
16+
final String lastName;
1417

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

31-
@JsonSerializable()
34+
@JsonSerializable(includeIfNull: false)
3235
class Order extends Object with _$OrderSerializerMixin {
3336
int count;
3437
int itemNumber;

example/example.g.dart

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

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 0.2.0-dev
2+
version: 0.2.0
33
author: Dart Team <misc@dartlang.org>
44
description: Generates utilities to aid in serializing to/from JSON.
55
homepage: https://github.com/dart-lang/json_serializable

0 commit comments

Comments
 (0)