@@ -128,12 +128,13 @@ class JsonSerializableGenerator
128
128
});
129
129
130
130
buffer.writeln (' Map<String, dynamic> toJson() ' );
131
- if (fieldsList.every ((fe) => _jsonKeyFor (fe).includeIfNull)) {
131
+ if (fieldsList
132
+ .every ((e) => _includeIfNull (e, annotation.includeIfNull))) {
132
133
// write simple `toJson` method that includes all keys...
133
134
_writeToJsonSimple (buffer, fields);
134
135
} else {
135
136
// At least one field should be excluded if null
136
- _writeToJsonWithNullChecks (buffer, fields);
137
+ _writeToJsonWithNullChecks (buffer, fields, annotation.includeIfNull );
137
138
}
138
139
139
140
// end of the mixin class
@@ -143,8 +144,8 @@ class JsonSerializableGenerator
143
144
return buffer.toString ();
144
145
}
145
146
146
- void _writeToJsonWithNullChecks (
147
- StringBuffer buffer, Map <String , FieldElement > fields) {
147
+ void _writeToJsonWithNullChecks (StringBuffer buffer,
148
+ Map <String , FieldElement > fields, bool includeIfNull ) {
148
149
buffer.writeln ('{' );
149
150
150
151
// TODO(kevmoo) We could write all values up to the null-excluded value
@@ -159,21 +160,21 @@ class JsonSerializableGenerator
159
160
160
161
fields.forEach ((fieldName, field) {
161
162
try {
162
- var jsonKey = _jsonKeyFor (field);
163
- var safeJsonKeyString = _safeNameAccess (jsonKey.name ?? fieldName);
163
+ var safeJsonKeyString =
164
+ _safeNameAccess (_fieldToJsonMapKey (field, fieldName) );
164
165
165
166
// If `fieldName` collides with one of the local helpers, prefix
166
167
// access with `this.`.
167
168
if (fieldName == toJsonMapVarName || fieldName == toJsonMapHelperName) {
168
169
fieldName = 'this.$fieldName ' ;
169
170
}
170
171
171
- if (jsonKey. includeIfNull) {
172
+ if (_includeIfNull (field, includeIfNull) ) {
172
173
buffer.writeln ("$toJsonMapVarName [$safeJsonKeyString ] = "
173
- "${_serialize (field .type , fieldName , jsonKey . nullable )};" );
174
+ "${_serialize (field .type , fieldName , _nullable ( field ) )};" );
174
175
} else {
175
- buffer.writeln ("$toJsonMapHelperName ($safeJsonKeyString ,"
176
- "${_serialize (field .type , fieldName , jsonKey . nullable )});" );
176
+ buffer.writeln ("$toJsonMapHelperName ($safeJsonKeyString , "
177
+ "${_serialize (field .type , fieldName , _nullable ( field ) )});" );
177
178
}
178
179
} on UnsupportedTypeError {
179
180
throw new InvalidGenerationSourceError (
@@ -195,7 +196,7 @@ class JsonSerializableGenerator
195
196
var pairs = < String > [];
196
197
fields.forEach ((fieldName, field) {
197
198
try {
198
- pairs.add ("'${_fieldToJsonMapKey (field ) ?? fieldName }': "
199
+ pairs.add ("'${_fieldToJsonMapKey (field , fieldName ) }': "
199
200
"${_serialize (field .type , fieldName , _nullable (field ))}" );
200
201
} on UnsupportedTypeError {
201
202
throw new InvalidGenerationSourceError (
@@ -321,7 +322,7 @@ class JsonSerializableGenerator
321
322
322
323
String _deserializeForField (String name, FieldElement field,
323
324
{ParameterElement ctorParam}) {
324
- name = _fieldToJsonMapKey (field) ?? name;
325
+ name = _fieldToJsonMapKey (field, name) ;
325
326
326
327
var targetType = ctorParam? .type ?? field.type;
327
328
@@ -350,13 +351,17 @@ String _safeNameAccess(String name) =>
350
351
/// Returns the JSON map `key` to be used when (de)serializing [field] , if any.
351
352
///
352
353
/// Otherwise, `null` ;
353
- String _fieldToJsonMapKey (FieldElement field) => _jsonKeyFor (field).name;
354
+ String _fieldToJsonMapKey (FieldElement field, String ifNull) =>
355
+ _jsonKeyFor (field).name ?? ifNull;
354
356
355
357
/// Returns `true` if the field should be treated as potentially nullable.
356
358
///
357
359
/// If no [JsonKey] annotation is present on the field, `true` is returned.
358
360
bool _nullable (FieldElement field) => _jsonKeyFor (field).nullable;
359
361
362
+ bool _includeIfNull (FieldElement element, bool parentValue) =>
363
+ _jsonKeyFor (element).includeIfNull ?? parentValue;
364
+
360
365
JsonKey _jsonKeyFor (FieldElement element) {
361
366
var key = _jsonKeyExpando[element];
362
367
0 commit comments