Skip to content

Commit be7394b

Browse files
committed
Flip the option name and default
1 parent 01e6694 commit be7394b

8 files changed

+29
-26
lines changed

json_serializable/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ targets:
8383
# Options configure how source code is generated for every
8484
# `@JsonSerializable`-annotated class in the package.
8585
#
86-
# The default value for each of them: `false`.
86+
# The default value for each is shown.
8787
#
8888
# For usage information, reference the corresponding field in
8989
# `JsonSerializableGenerator`.
90-
use_wrappers: true
91-
any_map: true
92-
checked: true
93-
explicit_to_json: true
90+
use_wrappers: false
91+
any_map: false
92+
checked: false
93+
implicit_to_json: true
9494
```
9595
9696
[example]: https://github.com/dart-lang/json_serializable/blob/master/example

json_serializable/lib/builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Builder jsonSerializable(BuilderOptions options) {
3030
useWrappers: optionsMap.remove('use_wrappers') as bool,
3131
checked: optionsMap.remove('checked') as bool,
3232
anyMap: optionsMap.remove('any_map') as bool,
33-
explicitToJson: optionsMap.remove('explicit_to_json') as bool,
33+
implicitToJson: optionsMap.remove('implicit_to_json') as bool,
3434
);
3535

3636
if (optionsMap.isNotEmpty) {

json_serializable/lib/src/json_part_builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ Builder jsonPartBuilder({
2626
bool useWrappers: false,
2727
bool anyMap: false,
2828
bool checked: false,
29-
bool explicitToJson: false,
29+
bool implicitToJson: true,
3030
}) {
3131
return new PartBuilder([
3232
new JsonSerializableGenerator(
3333
useWrappers: useWrappers,
3434
anyMap: anyMap,
3535
checked: checked,
36-
explicitToJson: explicitToJson,
36+
implicitToJson: implicitToJson,
3737
),
3838
const JsonLiteralGenerator()
3939
], header: header, formatOutput: formatOutput);

json_serializable/lib/src/json_serializable_generator.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,25 @@ class JsonSerializableGenerator
6666
/// [CheckedFromJsonException] is thrown.
6767
final bool checked;
6868

69-
/// If `true`, generated `toJson` methods will explicitly call `toJson` on
69+
/// If `false`, generated `toJson` methods will explicitly call `toJson` on
7070
/// nested objects.
7171
///
7272
/// When using JSON encoding support in `dart:convert`, `toJson` is
7373
/// automatically called on objects, so the default behavior
74-
/// (`explicitToJson: false`) is to omit the `toJson` call.
74+
/// (`implicitToJson: true`) is to omit the `toJson` call.
7575
///
76-
/// Example of `explicitToJson: false` (default)
76+
/// Example of `implicitToJson: true` (default)
7777
///
7878
/// ```dart
7979
/// Map<String, dynamic> toJson() => {'child': child};
8080
/// ```
8181
///
82-
/// Example of `explicitToJson: true`
82+
/// Example of `implicitToJson: false`
8383
///
8484
/// ```dart
8585
/// Map<String, dynamic> toJson() => {'child': child?.toJson()};
8686
/// ```
87-
final bool explicitToJson;
87+
final bool implicitToJson;
8888

8989
/// Creates an instance of [JsonSerializableGenerator].
9090
///
@@ -95,11 +95,11 @@ class JsonSerializableGenerator
9595
bool useWrappers: false,
9696
bool anyMap: false,
9797
bool checked: false,
98-
bool explicitToJson: false,
98+
bool implicitToJson: true,
9999
}) : this.useWrappers = useWrappers ?? false,
100100
this.anyMap = anyMap ?? false,
101101
this.checked = checked ?? false,
102-
this.explicitToJson = explicitToJson ?? false,
102+
this.implicitToJson = implicitToJson ?? true,
103103
this._typeHelpers = typeHelpers ?? _defaultHelpers;
104104

105105
/// Creates an instance of [JsonSerializableGenerator].

json_serializable/lib/src/type_helper_context.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TypeHelperContext implements SerializeContext, DeserializeContext {
2828
// Consider exposing it if there is interest
2929
bool get anyMap => _generator.anyMap;
3030

31-
bool get explicitToJson => _generator.explicitToJson;
31+
bool get implicitToJson => _generator.implicitToJson;
3232

3333
TypeHelperContext(this._generator, this.metadata, this.nullable,
3434
this.fromJsonData, this.toJsonData);

json_serializable/lib/src/type_helpers/json_helper.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@ import '../utils.dart';
1616
class JsonHelper extends TypeHelper {
1717
const JsonHelper();
1818

19-
/// Simply returns the [expression] provided.
19+
/// If the builder is configured with `implicit_to_json: true`, simply returns
20+
/// the [expression] provided.
2021
///
21-
/// By default, JSON encoding in from `dart:convert` calls `toJson()` on
22-
/// provided objects.
22+
/// Otherwise, returns [expression]`.toJson()` with proper `null` handling.
2323
@override
2424
String serialize(
2525
DartType targetType, String expression, SerializeContext context) {
2626
if (!_canSerialize(targetType)) {
2727
return null;
2828
}
2929

30-
if (context is TypeHelperContext && context.explicitToJson) {
31-
return '$expression${context.nullable ? '?' : ''}.toJson()';
30+
var ctx = context as TypeHelperContext;
31+
32+
if (ctx.implicitToJson) {
33+
return expression;
3234
}
33-
return expression;
35+
36+
return '$expression${context.nullable ? '?' : ''}.toJson()';
3437
}
3538

3639
@override

json_serializable/test/config_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ const _validConfig = const {
101101
'use_wrappers': true,
102102
'any_map': true,
103103
'checked': true,
104-
'explicit_to_json': true
104+
'implicit_to_json': false
105105
};
106106

107107
const _invalidConfig = const {
108108
'header': true,
109109
'use_wrappers': 42,
110110
'any_map': 42,
111111
'checked': 42,
112-
'explicit_to_json': 42
112+
'implicit_to_json': 42
113113
};

json_serializable/test/json_serializable_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void _registerTests(JsonSerializableGenerator generator) {
7070
test('nullable', () async {
7171
var output = await _runForElementNamed(
7272
new JsonSerializableGenerator(
73-
explicitToJson: true, useWrappers: generator.useWrappers),
73+
implicitToJson: false, useWrappers: generator.useWrappers),
7474
'TrivialNestedNullable');
7575

7676
var expected = generator.useWrappers
@@ -115,7 +115,7 @@ class _$TrivialNestedNullableJsonMapWrapper extends $JsonMapWrapper {
115115
test('non-nullable', () async {
116116
var output = await _runForElementNamed(
117117
new JsonSerializableGenerator(
118-
explicitToJson: true, useWrappers: generator.useWrappers),
118+
implicitToJson: false, useWrappers: generator.useWrappers),
119119
'TrivialNestedNonNullable');
120120

121121
var expected = generator.useWrappers

0 commit comments

Comments
 (0)