Skip to content

Commit 8db00c6

Browse files
committed
Extended json_serializable to use special annotation for skipping enum values
1 parent 738f7b9 commit 8db00c6

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

json_serializable/lib/src/json_key_utils.dart

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import 'utils.dart';
1818
final _jsonKeyExpando = Expando<Map<ClassConfig, KeyConfig>>();
1919

2020
KeyConfig jsonKeyForField(FieldElement field, ClassConfig classAnnotation) =>
21-
(_jsonKeyExpando[field] ??= Map.identity())[classAnnotation] ??=
22-
_from(field, classAnnotation);
21+
(_jsonKeyExpando[field] ??= Map.identity())[classAnnotation] ??= _from(field, classAnnotation);
2322

2423
KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
2524
// If an annotation exists on `element` the source is a 'real' field.
@@ -145,10 +144,7 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
145144

146145
final functionValue = objectValue.toFunctionValue()!;
147146

148-
final invokeConst =
149-
functionValue is ConstructorElement && functionValue.isConst
150-
? 'const '
151-
: '';
147+
final invokeConst = functionValue is ConstructorElement && functionValue.isConst ? 'const ' : '';
152148

153149
return '$invokeConst${functionValue.qualifiedName}()';
154150
}
@@ -172,6 +168,8 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
172168

173169
if (_nullAsUnknownChecker.isExactlyType(annotationType)) {
174170
return jsonKeyNullForUndefinedEnumValueFieldName;
171+
} else if (_skipAsUnknownChecker.isExactlyType(annotationType)) {
172+
return jsonKeySkipForUndefinedEnumValueFieldName;
175173
} else if (!_interfaceTypesEqual(annotationType, targetEnumType)) {
176174
throwUnsupported(
177175
element,
@@ -191,11 +189,9 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
191189
);
192190
}
193191

194-
final enumValueNames =
195-
enumFields.map((p) => p.name).toList(growable: false);
192+
final enumValueNames = enumFields.map((p) => p.name).toList(growable: false);
196193

197-
final enumValueName =
198-
enumValueForDartObject<String>(objectValue, enumValueNames, (n) => n);
194+
final enumValueName = enumValueForDartObject<String>(objectValue, enumValueNames, (n) => n);
199195

200196
return '${annotationType.element!.name}.$enumValueName';
201197
} else {
@@ -233,8 +229,7 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
233229
String? readValueFunctionName;
234230
final readValue = obj.read('readValue');
235231
if (!readValue.isNull) {
236-
readValueFunctionName =
237-
readValue.objectValue.toFunctionValue()!.qualifiedName;
232+
readValueFunctionName = readValue.objectValue.toFunctionValue()!.qualifiedName;
238233
}
239234

240235
final ignore = obj.read('ignore').literalValue as bool?;
@@ -269,8 +264,7 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
269264
name: obj.read('name').literalValue as String?,
270265
readValueFunctionName: readValueFunctionName,
271266
required: obj.read('required').literalValue as bool?,
272-
unknownEnumValue:
273-
createAnnotationValue('unknownEnumValue', mustBeEnum: true),
267+
unknownEnumValue: createAnnotationValue('unknownEnumValue', mustBeEnum: true),
274268
includeToJson: includeToJson,
275269
includeFromJson: includeFromJson,
276270
);
@@ -301,8 +295,7 @@ KeyConfig _populateJsonKey(
301295
return KeyConfig(
302296
defaultValue: defaultValue,
303297
disallowNullValue: disallowNullValue ?? false,
304-
includeIfNull: _includeIfNull(
305-
includeIfNull, disallowNullValue, classAnnotation.includeIfNull),
298+
includeIfNull: _includeIfNull(includeIfNull, disallowNullValue, classAnnotation.includeIfNull),
306299
name: name ?? encodedFieldName(classAnnotation.fieldRename, element.name),
307300
readValueFunctionName: readValueFunctionName,
308301
required: required ?? false,
@@ -332,8 +325,10 @@ bool _interfaceTypesEqual(DartType a, DartType b) {
332325
return a == b;
333326
}
334327

335-
const jsonKeyNullForUndefinedEnumValueFieldName =
336-
'JsonKey.nullForUndefinedEnumValue';
328+
const jsonKeyNullForUndefinedEnumValueFieldName = 'JsonKey.nullForUndefinedEnumValue';
337329

338-
final _nullAsUnknownChecker =
339-
TypeChecker.fromRuntime(JsonKey.nullForUndefinedEnumValue.runtimeType);
330+
final _nullAsUnknownChecker = TypeChecker.fromRuntime(JsonKey.nullForUndefinedEnumValue.runtimeType);
331+
332+
const jsonKeySkipForUndefinedEnumValueFieldName = 'JsonKey.skipForUndefinedEnumValue';
333+
334+
final _skipAsUnknownChecker = TypeChecker.fromRuntime(JsonKey.skipForUndefinedEnumValue.runtimeType);

0 commit comments

Comments
 (0)