Skip to content

Commit 25ba2b8

Browse files
committed
Add new library: json_serializable.dart
* Added a new method: jsonPartBuilder - so folks don't need to reference source_gen * Deprecated generators.dart in favor of json_serializable.dart
1 parent b854ce5 commit 25ba2b8

7 files changed

+71
-8
lines changed

json_serializable/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## 0.3.0
22

3+
* **NEW** top-level library `json_serializable.dart`.
4+
5+
* Replaces now deprecated `generators.dart` to access
6+
`JsonSerializableGenerator` and `JsonLiteralGenerator`.
7+
8+
* Adds the `jsonPartBuilder` function to make it easy to create a
9+
`PartBuilder`, without creating an explicit dependency on `source_gen`.
10+
311
* **BREAKING** `UnsupportedTypeError` added a new required constructor argument:
412
`reason`.
513

json_serializable/lib/generators.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// Generator classes.
5+
/// **DEPRECATED** import `package:json_serializable/json_serializable.dart`
6+
/// instead.
7+
@Deprecated(
8+
'import `package:json_serializable/json_serializable.dart` instead.')
9+
library json_serializable.generators;
610

711
export 'src/json_literal_generator.dart';
812
export 'src/json_serializable_generator.dart';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
export 'src/json_literal_generator.dart';
6+
export 'src/json_part_builder.dart';
7+
export 'src/json_serializable_generator.dart';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:source_gen/source_gen.dart';
6+
7+
import 'json_literal_generator.dart';
8+
import 'json_serializable_generator.dart';
9+
10+
/// Returns a [PartBuilder] for use within a `package:build_runner`
11+
/// `BuildAction`.
12+
///
13+
/// [header] is used to specify the content at the top of each generated file.
14+
/// If `null`, the content of [defaultFileHeader] is used.
15+
/// If [header] is an empty `String` no header is added.
16+
///
17+
/// If [useWrappers] is `true`, wrappers are used to minimize the number of
18+
/// [Map] and [List] instances created during serialization. This will
19+
/// increase the code size, but it may improve runtime performance, especially
20+
/// for large object graphs.
21+
///
22+
/// Usage:
23+
///
24+
/// ```dart
25+
/// new BuildAction(
26+
/// jsonPartBuilder(),
27+
/// 'my_package',
28+
/// inputs: const ['lib/*.dart']
29+
/// )
30+
/// ```
31+
///
32+
/// See the [example] to understand how to configure your project for
33+
/// `json_serializable`.
34+
///
35+
/// [example]: https://github.com/dart-lang/json_serializable/tree/master/example
36+
PartBuilder jsonPartBuilder({String header, bool useWrappers: false}) {
37+
useWrappers ??= false;
38+
return new PartBuilder([
39+
new JsonSerializableGenerator(useWrappers: useWrappers),
40+
const JsonLiteralGenerator()
41+
], header: header);
42+
}

json_serializable/lib/src/json_serializable_generator.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class JsonSerializableGenerator
4242
///
4343
/// If [typeHelpers] is not provided, two built-in helpers are used:
4444
/// [JsonHelper] and [DateTimeHelper].
45+
///
46+
/// If [useWrappers] is `true`, wrappers are used to minimize the number of
47+
/// [Map] and [List] instances created during serialization. This will
48+
/// increase the code size, but it may improve runtime performance, especially
49+
/// for large object graphs.
4550
const JsonSerializableGenerator(
4651
{List<TypeHelper> typeHelpers, bool useWrappers: false})
4752
: this.useWrappers = useWrappers ?? false,

json_serializable/test/json_serializable_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'dart:async';
1212
import 'package:analyzer/dart/ast/ast.dart';
1313
import 'package:analyzer/src/string_source.dart';
1414
import 'package:dart_style/dart_style.dart' as dart_style;
15-
import 'package:json_serializable/generators.dart';
15+
import 'package:json_serializable/json_serializable.dart';
1616
import 'package:json_serializable/src/utils.dart';
1717
import 'package:path/path.dart' as p;
1818
import 'package:source_gen/source_gen.dart';

json_serializable/tool/build_actions.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'dart:async';
77
import 'package:build/build.dart';
88

99
import 'package:build_runner/build_runner.dart';
10-
import 'package:json_serializable/generators.dart';
10+
import 'package:json_serializable/json_serializable.dart';
1111
import 'package:path/path.dart' as p;
1212
import 'package:source_gen/source_gen.dart';
1313

@@ -31,9 +31,7 @@ final List<BuildAction> buildActions = [
3131
'test/test_files/json_test_example.non_nullable.dart',
3232
]),
3333
new BuildAction(
34-
new PartBuilder(
35-
const [const JsonSerializableGenerator(), const JsonLiteralGenerator()],
36-
header: _copyrightHeader),
34+
jsonPartBuilder(header: _copyrightHeader),
3735
'json_serializable',
3836
inputs: const [
3937
'test/test_files/json_literal.dart',
@@ -44,8 +42,7 @@ final List<BuildAction> buildActions = [
4442
],
4543
),
4644
new BuildAction(
47-
new PartBuilder(const [const JsonSerializableGenerator(useWrappers: true)],
48-
header: _copyrightHeader),
45+
jsonPartBuilder(useWrappers: true, header: _copyrightHeader),
4946
'json_serializable',
5047
inputs: const [
5148
'test/test_files/kitchen_sink*wrapped.dart',

0 commit comments

Comments
 (0)