|
1 | 1 | [](https://travis-ci.org/dart-lang/json_serializable)
|
2 | 2 |
|
3 |
| -Provides [source_gen] `Generator`s to create code for JSON serialization and |
4 |
| -deserialization using the [Dart Build System]. |
| 3 | +Provides [Dart Build System] builders for handling JSON. |
5 | 4 |
|
6 |
| -See the [example] to understand how to configure your project for the latest |
7 |
| -released version of `json_serializable`. |
| 5 | +The builders generate code when they find members annotated with classes defined |
| 6 | +in [package:json_annotation]. |
| 7 | + |
| 8 | +- To generate to/from JSON code for a class, annotate it with |
| 9 | + `JsonSerializable`. You can provide arguments to `JsonSerializable` to |
| 10 | + configure the generated code. You can also customize individual fields |
| 11 | + by annotating them with `JsonKey` and providing custom arguments. |
| 12 | + |
| 13 | +- To generate a Dart field with the contents of a file containting JSON, use the |
| 14 | + `JsonLiteral` annotation. |
| 15 | + |
| 16 | +To configure your project for the latest released version of, |
| 17 | +`json_serializable` see the [example]. |
8 | 18 |
|
9 | 19 | ## Example
|
10 | 20 |
|
@@ -48,6 +58,40 @@ abstract class _$PersonSerializerMixin {
|
48 | 58 | }
|
49 | 59 | ```
|
50 | 60 |
|
| 61 | +# Build configuration |
| 62 | + |
| 63 | +Besides setting arguments on the associated annotation classes, you can also |
| 64 | +configure code generation by setting values in `build.yaml`. |
| 65 | + |
| 66 | +```yaml |
| 67 | +targets: |
| 68 | + $default: |
| 69 | + builders: |
| 70 | + json_serializable: |
| 71 | + options: |
| 72 | + # Specifies a string to add to the top of every generated file. |
| 73 | + # |
| 74 | + # If not specified, the default is the value of `defaultFileHeader` |
| 75 | + # defined in `package:source_gen/source_gen.dart`. |
| 76 | + # |
| 77 | + # Note: use `|` to define a multi-line block. |
| 78 | + header: | |
| 79 | + // Copyright (c) 2018, the Dart project authors. |
| 80 | +
|
| 81 | + // GENERATED CODE - DO NOT MODIFY BY HAND |
| 82 | +
|
| 83 | + # Options configure how source code is generated for every |
| 84 | + # `@JsonSerializable`-annotated class in the package. |
| 85 | + # |
| 86 | + # The default value for each of them: `false`. |
| 87 | + # |
| 88 | + # For usage information, reference the corresponding field in |
| 89 | + # `JsonSerializableGenerator`. |
| 90 | + use_wrappers: true |
| 91 | + any_map: true |
| 92 | + checked: true |
| 93 | +``` |
| 94 | +
|
51 | 95 | [example]: https://github.com/dart-lang/json_serializable/blob/master/example
|
52 |
| -[source_gen]: https://pub.dartlang.org/packages/source_gen |
53 | 96 | [Dart Build System]: https://github.com/dart-lang/build
|
| 97 | +[package:json_annotation]: https://pub.dartlang.org/packages/json_annotation |
0 commit comments