|
1 |
| -An simple example of a Dart package using [json_serializable]. |
| 1 | +To use [package:json_serializable][json_serializable] in your package, add these |
| 2 | +dependencies to your `pubspec.yaml`. |
2 | 3 |
|
3 |
| -* `lib/example.dart`: A library configured with annotations – |
4 |
| - `JsonSerializable` and `JsonLiteral` – to enable code generation. |
| 4 | +```yaml |
| 5 | +dependencies: |
| 6 | + json_annotation: ^0.2.1 |
5 | 7 |
|
6 |
| - * Note: the annotations are defined in `package:json_annotation`. |
7 |
| - This is the only package required in the `dependencies` section of your |
8 |
| - `pubspec.yaml`. |
| 8 | +dev_dependencies: |
| 9 | + build_runner: ^0.6.1 |
| 10 | + json_serializable: ^0.2.5 |
| 11 | + source_gen: ^0.7.2+1 |
| 12 | +``` |
9 | 13 |
|
10 |
| -* `tool/`: Contains the code run during development to create and update |
11 |
| - generated code. |
| 14 | +Next, create the scripts to run code generation. The [`tool`][tool] directory |
| 15 | +contains the conventional layout of these scripts. |
12 | 16 |
|
13 |
| - * `build_actions.dart`: A convention when using `package:build` to |
14 |
| - have one location to define the actions to be run by `build.dart` and |
15 |
| - `watch.dart`. See the comments in the source file for more information. |
| 17 | +* [`build_actions.dart`][tool]: A convention when using `package:build` to |
| 18 | + have one location to define the actions to be run by [`build.dart`][build] and |
| 19 | + [`watch.dart`][watch]. See the comments in the source file for more |
| 20 | + information. |
16 | 21 |
|
17 |
| - * `build.dart`: Runs one build using the actions defined in |
18 |
| - `build_actions.dart`. |
| 22 | +* [`build.dart`][build]: Runs one build using the actions defined in |
| 23 | + [`build_actions.dart`][build_actions]. |
19 | 24 |
|
20 |
| - * `watch.dart`: Starts a watcher that (re)runs the actions defined in |
21 |
| - `build_actions.dart` when files change. |
| 25 | +* [`watch.dart`][watch]: Starts a watcher that (re)runs the actions defined in |
| 26 | + [`build_actions.dart`][build_actions] when files change. |
22 | 27 |
|
| 28 | +Finally, annotate your code with classes defined in |
| 29 | +[package:json_annotation][json_annotation]. |
| 30 | + |
| 31 | +* See [`lib/example.dart`][example] for an example of a file using these |
| 32 | + annotations. |
| 33 | + |
| 34 | +* See [`lib/example.g.dart`][example_g] for the generated file. |
| 35 | + |
| 36 | +Run the build script to update the generated code after you modify of of the |
| 37 | +source files. |
| 38 | + |
| 39 | +```console |
| 40 | +> dart bin/build.dart |
| 41 | +[INFO] BuildDefinition: Reading cached asset graph completed, took 54ms |
| 42 | +[INFO] BuildDefinition: Checking for updates since last build completed, took 663ms |
| 43 | +[INFO] Build: Running build completed, took 10ms |
| 44 | +[INFO] Build: Caching finalized dependency graph completed, took 44ms |
| 45 | +[INFO] Build: Succeeded after 68ms with 0 outputs |
| 46 | +``` |
| 47 | + |
| 48 | +*NOTE*: If you're using Flutter, running a Dart script is a bit tricky. You can |
| 49 | +find the Dart entry point at `$FLUTTER_ROOT/bin/cache/dart-sdk/bin/dart`. |
| 50 | +You can determine `FLUTTER_ROOT` by looking at the first few lines of |
| 51 | +`flutter doctor`. |
| 52 | + |
| 53 | +```console |
| 54 | +> flutter doctor |
| 55 | +[✓] Flutter (on Mac OS X 10.12.6 16G1114, locale en-US, channel master) |
| 56 | + • Flutter at YOUR_FLUTTER_ROOT_HERE |
| 57 | + • Framework revision 235b64ed2f (13 hours ago), 2017-12-14 20:38:39 -0800 |
| 58 | +... |
| 59 | +``` |
| 60 | + |
| 61 | +[tool]: tool |
| 62 | +[build_actions]: tool/build_actions.dart |
| 63 | +[build]: tool/build.dart |
| 64 | +[watch]: tool/watch.dart |
| 65 | +[example]: lib/example.dart |
| 66 | +[example_g]: lib/example.g.dart |
| 67 | +[json_annotation]: https://pub.dartlang.org/packages/json_annotation |
23 | 68 | [json_serializable]: https://pub.dartlang.org/packages/json_serializable
|
0 commit comments