11[ ![ Build Status] ( https://travis-ci.org/dart-lang/source_gen.svg?branch=master )] ( https://travis-ci.org/dart-lang/source_gen )
22[ ![ Coverage Status] ( https://coveralls.io/repos/dart-lang/source_gen/badge.svg?branch=master )] ( https://coveralls.io/r/dart-lang/source_gen )
3- [ ![ Stories in Ready] ( https://badge.waffle.io/dart-lang/source_gen.png?label=ready&title=Ready )] ( https://waffle.io/dart-lang/source_gen )
43
54_ ** Highly experimental. Expect breaking changes.** _
65
@@ -14,18 +13,20 @@ _**Highly experimental. Expect breaking changes.**_
1413
1514## Example
1615
17- Given a library ` example.dart ` :
16+ Given a library ` example.dart ` with an ` Person ` class annotated with
17+ ` @JsonSerializable ` :
1818
1919``` dart
20- library source_gen.example.example ;
20+ library source_gen.example;
2121
2222import 'package:source_gen/generators/json_serializable.dart';
23-
2423part 'example.g.dart';
2524
2625@JsonSerializable()
2726class Person extends Object with _$PersonSerializerMixin {
2827 final String firstName, middleName, lastName;
28+
29+ @JsonKey('date-of-birth')
2930 final DateTime dateOfBirth;
3031
3132 Person(this.firstName, this.lastName, {this.middleName, this.dateOfBirth});
@@ -37,31 +38,26 @@ class Person extends Object with _$PersonSerializerMixin {
3738` source_gen ` creates the corresponding part ` example.g.dart ` :
3839
3940``` dart
40- part of source_gen.example.example;
41-
42- // **************************************************************************
43- // Generator: JsonGenerator
44- // Target: class Person
45- // **************************************************************************
41+ part of source_gen.example;
4642
47- Person _$PersonFromJson(Map<String, Object> json) => new Person(
43+ Person _$PersonFromJson(Map json) => new Person(
4844 json['firstName'], json['lastName'],
4945 middleName: json['middleName'],
50- dateOfBirth: json.containsKey('dateOfBirth')
51- ? DateTime.parse(json['dateOfBirth'])
52- : null );
46+ dateOfBirth: json['date-of-birth'] == null
47+ ? null
48+ : DateTime.parse(json['date-of-birth']) );
5349
5450abstract class _$PersonSerializerMixin {
5551 String get firstName;
5652 String get middleName;
5753 String get lastName;
5854 DateTime get dateOfBirth;
59- Map<String, Object > toJson() => {
60- 'firstName': firstName,
61- 'middleName': middleName,
62- 'lastName': lastName,
63- 'dateOfBirth': dateOfBirth == null ? null : dateOfBirth.toIso8601String()
64- };
55+ Map<String, dynamic > toJson() => <String, dynamic> {
56+ 'firstName': firstName,
57+ 'middleName': middleName,
58+ 'lastName': lastName,
59+ 'date-of-birth' : dateOfBirth? .toIso8601String(),
60+ };
6561}
6662```
6763
@@ -76,15 +72,13 @@ Extend the `Generator` class to plug into `source_gen`.
7672
7773## Running generators
7874
79- Create a script that invokes the [ generate] [ ] method.
80-
81- Alternatively, you can create a ` build.dart ` file which calls the [ build]
82- method. You can use the [ build_system package] [ ] to enjoy this automatic workflow.
83-
84- * Note: The original design was meant to work with the
85- [ Dart Editor build system] [ ] . Dart Editor is unsupported as of Dart 1.11.*
75+ ` source_gen ` is based on the ` build ` package (
76+ [ pub] ( https://pub.dartlang.org/packages/build ) ,
77+ [ GitHub] ( https://github.com/dart-lang/build ) ).
8678
87- See [ build.dart] [ ] in the repository for an example.
79+ See ` build.dart ` and ` watch.dart ` in the ` tool ` directory. Both reference
80+ ` tool/phases.dart ` , which contains information mapping ` source_gen ` generators
81+ to target files.
8882
8983## source_gen vs Dart Transformers
9084[ Dart Transformers] [ ] are often used to create and modify code and assets as part
@@ -111,5 +105,3 @@ detail*.
111105[ build.dart ] : https://github.com/dart-lang/source_gen/blob/master/build.dart
112106[ generate ] : http://www.dartdocs.org/documentation/source_gen/latest/index.html#source_gen/source_gen@id_generate
113107[ build ] : http://www.dartdocs.org/documentation/source_gen/latest/index.html#source_gen/source_gen@id_build
114- [ Dart Editor build system ] : https://www.dartlang.org/tools/editor/build.html
115- [ build_system package ] : https://pub.dartlang.org/packages/build_system
0 commit comments