@@ -106,8 +106,11 @@ class JsonSerializableGenerator
106
106
107
107
var buffer = new StringBuffer ();
108
108
109
- if (annotation.read ('createFactory' ).boolValue) {
110
- var toSkip = _writeFactory (buffer, classElement, fields, prefix);
109
+ final classAnnotation = _valueForAnnotation (annotation);
110
+
111
+ if (classAnnotation.createFactory) {
112
+ var toSkip = _writeFactory (
113
+ buffer, classElement, fields, prefix, classAnnotation.nullable);
111
114
112
115
// If there are fields that are final – that are not set via the generated
113
116
// constructor, then don't output them when generating the `toJson` call.
@@ -130,7 +133,7 @@ class JsonSerializableGenerator
130
133
return set ;
131
134
});
132
135
133
- if (annotation. read ( ' createToJson' ).boolValue ) {
136
+ if (classAnnotation. createToJson) {
134
137
//
135
138
// Generate the mixin class
136
139
//
@@ -143,15 +146,14 @@ class JsonSerializableGenerator
143
146
buffer.writeln (' ${field .type } get ${field .name };' );
144
147
}
145
148
146
- var includeIfNull = annotation.read ('includeIfNull' ).boolValue;
147
-
148
149
buffer.writeln (' Map<String, dynamic> toJson() ' );
149
- if (fieldsList.every ((e) => _includeIfNull (e, includeIfNull))) {
150
+ if (fieldsList
151
+ .every ((e) => _includeIfNull (e, classAnnotation.includeIfNull))) {
150
152
// write simple `toJson` method that includes all keys...
151
- _writeToJsonSimple (buffer, fields.values);
153
+ _writeToJsonSimple (buffer, fields.values, classAnnotation.nullable );
152
154
} else {
153
155
// At least one field should be excluded if null
154
- _writeToJsonWithNullChecks (buffer, fields.values, includeIfNull );
156
+ _writeToJsonWithNullChecks (buffer, fields.values, classAnnotation );
155
157
}
156
158
157
159
// end of the mixin class
@@ -162,7 +164,7 @@ class JsonSerializableGenerator
162
164
}
163
165
164
166
void _writeToJsonWithNullChecks (StringBuffer buffer,
165
- Iterable <FieldElement > fields, bool classIncludeIfNull ) {
167
+ Iterable <FieldElement > fields, JsonSerializable classAnnotation ) {
166
168
buffer.writeln ('{' );
167
169
168
170
buffer.writeln ('var $toJsonMapVarName = <String, dynamic>{' );
@@ -184,13 +186,14 @@ class JsonSerializableGenerator
184
186
safeFieldAccess = 'this.$safeFieldAccess ' ;
185
187
}
186
188
187
- if (_includeIfNull (field, classIncludeIfNull)) {
189
+ var expression = _serializeField (field, classAnnotation.nullable,
190
+ accessOverride: safeFieldAccess);
191
+ if (_includeIfNull (field, classAnnotation.includeIfNull)) {
188
192
if (directWrite) {
189
- buffer.writeln ('$safeJsonKeyString : '
190
- '${_serializeField (field , accessOverride : safeFieldAccess )},' );
193
+ buffer.writeln ('$safeJsonKeyString : $expression ,' );
191
194
} else {
192
- buffer. writeln ( '$ toJsonMapVarName [$ safeJsonKeyString ] = '
193
- '${ _serializeField ( field , accessOverride : safeFieldAccess )} ;' );
195
+ buffer
196
+ . writeln ( '$ toJsonMapVarName [$ safeJsonKeyString ] = $ expression ;' );
194
197
}
195
198
} else {
196
199
if (directWrite) {
@@ -208,8 +211,8 @@ void $toJsonMapHelperName(String key, dynamic value) {
208
211
}''' );
209
212
directWrite = false ;
210
213
}
211
- buffer. writeln ( '$ toJsonMapHelperName ($ safeJsonKeyString , '
212
- '${ _serializeField ( field , accessOverride : safeFieldAccess )} );' );
214
+ buffer
215
+ . writeln ( '$ toJsonMapHelperName ($ safeJsonKeyString , $ expression );' );
213
216
}
214
217
}
215
218
@@ -218,12 +221,14 @@ void $toJsonMapHelperName(String key, dynamic value) {
218
221
buffer.writeln ('}' );
219
222
}
220
223
221
- void _writeToJsonSimple (StringBuffer buffer, Iterable <FieldElement > fields) {
224
+ void _writeToJsonSimple (StringBuffer buffer, Iterable <FieldElement > fields,
225
+ bool classSupportNullable) {
222
226
buffer.writeln ('=> <String, dynamic>{' );
223
227
224
228
var pairs = < String > [];
225
229
for (var field in fields) {
226
- pairs.add ('${_safeNameAccess (field )}: ${_serializeField (field )}' );
230
+ pairs.add (
231
+ '${_safeNameAccess (field )}: ${_serializeField (field , classSupportNullable )}' );
227
232
}
228
233
buffer.writeAll (pairs, ',\n ' );
229
234
@@ -235,7 +240,8 @@ void $toJsonMapHelperName(String key, dynamic value) {
235
240
StringBuffer buffer,
236
241
ClassElement classElement,
237
242
Map <String , FieldElement > fields,
238
- String prefix) {
243
+ String prefix,
244
+ bool classSupportNullable) {
239
245
// creating a copy so it can be mutated
240
246
var fieldsToSet = new Map <String , FieldElement >.from (fields);
241
247
var className = classElement.displayName;
@@ -303,7 +309,7 @@ void $toJsonMapHelperName(String key, dynamic value) {
303
309
buffer.write (' new $className (' );
304
310
buffer.writeAll (
305
311
ctorArguments.map ((paramElement) => _deserializeForField (
306
- fields[paramElement.name],
312
+ fields[paramElement.name], classSupportNullable,
307
313
ctorParam: paramElement)),
308
314
', ' );
309
315
if (ctorArguments.isNotEmpty && ctorNamedArguments.isNotEmpty) {
@@ -312,7 +318,8 @@ void $toJsonMapHelperName(String key, dynamic value) {
312
318
buffer.writeAll (
313
319
ctorNamedArguments.map ((paramElement) =>
314
320
'${paramElement .name }: ' +
315
- _deserializeForField (fields[paramElement.name],
321
+ _deserializeForField (
322
+ fields[paramElement.name], classSupportNullable,
316
323
ctorParam: paramElement)),
317
324
', ' );
318
325
@@ -323,7 +330,7 @@ void $toJsonMapHelperName(String key, dynamic value) {
323
330
for (var field in fieldsToSet.values) {
324
331
buffer.writeln ();
325
332
buffer.write (' ..${field .name } = ' );
326
- buffer.write (_deserializeForField (field));
333
+ buffer.write (_deserializeForField (field, classSupportNullable ));
327
334
}
328
335
buffer.writeln (';' );
329
336
}
@@ -335,10 +342,12 @@ void $toJsonMapHelperName(String key, dynamic value) {
335
342
Iterable <TypeHelper > get _allHelpers =>
336
343
[_typeHelpers, _coreHelpers].expand ((e) => e);
337
344
338
- String _serializeField (FieldElement field, {String accessOverride}) {
345
+ String _serializeField (FieldElement field, bool classIncludeNullable,
346
+ {String accessOverride}) {
339
347
accessOverride ?? = field.name;
340
348
try {
341
- return _serialize (field.type, accessOverride, _nullable (field));
349
+ return _serialize (
350
+ field.type, accessOverride, _nullable (field, classIncludeNullable));
342
351
} on UnsupportedTypeError {
343
352
throw new InvalidGenerationSourceError (
344
353
'Could not generate `toJson` code for '
@@ -356,14 +365,15 @@ void $toJsonMapHelperName(String key, dynamic value) {
356
365
orElse: () =>
357
366
throw new UnsupportedTypeError (targetType, expression));
358
367
359
- String _deserializeForField (FieldElement field,
368
+ String _deserializeForField (FieldElement field, bool classSupportNullable,
360
369
{ParameterElement ctorParam}) {
361
370
var jsonKey = _safeNameAccess (field);
362
371
363
372
var targetType = ctorParam? .type ?? field.type;
364
373
365
374
try {
366
- return _deserialize (targetType, 'json[$jsonKey ]' , _nullable (field));
375
+ return _deserialize (
376
+ targetType, 'json[$jsonKey ]' , _nullable (field, classSupportNullable));
367
377
} on UnsupportedTypeError {
368
378
throw new InvalidGenerationSourceError (
369
379
'Could not generate fromJson code for '
@@ -390,10 +400,11 @@ String _safeNameAccess(FieldElement field) {
390
400
/// Returns `true` if the field should be treated as potentially nullable.
391
401
///
392
402
/// If no [JsonKey] annotation is present on the field, `true` is returned.
393
- bool _nullable (FieldElement field) => _jsonKeyFor (field).nullable;
403
+ bool _nullable (FieldElement field, bool parentValue) =>
404
+ _jsonKeyFor (field).nullable ?? parentValue;
394
405
395
- bool _includeIfNull (FieldElement element , bool parentValue) =>
396
- _jsonKeyFor (element ).includeIfNull ?? parentValue;
406
+ bool _includeIfNull (FieldElement field , bool parentValue) =>
407
+ _jsonKeyFor (field ).includeIfNull ?? parentValue;
397
408
398
409
JsonKey _jsonKeyFor (FieldElement element) {
399
410
var key = _jsonKeyExpando[element];
@@ -416,6 +427,13 @@ JsonKey _jsonKeyFor(FieldElement element) {
416
427
return key;
417
428
}
418
429
430
+ JsonSerializable _valueForAnnotation (ConstantReader annotation) =>
431
+ new JsonSerializable (
432
+ createToJson: annotation.read ('createToJson' ).boolValue,
433
+ createFactory: annotation.read ('createFactory' ).boolValue,
434
+ nullable: annotation.read ('nullable' ).boolValue,
435
+ includeIfNull: annotation.read ('includeIfNull' ).boolValue);
436
+
419
437
final _jsonKeyExpando = new Expando <JsonKey >();
420
438
421
439
final _jsonKeyChecker = new TypeChecker .fromRuntime (JsonKey );
0 commit comments