Skip to content

Commit eba5cd1

Browse files
committed
feature: default to creating toJson function instead of a mixin
The default for the `generate_to_json_function` is now `true`.
1 parent 749573e commit eba5cd1

19 files changed

+322
-446
lines changed

json_serializable/CHANGELOG.md

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

3+
* **BREAKING** By default, code generated to support `toJson` now creates
4+
a top-level function instead of a mixin.
5+
* The default for the `generate_to_json_function` is now `true`.
6+
* To opt-out of this change, set `generate_to_json_function` to `false`.
7+
38
* Now supports changing the serialized values of enums using `JsonValue`.
49

510
```dart

json_serializable/README.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ import 'package:json_annotation/json_annotation.dart';
2727
part 'example.g.dart';
2828
2929
@JsonSerializable(nullable: false)
30-
class Person extends Object with _$PersonSerializerMixin {
30+
class Person {
3131
final String firstName;
3232
final String lastName;
3333
final DateTime dateOfBirth;
3434
Person({this.firstName, this.lastName, this.dateOfBirth});
3535
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
36+
Map<String, dynamic> toJson() => _$PersonToJson(this);
3637
}
3738
```
3839

@@ -48,16 +49,11 @@ Person _$PersonFromJson(Map<String, dynamic> json) {
4849
dateOfBirth: DateTime.parse(json['dateOfBirth'] as String));
4950
}
5051
51-
abstract class _$PersonSerializerMixin {
52-
String get firstName;
53-
String get lastName;
54-
DateTime get dateOfBirth;
55-
Map<String, dynamic> toJson() => <String, dynamic>{
56-
'firstName': firstName,
57-
'lastName': lastName,
58-
'dateOfBirth': dateOfBirth.toIso8601String()
59-
};
60-
}
52+
Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
53+
'firstName': instance.firstName,
54+
'lastName': instance.lastName,
55+
'dateOfBirth': instance.dateOfBirth.toIso8601String()
56+
};
6157
```
6258

6359
# Build configuration
@@ -85,14 +81,14 @@ targets:
8581
# Options configure how source code is generated for every
8682
# `@JsonSerializable`-annotated class in the package.
8783
#
88-
# The default value for each of them: `false`.
84+
# The default value for each is listed.
8985
#
9086
# For usage information, reference the corresponding field in
9187
# `JsonSerializableGenerator`.
92-
use_wrappers: true
93-
any_map: true
94-
checked: true
95-
explicit_to_json: true
88+
use_wrappers: false
89+
any_map: false
90+
checked: false
91+
explicit_to_json: false
9692
generate_to_json_function: true
9793
```
9894

json_serializable/example/example.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import 'package:json_annotation/json_annotation.dart';
88
part 'example.g.dart';
99

1010
@JsonSerializable(nullable: false)
11-
class Person extends Object with _$PersonSerializerMixin {
11+
class Person {
1212
final String firstName;
1313
final String lastName;
1414
final DateTime dateOfBirth;
1515
Person({this.firstName, this.lastName, this.dateOfBirth});
1616
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
17+
Map<String, dynamic> toJson() => _$PersonToJson(this);
1718
}

json_serializable/example/example.g.dart

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ Person _$PersonFromJson(Map<String, dynamic> json) {
1717
dateOfBirth: DateTime.parse(json['dateOfBirth'] as String));
1818
}
1919

20-
abstract class _$PersonSerializerMixin {
21-
String get firstName;
22-
String get lastName;
23-
DateTime get dateOfBirth;
24-
Map<String, dynamic> toJson() => <String, dynamic>{
25-
'firstName': firstName,
26-
'lastName': lastName,
27-
'dateOfBirth': dateOfBirth.toIso8601String()
28-
};
29-
}
20+
Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
21+
'firstName': instance.firstName,
22+
'lastName': instance.lastName,
23+
'dateOfBirth': instance.dateOfBirth.toIso8601String()
24+
};

