Skip to content

Commit dcc438e

Browse files
authored
Update readme docs to include configuration information (#175)
1 parent f174168 commit dcc438e

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

example/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ targets:
44
builders:
55
json_serializable:
66
options:
7-
header: |+
7+
header: |
88
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
99
// for details. All rights reserved. Use of this source code is governed by a
1010
// BSD-style license that can be found in the LICENSE file.

json_serializable/README.md

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
[![Build Status](https://travis-ci.org/dart-lang/json_serializable.svg?branch=master)](https://travis-ci.org/dart-lang/json_serializable)
22

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.
54

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].
818

919
## Example
1020

@@ -48,6 +58,40 @@ abstract class _$PersonSerializerMixin {
4858
}
4959
```
5060

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+
5195
[example]: https://github.com/dart-lang/json_serializable/blob/master/example
52-
[source_gen]: https://pub.dartlang.org/packages/source_gen
5396
[Dart Build System]: https://github.com/dart-lang/build
97+
[package:json_annotation]: https://pub.dartlang.org/packages/json_annotation

json_serializable/test/config_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:io';
67

78
import 'package:build/build.dart';
89
import 'package:json_serializable/builder.dart';
910
import 'package:logging/logging.dart';
1011
import 'package:source_gen/source_gen.dart';
1112
import 'package:test/test.dart';
13+
import 'package:yaml/yaml.dart';
1214

1315
final _throwsCastError = throwsA(const isInstanceOf<CastError>());
1416

@@ -40,6 +42,35 @@ void main() {
4042
expect(records, isEmpty);
4143
});
4244

45+
test('readme config', () async {
46+
var configExampleContent = new File('README.md')
47+
.readAsLinesSync()
48+
.skipWhile((line) => line != '```yaml')
49+
.skip(1)
50+
.takeWhile((line) => line != '```')
51+
.join('\n');
52+
53+
var yaml = loadYaml(configExampleContent) as YamlMap;
54+
55+
for (var key in [
56+
'targets',
57+
r'$default',
58+
'builders',
59+
'json_serializable',
60+
'options'
61+
]) {
62+
yaml = yaml[key] as YamlMap;
63+
}
64+
65+
var config = new Map<String, dynamic>.from(yaml);
66+
67+
expect(config.keys, unorderedEquals(_validConfig.keys));
68+
69+
var builder = jsonSerializable(new BuilderOptions(config)) as PartBuilder;
70+
expect(builder, isNotNull);
71+
expect(records, isEmpty);
72+
});
73+
4374
test('unsupported configuration', () async {
4475
var builder =
4576
jsonSerializable(const BuilderOptions(const {'unsupported': 'config'}))

0 commit comments

Comments
 (0)