json_serializable/lib/src/json_part_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Builder jsonPartBuilder({
2727
bool anyMap = false,
2828
bool checked = false,
2929
bool explicitToJson = false,
30-
bool generateToJsonFunction = false,
30+
bool generateToJsonFunction = true,
3131
}) {
3232
return new PartBuilder([
3333
new JsonSerializableGenerator(

json_serializable/lib/src/json_serializable_generator.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,26 @@ class JsonSerializableGenerator
9595
/// Controls how `toJson` functionality is generated for all types processed
9696
/// by this generator.
9797
///
98-
/// If `false` (the default), a private `_$ClassNameSerializerMixin` class is
99-
/// created in the generated part file which contains a `toJson` method.
100-
///
101-
/// Mix in this class to the source class:
98+
/// If `true` (the default), then a top-level function is created that you can reference
99+
/// from your class.
102100
///
103101
/// ```dart
104102
/// @JsonSerializable()
105-
/// class Example extends Object with _$ExampleSerializerMixin {
103+
/// class Example {
106104
/// // ...
105+
/// Map<String, dynamic> toJson() => _$ExampleToJson(this);
107106
/// }
108107
/// ```
109108
///
110-
/// If `true`, then a top-level function is created that you can reference
111-
/// from your class.
109+
/// If `false`, a private `_$ClassNameSerializerMixin` class is
110+
/// created in the generated part file which contains a `toJson` method.
111+
///
112+
/// Mix in this class to the source class:
112113
///
113114
/// ```dart
114115
/// @JsonSerializable()
115-
/// class Example {
116+
/// class Example extends Object with _$ExampleSerializerMixin {
116117
/// // ...
117-
/// Map<String, dynamic> toJson() => _$ExampleToJson(this);
118118
/// }
119119
/// ```
120120
final bool generateToJsonFunction;
@@ -129,12 +129,12 @@ class JsonSerializableGenerator
129129
bool anyMap = false,
130130
bool checked = false,
131131
bool explicitToJson = false,
132-
bool generateToJsonFunction = false,
132+
bool generateToJsonFunction = true,
133133
}) : this.useWrappers = useWrappers ?? false,
134134
this.anyMap = anyMap ?? false,
135135
this.checked = checked ?? false,
136136
this.explicitToJson = explicitToJson ?? false,
137-
this.generateToJsonFunction = generateToJsonFunction ?? false,
137+
this.generateToJsonFunction = generateToJsonFunction ?? true,
138138
this._typeHelpers = typeHelpers ?? _defaultHelpers;
139139

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

json_serializable/test/config_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const _validConfig = const {
102102
'any_map': true,
103103
'checked': true,
104104
'explicit_to_json': true,
105-
'generate_to_json_function': true,
105+
'generate_to_json_function': false,
106106
};
107107

108108
const _invalidConfig = const {

json_serializable/test/integration/json_test_example.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'json_test_common.dart';
1111
part 'json_test_example.g.dart';
1212

1313
@JsonSerializable()
14-
class Person extends Object with _$PersonSerializerMixin {
14+
class Person {
1515
final String firstName, middleName, lastName;
1616
final DateTime dateOfBirth;
1717
@JsonKey(name: '\$house')
@@ -26,6 +26,8 @@ class Person extends Object with _$PersonSerializerMixin {
2626

2727
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
2828

29+
Map<String, dynamic> toJson() => _$PersonToJson(this);
30+
2931
bool operator ==(Object other) =>
3032
other is Person &&
3133
firstName == other.firstName &&
@@ -37,7 +39,7 @@ class Person extends Object with _$PersonSerializerMixin {
3739
}
3840

3941
@JsonSerializable()
40-
class Order extends Object with _$OrderSerializerMixin {
42+
class Order {
4143
/// Used to test that `disallowNullValues: true` forces `includeIfNull: false`
4244
@JsonKey(disallowNullValue: true)
4345
int count;
@@ -74,6 +76,8 @@ class Order extends Object with _$OrderSerializerMixin {
7476

7577
factory Order.fromJson(Map<String, dynamic> json) => _$OrderFromJson(json);
7678

79+
Map<String, dynamic> toJson() => _$OrderToJson(this);
80+
7781
bool operator ==(Object other) =>
7882
other is Order &&
7983
count == other.count &&
@@ -83,7 +87,7 @@ class Order extends Object with _$OrderSerializerMixin {
8387
}
8488

8589
@JsonSerializable()
86-
class Item extends ItemCore with _$ItemSerializerMixin {
90+
class Item extends ItemCore {
8791
@JsonKey(includeIfNull: false, name: 'item-number')
8892
int itemNumber;
8993
List<DateTime> saleDates;
@@ -93,6 +97,8 @@ class Item extends ItemCore with _$ItemSerializerMixin {
9397

9498
factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);
9599

100+
Map<String, dynamic> toJson() => _$ItemToJson(this);
101+
96102
bool operator ==(Object other) =>
97103
other is Item &&
98104
price == other.price &&
@@ -101,7 +107,7 @@ class Item extends ItemCore with _$ItemSerializerMixin {
101107
}
102108

103109
@JsonSerializable()
104-
class Numbers extends Object with _$NumbersSerializerMixin {
110+
class Numbers {
105111
List<int> ints;
106112
List<num> nums;
107113
List<double> doubles;
@@ -120,6 +126,8 @@ class Numbers extends Object with _$NumbersSerializerMixin {
120126
factory Numbers.fromJson(Map<String, dynamic> json) =>
121127
_$NumbersFromJson(json);
122128

129+
Map<String, dynamic> toJson() => _$NumbersToJson(this);
130+
123131
bool operator ==(Object other) =>
124132
other is Numbers &&
125133
deepEquals(ints, other.ints) &&

json_serializable/test/integration/json_test_example.g.dart

Lines changed: 48 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,16 @@ Person _$PersonFromJson(Map<String, dynamic> json) {
2424
(k, e) => new MapEntry(k, _$enumDecodeNullable(_$CategoryEnumMap, e)));
2525
}
2626

27-
abstract class _$PersonSerializerMixin {
28-
String get firstName;
29-
String get middleName;
30-
String get lastName;
31-
DateTime get dateOfBirth;
32-
Category get house;
33-
Order get order;
34-
Map<String, Category> get houseMap;
35-
Map<String, dynamic> toJson() => <String, dynamic>{
36-
'firstName': firstName,
37-
'middleName': middleName,
38-
'lastName': lastName,
39-
'dateOfBirth': dateOfBirth?.toIso8601String(),
40-
r'$house': _$CategoryEnumMap[house],
41-
'order': order,
42-
'houseMap':
43-
houseMap?.map((k, e) => new MapEntry(k, _$CategoryEnumMap[e]))
44-
};
45-
}
27+
Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
28+
'firstName': instance.firstName,
29+
'middleName': instance.middleName,
30+
'lastName': instance.lastName,
31+
'dateOfBirth': instance.dateOfBirth?.toIso8601String(),
32+
r'$house': _$CategoryEnumMap[instance.house],
33+
'order': instance.order,
34+
'houseMap': instance.houseMap
35+
?.map((k, e) => new MapEntry(k, _$CategoryEnumMap[e]))
36+
};
4637

4738
T _$enumDecode<T>(Map<T, dynamic> enumValues, dynamic source) {
4839
if (source == null) {
@@ -95,34 +86,24 @@ Order _$OrderFromJson(Map<String, dynamic> json) {
9586
StatusCode.success;
9687
}
9788

98-
abstract class _$OrderSerializerMixin {
99-
int get count;
100-
bool get isRushed;
101-
Category get category;
102-
UnmodifiableListView<Item> get items;
103-
Platform get platform;
104-
Map<String, Platform> get altPlatforms;
105-
Uri get homepage;
106-
StatusCode get statusCode;
107-
Map<String, dynamic> toJson() {
108-
var val = <String, dynamic>{};
109-
110-
void writeNotNull(String key, dynamic value) {
111-
if (value != null) {
112-
val[key] = value;
113-
}
114-
}
89+
Map<String, dynamic> _$OrderToJson(Order instance) {
90+
var val = <String, dynamic>{};
11591

116-
writeNotNull('count', count);
117-
val['isRushed'] = isRushed;
118-
val['category'] = _$CategoryEnumMap[category];
119-
val['items'] = items;
120-
val['platform'] = platform;
121-
val['altPlatforms'] = altPlatforms;
122-
val['homepage'] = homepage?.toString();
123-
val['status_code'] = _$StatusCodeEnumMap[statusCode];
124-
return val;
92+
void writeNotNull(String key, dynamic value) {
93+
if (value != null) {
94+
val[key] = value;
95+
}
12596
}
97+
98+
writeNotNull('count', instance.count);
99+
val['isRushed'] = instance.isRushed;
100+
val['category'] = _$CategoryEnumMap[instance.category];
101+
val['items'] = instance.items;
102+
val['platform'] = instance.platform;
103+
val['altPlatforms'] = instance.altPlatforms;
104+
val['homepage'] = instance.homepage?.toString();
105+
val['status_code'] = _$StatusCodeEnumMap[instance.statusCode];
106+
return val;
126107
}
127108

128109
const _$StatusCodeEnumMap = const <StatusCode, dynamic>{
@@ -139,27 +120,22 @@ Item _$ItemFromJson(Map<String, dynamic> json) {
139120
..rates = (json['rates'] as List)?.map((e) => e as int)?.toList();
140121
}
141122

142-
abstract class _$ItemSerializerMixin {
143-
int get price;
144-
int get itemNumber;
145-
List<DateTime> get saleDates;
146-
List<int> get rates;
147-
Map<String, dynamic> toJson() {
148-
var val = <String, dynamic>{
149-
'price': price,
150-
};
123+
Map<String, dynamic> _$ItemToJson(Item instance) {
124+
var val = <String, dynamic>{
125+
'price': instance.price,
126+
};
151127

152-
void writeNotNull(String key, dynamic value) {
153-
if (value != null) {
154-
val[key] = value;
155-
}
128+
void writeNotNull(String key, dynamic value) {
129+
if (value != null) {
130+
val[key] = value;
156131
}
157-
158-
writeNotNull('item-number', itemNumber);
159-
val['saleDates'] = saleDates?.map((e) => e?.toIso8601String())?.toList();
160-
val['rates'] = rates;
161-
return val;
162132
}
133+
134+
writeNotNull('item-number', instance.itemNumber);
135+
val['saleDates'] =
136+
instance.saleDates?.map((e) => e?.toIso8601String())?.toList();
137+
val['rates'] = instance.rates;
138+
return val;
163139
}
164140

165141
Numbers _$NumbersFromJson(Map<String, dynamic> json) {
@@ -177,19 +153,12 @@ Numbers _$NumbersFromJson(Map<String, dynamic> json) {
177153
json['date'] == null ? null : dateTimeFromEpochUs(json['date'] as int);
178154
}
179155

180-
abstract class _$NumbersSerializerMixin {
181-
List<int> get ints;
182-
List<num> get nums;
183-
List<double> get doubles;
184-
List<double> get nnDoubles;
185-
Duration get duration;
186-
DateTime get date;
187-
Map<String, dynamic> toJson() => <String, dynamic>{
188-
'ints': ints,
189-
'nums': nums,
190-
'doubles': doubles,
191-
'nnDoubles': nnDoubles,
192-
'duration': duration == null ? null : durationToInt(duration),
193-
'date': date == null ? null : dateTimeToEpochUs(date)
194-
};
195-
}
156+
Map<String, dynamic> _$NumbersToJson(Numbers instance) => <String, dynamic>{
157+
'ints': instance.ints,
158+
'nums': instance.nums,
159+
'doubles': instance.doubles,
160+
'nnDoubles': instance.nnDoubles,
161+
'duration':
162+
instance.duration == null ? null : durationToInt(instance.duration),
163+
'date': instance.date == null ? null : dateTimeToEpochUs(instance.date)
164+
};

0 commit comments

Comments
 (0)