diff --git a/_test_yaml/test/src/build_config.g.dart b/_test_yaml/test/src/build_config.g.dart index e4d832e78..42127cb29 100644 --- a/_test_yaml/test/src/build_config.g.dart +++ b/_test_yaml/test/src/build_config.g.dart @@ -25,7 +25,8 @@ Config _$ConfigFromJson(Map json) => $checkedCreate( 'weights', (v) => val.weights = (v as Map?)?.map( (k, e) => MapEntry( - $enumDecode(_$AutoApplyEnumMap, k), (e as num?)?.toInt()), + $enumDecodeWithDecodeMap(_$AutoApplyEnumDecodeMap, k), + (e as num?)?.toInt()), )); return val; }, @@ -37,6 +38,7 @@ Map _$ConfigToJson(Config instance) => { instance.weights?.map((k, e) => MapEntry(_$AutoApplyEnumMap[k]!, e)), }; +// ignore: unused_element const _$AutoApplyEnumMap = { AutoApply.none: 'none', AutoApply.dependents: 'dependents', @@ -44,6 +46,14 @@ const _$AutoApplyEnumMap = { AutoApply.rootPackage: 'root_package', }; +// ignore: unused_element +const _$AutoApplyEnumDecodeMap = { + 'none': AutoApply.none, + 'dependents': AutoApply.dependents, + 'all_packages': AutoApply.allPackages, + 'root_package': AutoApply.rootPackage, +}; + Builder _$BuilderFromJson(Map json) => $checkedCreate( 'Builder', json, @@ -70,11 +80,17 @@ Builder _$BuilderFromJson(Map json) => $checkedCreate( target: $checkedConvert('target', (v) => v as String?), isOptional: $checkedConvert('is_optional', (v) => v as bool?), autoApply: $checkedConvert( - 'auto_apply', (v) => $enumDecodeNullable(_$AutoApplyEnumMap, v)), + 'auto_apply', + (v) => $enumDecodeNullableWithDecodeMap( + _$AutoApplyEnumDecodeMap, v)), buildTo: $checkedConvert( - 'build_to', (v) => $enumDecodeNullable(_$BuildToEnumMap, v)), - defaultEnumTest: $checkedConvert('defaultEnumTest', - (v) => $enumDecodeNullable(_$AutoApplyEnumMap, v)), + 'build_to', + (v) => + $enumDecodeNullableWithDecodeMap(_$BuildToEnumDecodeMap, v)), + defaultEnumTest: $checkedConvert( + 'defaultEnumTest', + (v) => $enumDecodeNullableWithDecodeMap( + _$AutoApplyEnumDecodeMap, v)), builderFactories: $checkedConvert('builder_factories', (v) => (v as List).map((e) => e as String).toList()), appliesBuilders: $checkedConvert('applies_builders', @@ -121,7 +137,14 @@ Map _$BuilderToJson(Builder instance) => { if (instance.buildExtensions case final value?) 'build_extensions': value, }; +// ignore: unused_element const _$BuildToEnumMap = { BuildTo.cache: 'cache', BuildTo.source: 'source', }; + +// ignore: unused_element +const _$BuildToEnumDecodeMap = { + 'cache': BuildTo.cache, + 'source': BuildTo.source, +}; diff --git a/json_annotation/lib/src/enum_helpers.dart b/json_annotation/lib/src/enum_helpers.dart index c0c5cb3ed..2e937f2ad 100644 --- a/json_annotation/lib/src/enum_helpers.dart +++ b/json_annotation/lib/src/enum_helpers.dart @@ -51,6 +51,53 @@ K? $enumDecodeNullable( return unknownValue; } +/// Returns the key associated with value [source] from [decodeMap], if one +/// exists. +/// +/// If [unknownValue] is not `null` and [source] is not a value in [decodeMap], +/// [unknownValue] is returned. Otherwise, an [ArgumentError] is thrown. +/// +/// If [source] is `null`, `null` is returned. +/// +/// Exposed only for code generated by `package:json_serializable`. +/// Not meant to be used directly by user code. +V? $enumDecodeNullableWithDecodeMap( + Map decodeMap, + Object? source, { + Enum? unknownValue, +}) { + if (source == null) { + return null; + } + + final decodedValue = decodeMap[source]; + + if (decodedValue != null) { + return decodedValue; + } + + if (unknownValue == JsonKey.nullForUndefinedEnumValue) { + return null; + } + + if (unknownValue == null) { + throw ArgumentError( + '`$source` is not one of the supported values: ' + '${decodeMap.keys.join(', ')}', + ); + } + + if (unknownValue is! V) { + throw ArgumentError.value( + unknownValue, + 'unknownValue', + 'Must by of type `$K` or `JsonKey.nullForUndefinedEnumValue`.', + ); + } + + return unknownValue; +} + /// Returns the key associated with value [source] from [enumValues], if one /// exists. /// @@ -88,3 +135,41 @@ K $enumDecode( return unknownValue; } + +/// Returns the key associated with value [source] from [decodeMap], if one +/// exists. +/// +/// If [unknownValue] is not `null` and [source] is not a value in [decodeMap], +/// [unknownValue] is returned. Otherwise, an [ArgumentError] is thrown. +/// +/// If [source] is `null`, an [ArgumentError] is thrown. +/// +/// Exposed only for code generated by `package:json_serializable`. +/// Not meant to be used directly by user code. +V $enumDecodeWithDecodeMap( + Map decodeMap, + Object? source, { + V? unknownValue, +}) { + if (source == null) { + throw ArgumentError( + 'A value must be provided. Supported values: ' + '${decodeMap.keys.join(', ')}', + ); + } + + final decodedValue = decodeMap[source]; + + if (decodedValue != null) { + return decodedValue; + } + + if (unknownValue == null) { + throw ArgumentError( + '`$source` is not one of the supported values: ' + '${decodeMap.keys.join(', ')}', + ); + } + + return unknownValue; +} diff --git a/json_annotation/lib/src/json_value.dart b/json_annotation/lib/src/json_value.dart index 4e5cb4f2b..243d85a03 100644 --- a/json_annotation/lib/src/json_value.dart +++ b/json_annotation/lib/src/json_value.dart @@ -9,5 +9,10 @@ class JsonValue { /// Can be a [String] or an [int]. final dynamic value; - const JsonValue(this.value); + /// Optional values that can be used when deserializing. + /// + /// The elements of [aliases] must be either [String] or [int]. + final Set aliases; + + const JsonValue(this.value, {this.aliases = const {}}); } diff --git a/json_serializable/lib/src/enum_utils.dart b/json_serializable/lib/src/enum_utils.dart index d5aa4d2d2..44f68632e 100644 --- a/json_serializable/lib/src/enum_utils.dart +++ b/json_serializable/lib/src/enum_utils.dart @@ -15,6 +15,9 @@ import 'utils.dart'; String constMapName(DartType targetType) => '_\$${targetType.element!.name}EnumMap'; +String constDecodeMapName(DartType targetType) => + '_\$${targetType.element!.name}EnumDecodeMap'; + /// If [targetType] is not an enum, return `null`. /// /// Otherwise, returns `true` if [targetType] is nullable OR if one of the @@ -31,21 +34,45 @@ bool? enumFieldWithNullInEncodeMap(DartType targetType) { return enumMap.values.contains(null); } -String? enumValueMapFromType( +String? enumMapsFromType( DartType targetType, { bool nullWithNoAnnotation = false, }) { final enumMap = _enumMap(targetType, nullWithNoAnnotation: nullWithNoAnnotation); - if (enumMap == null) return null; - - final items = enumMap.entries - .map((e) => ' ${targetType.element!.name}.${e.key.name}: ' - '${jsonLiteralAsDart(e.value)},') - .join(); - - return 'const ${constMapName(targetType)} = {\n$items\n};'; + final enumAliases = + _enumAliases(targetType, nullWithNoAnnotation: nullWithNoAnnotation); + + final valuesItems = enumMap == null + ? null + : [ + for (final MapEntry(:key, :value) in enumMap.entries) + ' ${targetType.element!.name}.${key.name}: ' + '${jsonLiteralAsDart(value)},', + ].join(); + + final valuesMap = valuesItems == null + ? null + : '// ignore: unused_element\n' + 'const ${constMapName(targetType)} = {\n$valuesItems\n};'; + + final decodeItems = enumAliases == null + ? null + : [ + for (final MapEntry(:key, :value) in enumAliases.entries) + ' ${jsonLiteralAsDart(key)}: ' + '${targetType.element!.name}.${value.name},', + ].join(); + + final decodeMap = decodeItems == null + ? null + : '// ignore: unused_element\n' + 'const ${constDecodeMapName(targetType)} = {\n$decodeItems\n};'; + + return valuesMap == null && decodeMap == null + ? null + : [valuesMap, decodeMap].join('\n\n'); } Map? _enumMap( @@ -73,6 +100,34 @@ Map? _enumMap( }; } +Map? _enumAliases( + DartType targetType, { + bool nullWithNoAnnotation = false, +}) { + final targetTypeElement = targetType.element; + if (targetTypeElement == null) return null; + final annotation = _jsonEnumChecker.firstAnnotationOf(targetTypeElement); + final jsonEnum = _fromAnnotation(annotation); + + final enumFields = iterateEnumFields(targetType); + + if (enumFields == null || (nullWithNoAnnotation && !jsonEnum.alwaysCreate)) { + return null; + } + + return { + for (var field in enumFields) ...{ + _generateEntry( + field: field, + jsonEnum: jsonEnum, + targetType: targetType, + ): field, + for (var alias in _generateAliases(field: field, targetType: targetType)) + alias: field, + }, + }; +} + Object? _generateEntry({ required FieldElement field, required JsonEnum jsonEnum, @@ -138,6 +193,36 @@ Object? _generateEntry({ } } +List _generateAliases({ + required FieldElement field, + required DartType targetType, +}) { + final annotation = + const TypeChecker.fromRuntime(JsonValue).firstAnnotationOfExact(field); + + if (annotation == null) { + return const []; + } else { + final reader = ConstantReader(annotation); + + final valueReader = reader.read('aliases'); + + if (valueReader.validAliasesType) { + return [ + for (final value in valueReader.setValue) + ConstantReader(value).literalValue, + ]; + } else { + final targetTypeCode = typeToCode(targetType); + throw InvalidGenerationSourceError( + 'The `JsonValue` annotation on `$targetTypeCode.${field.name}` aliases ' + 'should all be of type String or int.', + element: field, + ); + } + } +} + const _jsonEnumChecker = TypeChecker.fromRuntime(JsonEnum); JsonEnum _fromAnnotation(DartObject? dartObject) { @@ -154,4 +239,10 @@ JsonEnum _fromAnnotation(DartObject? dartObject) { extension on ConstantReader { bool get validValueType => isString || isNull || isInt; + + bool get validAliasesType => + isSet && + setValue.every((element) => + (element.type?.isDartCoreString ?? false) || + (element.type?.isDartCoreInt ?? false)); } diff --git a/json_serializable/lib/src/json_enum_generator.dart b/json_serializable/lib/src/json_enum_generator.dart index e72657add..ffce56d9c 100644 --- a/json_serializable/lib/src/json_enum_generator.dart +++ b/json_serializable/lib/src/json_enum_generator.dart @@ -26,7 +26,7 @@ class JsonEnumGenerator extends GeneratorForAnnotation { } final value = - enumValueMapFromType(element.thisType, nullWithNoAnnotation: true); + enumMapsFromType(element.thisType, nullWithNoAnnotation: true); return [ if (value != null) value, diff --git a/json_serializable/lib/src/type_helpers/enum_helper.dart b/json_serializable/lib/src/type_helpers/enum_helper.dart index ce7641fdf..6d689e10b 100644 --- a/json_serializable/lib/src/type_helpers/enum_helper.dart +++ b/json_serializable/lib/src/type_helpers/enum_helper.dart @@ -21,7 +21,7 @@ class EnumHelper extends TypeHelper { String expression, TypeHelperContextWithConfig context, ) { - final memberContent = enumValueMapFromType(targetType); + final memberContent = enumMapsFromType(targetType); if (memberContent == null) { return null; @@ -44,7 +44,7 @@ class EnumHelper extends TypeHelper { TypeHelperContextWithConfig context, bool defaultProvided, ) { - final memberContent = enumValueMapFromType(targetType); + final memberContent = enumMapsFromType(targetType); if (memberContent == null) { return null; @@ -64,15 +64,15 @@ class EnumHelper extends TypeHelper { String functionName; if (targetType.isNullableType || defaultProvided) { - functionName = r'$enumDecodeNullable'; + functionName = r'$enumDecodeNullableWithDecodeMap'; } else { - functionName = r'$enumDecode'; + functionName = r'$enumDecodeWithDecodeMap'; } context.addMember(memberContent); final args = [ - constMapName(targetType), + constDecodeMapName(targetType), expression, if (jsonKey.unknownEnumValue != null) 'unknownValue: ${jsonKey.unknownEnumValue}', diff --git a/json_serializable/test/default_value/default_value.g.dart b/json_serializable/test/default_value/default_value.g.dart index 6bc4525d5..6e49d0c6c 100644 --- a/json_serializable/test/default_value/default_value.g.dart +++ b/json_serializable/test/default_value/default_value.g.dart @@ -35,7 +35,9 @@ DefaultValue _$DefaultValueFromJson(Map json) => DefaultValue( { 'root': ['child'] }, - $enumDecodeNullable(_$GreekEnumMap, json['fieldEnum']) ?? Greek.beta, + $enumDecodeNullableWithDecodeMap( + _$GreekEnumDecodeMap, json['fieldEnum']) ?? + Greek.beta, durationField: json['durationField'] == null ? Duration.zero : Duration(microseconds: (json['durationField'] as num).toInt()), @@ -92,9 +94,18 @@ Map _$DefaultValueToJson(DefaultValue instance) => instance.valueFromDefaultValueNamedConstructor, }; +// ignore: unused_element const _$GreekEnumMap = { Greek.alpha: 'alpha', Greek.beta: 'beta', Greek.gamma: 'gamma', Greek.delta: 'delta', }; + +// ignore: unused_element +const _$GreekEnumDecodeMap = { + 'alpha': Greek.alpha, + 'beta': Greek.beta, + 'gamma': Greek.gamma, + 'delta': Greek.delta, +}; diff --git a/json_serializable/test/default_value/default_value.g_any_map__checked.g.dart b/json_serializable/test/default_value/default_value.g_any_map__checked.g.dart index 9ce12321f..f63574106 100644 --- a/json_serializable/test/default_value/default_value.g_any_map__checked.g.dart +++ b/json_serializable/test/default_value/default_value.g_any_map__checked.g.dart @@ -51,8 +51,11 @@ DefaultValue _$DefaultValueFromJson(Map json) => $checkedCreate( { 'root': ['child'] }), - $checkedConvert('fieldEnum', - (v) => $enumDecodeNullable(_$GreekEnumMap, v) ?? Greek.beta), + $checkedConvert( + 'fieldEnum', + (v) => + $enumDecodeNullableWithDecodeMap(_$GreekEnumDecodeMap, v) ?? + Greek.beta), durationField: $checkedConvert( 'durationField', (v) => v == null @@ -117,9 +120,18 @@ Map _$DefaultValueToJson(DefaultValue instance) => instance.valueFromDefaultValueNamedConstructor, }; +// ignore: unused_element const _$GreekEnumMap = { Greek.alpha: 'alpha', Greek.beta: 'beta', Greek.gamma: 'gamma', Greek.delta: 'delta', }; + +// ignore: unused_element +const _$GreekEnumDecodeMap = { + 'alpha': Greek.alpha, + 'beta': Greek.beta, + 'gamma': Greek.gamma, + 'delta': Greek.delta, +}; diff --git a/json_serializable/test/default_value/implicit_default_value.g.dart b/json_serializable/test/default_value/implicit_default_value.g.dart index 408c1f0b2..701135858 100644 --- a/json_serializable/test/default_value/implicit_default_value.g.dart +++ b/json_serializable/test/default_value/implicit_default_value.g.dart @@ -39,8 +39,9 @@ DefaultValueImplicit _$DefaultValueImplicitFromJson( const { 'root': ['child'] }, - fieldEnum: - $enumDecodeNullable(_$GreekEnumMap, json['fieldEnum']) ?? Greek.beta, + fieldEnum: $enumDecodeNullableWithDecodeMap( + _$GreekEnumDecodeMap, json['fieldEnum']) ?? + Greek.beta, durationField: json['durationField'] == null ? const Duration() : Duration(microseconds: (json['durationField'] as num).toInt()), @@ -97,9 +98,18 @@ Map _$DefaultValueImplicitToJson( instance.valueFromDefaultValueNamedConstructor, }; +// ignore: unused_element const _$GreekEnumMap = { Greek.alpha: 'alpha', Greek.beta: 'beta', Greek.gamma: 'gamma', Greek.delta: 'delta', }; + +// ignore: unused_element +const _$GreekEnumDecodeMap = { + 'alpha': Greek.alpha, + 'beta': Greek.beta, + 'gamma': Greek.gamma, + 'delta': Greek.delta, +}; diff --git a/json_serializable/test/integration/converter_examples.g.dart b/json_serializable/test/integration/converter_examples.g.dart index f19c8fc2e..6c4fc4d21 100644 --- a/json_serializable/test/integration/converter_examples.g.dart +++ b/json_serializable/test/integration/converter_examples.g.dart @@ -11,7 +11,8 @@ part of 'converter_examples.dart'; Issue1202RegressionClass _$Issue1202RegressionClassFromJson( Map json) => Issue1202RegressionClass( - value: $enumDecode(_$Issue1202RegressionEnumEnumMap, json['value']), + value: $enumDecodeWithDecodeMap( + _$Issue1202RegressionEnumEnumDecodeMap, json['value']), normalNullableValue: (json['normalNullableValue'] as num?)?.toInt(), notNullableValueWithNullableConverter: const _Issue1202RegressionConverter().fromJson( @@ -47,11 +48,18 @@ Map _$Issue1202RegressionClassToJson( 'valueWithNullableFunctions': value, }; +// ignore: unused_element const _$Issue1202RegressionEnumEnumMap = { Issue1202RegressionEnum.normalValue: 42, Issue1202RegressionEnum.nullValue: null, }; +// ignore: unused_element +const _$Issue1202RegressionEnumEnumDecodeMap = { + 42: Issue1202RegressionEnum.normalValue, + null: Issue1202RegressionEnum.nullValue, +}; + Regression1229 _$Regression1229FromJson(Map json) => Regression1229( date: _$JsonConverterFromJson( diff --git a/json_serializable/test/integration/create_per_field_to_json_example.g.dart b/json_serializable/test/integration/create_per_field_to_json_example.g.dart index 28f3443a5..a862dd892 100644 --- a/json_serializable/test/integration/create_per_field_to_json_example.g.dart +++ b/json_serializable/test/integration/create_per_field_to_json_example.g.dart @@ -11,7 +11,8 @@ part of 'create_per_field_to_json_example.dart'; Model _$ModelFromJson(Map json) => Model( firstName: json['firstName'] as String, lastName: json['lastName'] as String, - enumValue: $enumDecodeNullable(_$EnumValueEnumMap, json['enumValue']), + enumValue: $enumDecodeNullableWithDecodeMap( + _$EnumValueEnumDecodeMap, json['enumValue']), nested: json['nested'] == null ? null : Nested.fromJson(json['nested'] as Map), @@ -57,11 +58,18 @@ Map _$ModelToJson(Model instance) => { 'nestedExcludeIfNull': value, }; +// ignore: unused_element const _$EnumValueEnumMap = { EnumValue.first: '1', EnumValue.second: 'second', }; +// ignore: unused_element +const _$EnumValueEnumDecodeMap = { + '1': EnumValue.first, + 'second': EnumValue.second, +}; + Nested _$NestedFromJson(Map json) => Nested( json['value'] as String, ); diff --git a/json_serializable/test/integration/integration_test.dart b/json_serializable/test/integration/integration_test.dart index 6aef150f2..85009e7a2 100644 --- a/json_serializable/test/integration/integration_test.dart +++ b/json_serializable/test/integration/integration_test.dart @@ -481,4 +481,37 @@ void main() { test('ModelJsonKeys', () { expect(js_keys.keys, {'first-name', 'LAST_NAME'}); }); + + group('JsonValue aliases', () { + test('should decode any one of the aliases values', () { + const jsonData = {'value': '2'}; + expect( + EnumWithAliasesData.fromJson(jsonData).value, + EnumWithAliases.two, + ); + + const jsonData2 = {'value': 'two'}; + expect( + EnumWithAliasesData.fromJson(jsonData2).value, + EnumWithAliases.two, + ); + + const jsonData3 = {'value': 'deux'}; + expect( + EnumWithAliasesData.fromJson(jsonData3).value, + EnumWithAliases.two, + ); + + const jsonInvalidData = {'value': 'modmao'}; + expect( + () => EnumWithAliasesData.fromJson(jsonInvalidData), + throwsA(isA()), + ); + }); + + test('should encode to the non-alias value', () { + final jsonData = EnumWithAliasesData(EnumWithAliases.two).toJson(); + expect(jsonData, {'value': '2'}); + }); + }); } diff --git a/json_serializable/test/integration/json_enum_example.dart b/json_serializable/test/integration/json_enum_example.dart index 9f8cee0a8..f0b81f7d7 100644 --- a/json_serializable/test/integration/json_enum_example.dart +++ b/json_serializable/test/integration/json_enum_example.dart @@ -121,3 +121,26 @@ class Issue1226Regression { Map toJson() => _$Issue1226RegressionToJson(this); } + +enum EnumWithAliases { + @JsonValue('1') + one, + + @JsonValue('2', aliases: {'two', 'deux'}) + two, + + @JsonValue('3') + three; +} + +@JsonSerializable() +class EnumWithAliasesData { + final EnumWithAliases value; + + EnumWithAliasesData(this.value); + + factory EnumWithAliasesData.fromJson(Map json) => + _$EnumWithAliasesDataFromJson(json); + + Map toJson() => _$EnumWithAliasesDataToJson(this); +} diff --git a/json_serializable/test/integration/json_enum_example.g.dart b/json_serializable/test/integration/json_enum_example.g.dart index 95ae41b17..1e78f169b 100644 --- a/json_serializable/test/integration/json_enum_example.g.dart +++ b/json_serializable/test/integration/json_enum_example.g.dart @@ -15,17 +15,26 @@ Issue559Regression _$Issue559RegressionFromJson(Map json) { disallowNullValues: const ['status'], ); return Issue559Regression( - status: $enumDecodeNullable(_$Issue559RegressionEnumEnumMap, json['status'], + status: $enumDecodeNullableWithDecodeMap( + _$Issue559RegressionEnumEnumDecodeMap, json['status'], unknownValue: JsonKey.nullForUndefinedEnumValue), ); } +// ignore: unused_element const _$Issue559RegressionEnumEnumMap = { Issue559RegressionEnum.alpha: 'alpha', Issue559RegressionEnum.beta: 'beta', Issue559RegressionEnum.gamma: 'gamma', }; +// ignore: unused_element +const _$Issue559RegressionEnumEnumDecodeMap = { + 'alpha': Issue559RegressionEnum.alpha, + 'beta': Issue559RegressionEnum.beta, + 'gamma': Issue559RegressionEnum.gamma, +}; + Map _$Issue1145RegressionAToJson( Issue1145RegressionA instance) => { @@ -33,12 +42,20 @@ Map _$Issue1145RegressionAToJson( .map((k, e) => MapEntry(_$Issue1145RegressionEnumEnumMap[k]!, e)), }; +// ignore: unused_element const _$Issue1145RegressionEnumEnumMap = { Issue1145RegressionEnum.alpha: 'alpha', Issue1145RegressionEnum.beta: 'beta', Issue1145RegressionEnum.gamma: 'gamma', }; +// ignore: unused_element +const _$Issue1145RegressionEnumEnumDecodeMap = { + 'alpha': Issue1145RegressionEnum.alpha, + 'beta': Issue1145RegressionEnum.beta, + 'gamma': Issue1145RegressionEnum.gamma, +}; + Map _$Issue1145RegressionBToJson( Issue1145RegressionB instance) => { @@ -49,8 +66,8 @@ Map _$Issue1145RegressionBToJson( Issue1226Regression _$Issue1226RegressionFromJson(Map json) => Issue1226Regression( - durationType: $enumDecodeNullable( - _$Issue1145RegressionEnumEnumMap, json['durationType']), + durationType: $enumDecodeNullableWithDecodeMap( + _$Issue1145RegressionEnumEnumDecodeMap, json['durationType']), ); Map _$Issue1226RegressionToJson( @@ -61,6 +78,34 @@ Map _$Issue1226RegressionToJson( 'durationType': value, }; +EnumWithAliasesData _$EnumWithAliasesDataFromJson(Map json) => + EnumWithAliasesData( + $enumDecodeWithDecodeMap(_$EnumWithAliasesEnumDecodeMap, json['value']), + ); + +Map _$EnumWithAliasesDataToJson( + EnumWithAliasesData instance) => + { + 'value': _$EnumWithAliasesEnumMap[instance.value]!, + }; + +// ignore: unused_element +const _$EnumWithAliasesEnumMap = { + EnumWithAliases.one: '1', + EnumWithAliases.two: '2', + EnumWithAliases.three: '3', +}; + +// ignore: unused_element +const _$EnumWithAliasesEnumDecodeMap = { + '1': EnumWithAliases.one, + '2': EnumWithAliases.two, + 'two': EnumWithAliases.two, + 'deux': EnumWithAliases.two, + '3': EnumWithAliases.three, +}; + +// ignore: unused_element const _$StandAloneEnumEnumMap = { StandAloneEnum.alpha: 'a', StandAloneEnum.beta: 'b', @@ -68,19 +113,50 @@ const _$StandAloneEnumEnumMap = { StandAloneEnum.delta: 'd', }; +// ignore: unused_element +const _$StandAloneEnumEnumDecodeMap = { + 'a': StandAloneEnum.alpha, + 'b': StandAloneEnum.beta, + 'g': StandAloneEnum.gamma, + 'd': StandAloneEnum.delta, +}; + +// ignore: unused_element const _$DayTypeEnumMap = { DayType.noGood: 'no-good', DayType.rotten: 'rotten', DayType.veryBad: 'very-bad', }; +// ignore: unused_element +const _$DayTypeEnumDecodeMap = { + 'no-good': DayType.noGood, + 'rotten': DayType.rotten, + 'very-bad': DayType.veryBad, +}; + +// ignore: unused_element const _$MyStatusCodeEnumMap = { MyStatusCode.success: 200, MyStatusCode.weird: 701, }; +// ignore: unused_element +const _$MyStatusCodeEnumDecodeMap = { + 200: MyStatusCode.success, + 701: MyStatusCode.weird, +}; + +// ignore: unused_element const _$EnumValueFieldIndexEnumMap = { EnumValueFieldIndex.success: 0, EnumValueFieldIndex.weird: 701, EnumValueFieldIndex.oneMore: 2, }; + +// ignore: unused_element +const _$EnumValueFieldIndexEnumDecodeMap = { + 0: EnumValueFieldIndex.success, + 701: EnumValueFieldIndex.weird, + 2: EnumValueFieldIndex.oneMore, +}; diff --git a/json_serializable/test/integration/json_test_example.g.dart b/json_serializable/test/integration/json_test_example.g.dart index bf56859ed..03f3b53ba 100644 --- a/json_serializable/test/integration/json_test_example.g.dart +++ b/json_serializable/test/integration/json_test_example.g.dart @@ -11,7 +11,7 @@ part of 'json_test_example.dart'; Person _$PersonFromJson(Map json) => Person( json['firstName'] as String, json['lastName'] as String, - $enumDecode(_$CategoryEnumMap, json[r'$house']), + $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, json[r'$house']), middleName: json['middleName'] as String?, dateOfBirth: json['dateOfBirth'] == null ? null @@ -26,11 +26,12 @@ Person _$PersonFromJson(Map json) => Person( .map((e) => Order.fromJson(e as Map)) .toList()) ..houseMap = (json['houseMap'] as Map?)?.map( - (k, e) => MapEntry(k, $enumDecode(_$CategoryEnumMap, e)), + (k, e) => + MapEntry(k, $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, e)), ) ..categoryCounts = (json['categoryCounts'] as Map?)?.map( - (k, e) => - MapEntry($enumDecode(_$CategoryEnumMap, k), (e as num).toInt()), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, k), + (e as num).toInt()), ); Map _$PersonToJson(Person instance) => { @@ -47,6 +48,7 @@ Map _$PersonToJson(Person instance) => { ?.map((k, e) => MapEntry(_$CategoryEnumMap[k]!, e)), }; +// ignore: unused_element const _$CategoryEnumMap = { Category.top: 'top', Category.bottom: 'bottom', @@ -57,13 +59,24 @@ const _$CategoryEnumMap = { Category.notDiscoveredYet: 'not_discovered_yet', }; +// ignore: unused_element +const _$CategoryEnumDecodeMap = { + 'top': Category.top, + 'bottom': Category.bottom, + 'strange': Category.strange, + 'charmed': Category.charmed, + 'up': Category.up, + 'down': Category.down, + 'not_discovered_yet': Category.notDiscoveredYet, +}; + Order _$OrderFromJson(Map json) { $checkKeys( json, disallowNullValues: const ['count'], ); return Order.custom( - $enumDecodeNullable(_$CategoryEnumMap, json['category']), + $enumDecodeNullableWithDecodeMap(_$CategoryEnumDecodeMap, json['category']), (json['items'] as List?) ?.map((e) => Item.fromJson(e as Map)), ) @@ -80,7 +93,8 @@ Order _$OrderFromJson(Map json) { ) ..homepage = json['homepage'] == null ? null : Uri.parse(json['homepage'] as String) - ..statusCode = $enumDecodeNullable(_$StatusCodeEnumMap, json['status_code'], + ..statusCode = $enumDecodeNullableWithDecodeMap( + _$StatusCodeEnumDecodeMap, json['status_code'], unknownValue: StatusCode.unknown) ?? StatusCode.success; } @@ -97,6 +111,7 @@ Map _$OrderToJson(Order instance) => { 'status_code': _$StatusCodeEnumMap[instance.statusCode], }; +// ignore: unused_element const _$StatusCodeEnumMap = { StatusCode.success: 200, StatusCode.notFound: 404, @@ -104,6 +119,14 @@ const _$StatusCodeEnumMap = { StatusCode.unknown: 'unknown', }; +// ignore: unused_element +const _$StatusCodeEnumDecodeMap = { + 200: StatusCode.success, + 404: StatusCode.notFound, + '500': StatusCode.weird, + 'unknown': StatusCode.unknown, +}; + Item _$ItemFromJson(Map json) => Item( (json['price'] as num?)?.toInt(), ) @@ -175,17 +198,18 @@ Map _$MapKeyVarietyToJson(MapKeyVariety instance) => UnknownEnumValue _$UnknownEnumValueFromJson(Map json) => UnknownEnumValue() - ..enumValue = $enumDecode(_$CategoryEnumMap, json['enumValue'], + ..enumValue = $enumDecodeWithDecodeMap( + _$CategoryEnumDecodeMap, json['enumValue'], unknownValue: Category.notDiscoveredYet) ..enumIterable = (json['enumIterable'] as List).map((e) => - $enumDecode(_$CategoryEnumMap, e, + $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, e, unknownValue: Category.notDiscoveredYet)) ..enumList = (json['enumList'] as List) - .map((e) => $enumDecode(_$CategoryEnumMap, e, + .map((e) => $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, e, unknownValue: Category.notDiscoveredYet)) .toList() ..enumSet = (json['enumSet'] as List) - .map((e) => $enumDecode(_$CategoryEnumMap, e, + .map((e) => $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, e, unknownValue: Category.notDiscoveredYet)) .toSet(); diff --git a/json_serializable/test/integration/json_test_example.g_any_map.g.dart b/json_serializable/test/integration/json_test_example.g_any_map.g.dart index ef2ad4f95..b99210a5f 100644 --- a/json_serializable/test/integration/json_test_example.g_any_map.g.dart +++ b/json_serializable/test/integration/json_test_example.g_any_map.g.dart @@ -11,7 +11,7 @@ part of 'json_test_example.g_any_map.dart'; Person _$PersonFromJson(Map json) => Person( json['firstName'] as String, json['lastName'] as String, - $enumDecode(_$CategoryEnumMap, json[r'$house']), + $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, json[r'$house']), middleName: json['middleName'] as String?, dateOfBirth: json['dateOfBirth'] == null ? null @@ -26,11 +26,12 @@ Person _$PersonFromJson(Map json) => Person( .map((e) => Order.fromJson(Map.from(e as Map))) .toList()) ..houseMap = (json['houseMap'] as Map?)?.map( - (k, e) => MapEntry(k as String, $enumDecode(_$CategoryEnumMap, e)), + (k, e) => MapEntry( + k as String, $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, e)), ) ..categoryCounts = (json['categoryCounts'] as Map?)?.map( - (k, e) => - MapEntry($enumDecode(_$CategoryEnumMap, k), (e as num).toInt()), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, k), + (e as num).toInt()), ); Map _$PersonToJson(Person instance) => { @@ -47,6 +48,7 @@ Map _$PersonToJson(Person instance) => { ?.map((k, e) => MapEntry(_$CategoryEnumMap[k]!, e)), }; +// ignore: unused_element const _$CategoryEnumMap = { Category.top: 'top', Category.bottom: 'bottom', @@ -57,13 +59,24 @@ const _$CategoryEnumMap = { Category.notDiscoveredYet: 'not_discovered_yet', }; +// ignore: unused_element +const _$CategoryEnumDecodeMap = { + 'top': Category.top, + 'bottom': Category.bottom, + 'strange': Category.strange, + 'charmed': Category.charmed, + 'up': Category.up, + 'down': Category.down, + 'not_discovered_yet': Category.notDiscoveredYet, +}; + Order _$OrderFromJson(Map json) { $checkKeys( json, disallowNullValues: const ['count'], ); return Order.custom( - $enumDecodeNullable(_$CategoryEnumMap, json['category']), + $enumDecodeNullableWithDecodeMap(_$CategoryEnumDecodeMap, json['category']), (json['items'] as List?) ?.map((e) => Item.fromJson(Map.from(e as Map))), ) @@ -80,7 +93,8 @@ Order _$OrderFromJson(Map json) { ) ..homepage = json['homepage'] == null ? null : Uri.parse(json['homepage'] as String) - ..statusCode = $enumDecodeNullable(_$StatusCodeEnumMap, json['status_code'], + ..statusCode = $enumDecodeNullableWithDecodeMap( + _$StatusCodeEnumDecodeMap, json['status_code'], unknownValue: StatusCode.unknown) ?? StatusCode.success; } @@ -97,6 +111,7 @@ Map _$OrderToJson(Order instance) => { 'status_code': _$StatusCodeEnumMap[instance.statusCode], }; +// ignore: unused_element const _$StatusCodeEnumMap = { StatusCode.success: 200, StatusCode.notFound: 404, @@ -104,6 +119,14 @@ const _$StatusCodeEnumMap = { StatusCode.unknown: 'unknown', }; +// ignore: unused_element +const _$StatusCodeEnumDecodeMap = { + 200: StatusCode.success, + 404: StatusCode.notFound, + '500': StatusCode.weird, + 'unknown': StatusCode.unknown, +}; + Item _$ItemFromJson(Map json) => Item( (json['price'] as num?)?.toInt(), ) @@ -173,17 +196,18 @@ Map _$MapKeyVarietyToJson(MapKeyVariety instance) => }; UnknownEnumValue _$UnknownEnumValueFromJson(Map json) => UnknownEnumValue() - ..enumValue = $enumDecode(_$CategoryEnumMap, json['enumValue'], + ..enumValue = $enumDecodeWithDecodeMap( + _$CategoryEnumDecodeMap, json['enumValue'], unknownValue: Category.notDiscoveredYet) ..enumIterable = (json['enumIterable'] as List).map((e) => - $enumDecode(_$CategoryEnumMap, e, + $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, e, unknownValue: Category.notDiscoveredYet)) ..enumList = (json['enumList'] as List) - .map((e) => $enumDecode(_$CategoryEnumMap, e, + .map((e) => $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, e, unknownValue: Category.notDiscoveredYet)) .toList() ..enumSet = (json['enumSet'] as List) - .map((e) => $enumDecode(_$CategoryEnumMap, e, + .map((e) => $enumDecodeWithDecodeMap(_$CategoryEnumDecodeMap, e, unknownValue: Category.notDiscoveredYet)) .toSet(); diff --git a/json_serializable/test/json_serializable_test.dart b/json_serializable/test/json_serializable_test.dart index b6ff36789..b6ef4a57a 100644 --- a/json_serializable/test/json_serializable_test.dart +++ b/json_serializable/test/json_serializable_test.dart @@ -32,6 +32,7 @@ Future main() async { jsonEnumTestReader, const JsonEnumGenerator(), expectedAnnotatedTests: { + 'EnumAliasNotSupportType', 'EnumValueIssue1147', 'EnumValueNotAField', 'EnumValueNotSupportType', diff --git a/json_serializable/test/src/_json_enum_test_input.dart b/json_serializable/test/src/_json_enum_test_input.dart index 594229f7b..91a797c5a 100644 --- a/json_serializable/test/src/_json_enum_test_input.dart +++ b/json_serializable/test/src/_json_enum_test_input.dart @@ -5,11 +5,28 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:source_gen_test/annotations.dart'; +@ShouldThrow( + 'The `JsonValue` annotation on `EnumAliasNotSupportType.one` aliases should ' + 'all be of type String or int.', +) +@JsonEnum(alwaysCreate: true) +enum EnumAliasNotSupportType { + @JsonValue('one', aliases: {#one}) + one, +} + @ShouldGenerate(r''' +// ignore: unused_element const _$EnumValueIssue1147EnumMap = { EnumValueIssue1147.success: 200, EnumValueIssue1147.weird: 601, }; + +// ignore: unused_element +const _$EnumValueIssue1147EnumDecodeMap = { + 200: EnumValueIssue1147.success, + 601: EnumValueIssue1147.weird, +}; ''') @JsonEnum(alwaysCreate: true, valueField: 'statusCodeNumber') enum EnumValueIssue1147 { diff --git a/json_serializable/test/src/unknown_enum_value_test_input.dart b/json_serializable/test/src/unknown_enum_value_test_input.dart index 7dec6feaa..245bb3f55 100644 --- a/json_serializable/test/src/unknown_enum_value_test_input.dart +++ b/json_serializable/test/src/unknown_enum_value_test_input.dart @@ -6,11 +6,12 @@ part of '_json_serializable_test_input.dart'; r''' UnknownEnumValue _$UnknownEnumValueFromJson(Map json) => UnknownEnumValue() - ..value = $enumDecodeNullable( - _$UnknownEnumValueItemsEnumMap, json['value'], + ..value = $enumDecodeNullableWithDecodeMap( + _$UnknownEnumValueItemsEnumDecodeMap, json['value'], unknownValue: UnknownEnumValueItems.vUnknown) ?? UnknownEnumValueItems.vNull; +// ignore: unused_element const _$UnknownEnumValueItemsEnumMap = { UnknownEnumValueItems.v0: 'v0', UnknownEnumValueItems.v1: 'v1', @@ -18,6 +19,15 @@ const _$UnknownEnumValueItemsEnumMap = { UnknownEnumValueItems.vUnknown: 'vUnknown', UnknownEnumValueItems.vNull: 'vNull', }; + +// ignore: unused_element +const _$UnknownEnumValueItemsEnumDecodeMap = { + 'v0': UnknownEnumValueItems.v0, + 'v1': UnknownEnumValueItems.v1, + 'v2': UnknownEnumValueItems.v2, + 'vUnknown': UnknownEnumValueItems.vUnknown, + 'vNull': UnknownEnumValueItems.vNull, +}; ''', ) @JsonSerializable( diff --git a/json_serializable/test/supported_types/input.type_enumtype.g.dart b/json_serializable/test/supported_types/input.type_enumtype.g.dart index 77d67e064..841d889f1 100644 --- a/json_serializable/test/supported_types/input.type_enumtype.g.dart +++ b/json_serializable/test/supported_types/input.type_enumtype.g.dart @@ -9,8 +9,9 @@ part of 'input.type_enumtype.dart'; // ************************************************************************** SimpleClass _$SimpleClassFromJson(Map json) => SimpleClass( - $enumDecode(_$EnumTypeEnumMap, json['value']), - $enumDecodeNullable(_$EnumTypeEnumMap, json['withDefault']) ?? + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, json['value']), + $enumDecodeNullableWithDecodeMap( + _$EnumTypeEnumDecodeMap, json['withDefault']) ?? EnumType.alpha, ); @@ -20,6 +21,7 @@ Map _$SimpleClassToJson(SimpleClass instance) => 'withDefault': _$EnumTypeEnumMap[instance.withDefault]!, }; +// ignore: unused_element const _$EnumTypeEnumMap = { EnumType.alpha: 'alpha', EnumType.beta: 'beta', @@ -27,10 +29,19 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element +const _$EnumTypeEnumDecodeMap = { + 'alpha': EnumType.alpha, + 'beta': EnumType.beta, + 'gamma': EnumType.gamma, + 'delta': EnumType.delta, +}; + SimpleClassNullable _$SimpleClassNullableFromJson(Map json) => SimpleClassNullable( - $enumDecodeNullable(_$EnumTypeEnumMap, json['value']), - $enumDecodeNullable(_$EnumTypeEnumMap, json['withDefault']) ?? + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, json['value']), + $enumDecodeNullableWithDecodeMap( + _$EnumTypeEnumDecodeMap, json['withDefault']) ?? EnumType.alpha, ); diff --git a/json_serializable/test/supported_types/input.type_iterable.g.dart b/json_serializable/test/supported_types/input.type_iterable.g.dart index a9264f393..104f16100 100644 --- a/json_serializable/test/supported_types/input.type_iterable.g.dart +++ b/json_serializable/test/supported_types/input.type_iterable.g.dart @@ -308,7 +308,7 @@ SimpleClassOfEnumType _$SimpleClassOfEnumTypeFromJson( Map json) => SimpleClassOfEnumType( (json['value'] as List) - .map((e) => $enumDecode(_$EnumTypeEnumMap, e)), + .map((e) => $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ); Map _$SimpleClassOfEnumTypeToJson( @@ -317,6 +317,7 @@ Map _$SimpleClassOfEnumTypeToJson( 'value': instance.value.map((e) => _$EnumTypeEnumMap[e]!).toList(), }; +// ignore: unused_element const _$EnumTypeEnumMap = { EnumType.alpha: 'alpha', EnumType.beta: 'beta', @@ -324,11 +325,19 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element +const _$EnumTypeEnumDecodeMap = { + 'alpha': EnumType.alpha, + 'beta': EnumType.beta, + 'gamma': EnumType.gamma, + 'delta': EnumType.delta, +}; + SimpleClassNullableOfEnumType _$SimpleClassNullableOfEnumTypeFromJson( Map json) => SimpleClassNullableOfEnumType( (json['value'] as List?) - ?.map((e) => $enumDecode(_$EnumTypeEnumMap, e)), + ?.map((e) => $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ); Map _$SimpleClassNullableOfEnumTypeToJson( @@ -340,8 +349,8 @@ Map _$SimpleClassNullableOfEnumTypeToJson( SimpleClassOfEnumTypeNullable _$SimpleClassOfEnumTypeNullableFromJson( Map json) => SimpleClassOfEnumTypeNullable( - (json['value'] as List) - .map((e) => $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (json['value'] as List).map( + (e) => $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ); Map _$SimpleClassOfEnumTypeNullableToJson( @@ -354,8 +363,8 @@ SimpleClassNullableOfEnumTypeNullable _$SimpleClassNullableOfEnumTypeNullableFromJson( Map json) => SimpleClassNullableOfEnumTypeNullable( - (json['value'] as List?) - ?.map((e) => $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (json['value'] as List?)?.map((e) => + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ); Map _$SimpleClassNullableOfEnumTypeNullableToJson( diff --git a/json_serializable/test/supported_types/input.type_list.g.dart b/json_serializable/test/supported_types/input.type_list.g.dart index f86a6d00c..b4f17a06f 100644 --- a/json_serializable/test/supported_types/input.type_list.g.dart +++ b/json_serializable/test/supported_types/input.type_list.g.dart @@ -332,7 +332,7 @@ SimpleClassOfEnumType _$SimpleClassOfEnumTypeFromJson( Map json) => SimpleClassOfEnumType( (json['value'] as List) - .map((e) => $enumDecode(_$EnumTypeEnumMap, e)) + .map((e) => $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toList(), ); @@ -342,6 +342,7 @@ Map _$SimpleClassOfEnumTypeToJson( 'value': instance.value.map((e) => _$EnumTypeEnumMap[e]!).toList(), }; +// ignore: unused_element const _$EnumTypeEnumMap = { EnumType.alpha: 'alpha', EnumType.beta: 'beta', @@ -349,11 +350,19 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element +const _$EnumTypeEnumDecodeMap = { + 'alpha': EnumType.alpha, + 'beta': EnumType.beta, + 'gamma': EnumType.gamma, + 'delta': EnumType.delta, +}; + SimpleClassNullableOfEnumType _$SimpleClassNullableOfEnumTypeFromJson( Map json) => SimpleClassNullableOfEnumType( (json['value'] as List?) - ?.map((e) => $enumDecode(_$EnumTypeEnumMap, e)) + ?.map((e) => $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toList(), ); @@ -367,7 +376,8 @@ SimpleClassOfEnumTypeNullable _$SimpleClassOfEnumTypeNullableFromJson( Map json) => SimpleClassOfEnumTypeNullable( (json['value'] as List) - .map((e) => $enumDecodeNullable(_$EnumTypeEnumMap, e)) + .map((e) => + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toList(), ); @@ -382,7 +392,8 @@ SimpleClassNullableOfEnumTypeNullable Map json) => SimpleClassNullableOfEnumTypeNullable( (json['value'] as List?) - ?.map((e) => $enumDecodeNullable(_$EnumTypeEnumMap, e)) + ?.map((e) => + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toList(), ); diff --git a/json_serializable/test/supported_types/input.type_map.g.dart b/json_serializable/test/supported_types/input.type_map.g.dart index 5c8a3ea82..cf1f77e4e 100644 --- a/json_serializable/test/supported_types/input.type_map.g.dart +++ b/json_serializable/test/supported_types/input.type_map.g.dart @@ -125,8 +125,8 @@ SimpleClassOfEnumTypeToBigInt _$SimpleClassOfEnumTypeToBigIntFromJson( Map json) => SimpleClassOfEnumTypeToBigInt( (json['value'] as Map).map( - (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), BigInt.parse(e as String)), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + BigInt.parse(e as String)), ), ); @@ -137,6 +137,7 @@ Map _$SimpleClassOfEnumTypeToBigIntToJson( .map((k, e) => MapEntry(_$EnumTypeEnumMap[k]!, e.toString())), }; +// ignore: unused_element const _$EnumTypeEnumMap = { EnumType.alpha: 'alpha', EnumType.beta: 'beta', @@ -144,13 +145,22 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element +const _$EnumTypeEnumDecodeMap = { + 'alpha': EnumType.alpha, + 'beta': EnumType.beta, + 'gamma': EnumType.gamma, + 'delta': EnumType.delta, +}; + SimpleClassNullableOfEnumTypeToBigInt _$SimpleClassNullableOfEnumTypeToBigIntFromJson( Map json) => SimpleClassNullableOfEnumTypeToBigInt( (json['value'] as Map?)?.map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), BigInt.parse(e as String)), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + BigInt.parse(e as String)), ), ); @@ -378,7 +388,8 @@ SimpleClassOfEnumTypeToBigIntNullable Map json) => SimpleClassOfEnumTypeToBigIntNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e == null ? null : BigInt.parse(e as String)), ), ); @@ -395,7 +406,8 @@ SimpleClassNullableOfEnumTypeToBigIntNullable Map json) => SimpleClassNullableOfEnumTypeToBigIntNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e == null ? null : BigInt.parse(e as String)), ), ); @@ -617,7 +629,8 @@ SimpleClassOfEnumTypeToBool _$SimpleClassOfEnumTypeToBoolFromJson( Map json) => SimpleClassOfEnumTypeToBool( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as bool), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e as bool), ), ); @@ -631,7 +644,9 @@ SimpleClassNullableOfEnumTypeToBool _$SimpleClassNullableOfEnumTypeToBoolFromJson(Map json) => SimpleClassNullableOfEnumTypeToBool( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as bool), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + e as bool), ), ); @@ -839,7 +854,9 @@ SimpleClassOfEnumTypeToBoolNullable _$SimpleClassOfEnumTypeToBoolNullableFromJson(Map json) => SimpleClassOfEnumTypeToBoolNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as bool?), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + e as bool?), ), ); @@ -854,7 +871,9 @@ SimpleClassNullableOfEnumTypeToBoolNullable Map json) => SimpleClassNullableOfEnumTypeToBoolNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as bool?), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + e as bool?), ), ); @@ -1072,8 +1091,8 @@ SimpleClassOfEnumTypeToDateTime _$SimpleClassOfEnumTypeToDateTimeFromJson( Map json) => SimpleClassOfEnumTypeToDateTime( (json['value'] as Map).map( - (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), DateTime.parse(e as String)), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + DateTime.parse(e as String)), ), ); @@ -1090,7 +1109,8 @@ SimpleClassNullableOfEnumTypeToDateTime SimpleClassNullableOfEnumTypeToDateTime( (json['value'] as Map?)?.map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), DateTime.parse(e as String)), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + DateTime.parse(e as String)), ), ); @@ -1324,7 +1344,8 @@ SimpleClassOfEnumTypeToDateTimeNullable Map json) => SimpleClassOfEnumTypeToDateTimeNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e == null ? null : DateTime.parse(e as String)), ), ); @@ -1341,7 +1362,8 @@ SimpleClassNullableOfEnumTypeToDateTimeNullable Map json) => SimpleClassNullableOfEnumTypeToDateTimeNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e == null ? null : DateTime.parse(e as String)), ), ); @@ -1572,8 +1594,8 @@ SimpleClassOfEnumTypeToDouble _$SimpleClassOfEnumTypeToDoubleFromJson( Map json) => SimpleClassOfEnumTypeToDouble( (json['value'] as Map).map( - (k, e) => - MapEntry($enumDecode(_$EnumTypeEnumMap, k), (e as num).toDouble()), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + (e as num).toDouble()), ), ); @@ -1589,7 +1611,8 @@ SimpleClassNullableOfEnumTypeToDouble SimpleClassNullableOfEnumTypeToDouble( (json['value'] as Map?)?.map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), (e as num).toDouble()), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + (e as num).toDouble()), ), ); @@ -1806,7 +1829,8 @@ SimpleClassOfEnumTypeToDoubleNullable SimpleClassOfEnumTypeToDoubleNullable( (json['value'] as Map).map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), (e as num?)?.toDouble()), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + (e as num?)?.toDouble()), ), ); @@ -1822,7 +1846,8 @@ SimpleClassNullableOfEnumTypeToDoubleNullable SimpleClassNullableOfEnumTypeToDoubleNullable( (json['value'] as Map?)?.map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), (e as num?)?.toDouble()), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + (e as num?)?.toDouble()), ), ); @@ -2048,7 +2073,7 @@ SimpleClassOfEnumTypeToDuration _$SimpleClassOfEnumTypeToDurationFromJson( Map json) => SimpleClassOfEnumTypeToDuration( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), Duration(microseconds: (e as num).toInt())), ), ); @@ -2065,7 +2090,8 @@ SimpleClassNullableOfEnumTypeToDuration Map json) => SimpleClassNullableOfEnumTypeToDuration( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), Duration(microseconds: (e as num).toInt())), ), ); @@ -2304,7 +2330,8 @@ SimpleClassOfEnumTypeToDurationNullable Map json) => SimpleClassOfEnumTypeToDurationNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2321,7 +2348,8 @@ SimpleClassNullableOfEnumTypeToDurationNullable Map json) => SimpleClassNullableOfEnumTypeToDurationNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2549,7 +2577,8 @@ SimpleClassOfEnumTypeToDynamic _$SimpleClassOfEnumTypeToDynamicFromJson( Map json) => SimpleClassOfEnumTypeToDynamic( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e), + (k, e) => + MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e), ), ); @@ -2564,7 +2593,8 @@ SimpleClassNullableOfEnumTypeToDynamic Map json) => SimpleClassNullableOfEnumTypeToDynamic( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e), ), ); @@ -2683,7 +2713,8 @@ SimpleClassOfBigIntToEnumType _$SimpleClassOfBigIntToEnumTypeFromJson( Map json) => SimpleClassOfBigIntToEnumType( (json['value'] as Map).map( - (k, e) => MapEntry(BigInt.parse(k), $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(BigInt.parse(k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2699,8 +2730,8 @@ SimpleClassNullableOfBigIntToEnumType Map json) => SimpleClassNullableOfBigIntToEnumType( (json['value'] as Map?)?.map( - (k, e) => - MapEntry(BigInt.parse(k), $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(BigInt.parse(k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2715,8 +2746,8 @@ SimpleClassOfDateTimeToEnumType _$SimpleClassOfDateTimeToEnumTypeFromJson( Map json) => SimpleClassOfDateTimeToEnumType( (json['value'] as Map).map( - (k, e) => - MapEntry(DateTime.parse(k), $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(DateTime.parse(k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2732,8 +2763,8 @@ SimpleClassNullableOfDateTimeToEnumType Map json) => SimpleClassNullableOfDateTimeToEnumType( (json['value'] as Map?)?.map( - (k, e) => - MapEntry(DateTime.parse(k), $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(DateTime.parse(k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2748,7 +2779,8 @@ SimpleClassOfDynamicToEnumType _$SimpleClassOfDynamicToEnumTypeFromJson( Map json) => SimpleClassOfDynamicToEnumType( (json['value'] as Map).map( - (k, e) => MapEntry(k, $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => + MapEntry(k, $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2763,7 +2795,8 @@ SimpleClassNullableOfDynamicToEnumType Map json) => SimpleClassNullableOfDynamicToEnumType( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + k, $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2778,8 +2811,8 @@ SimpleClassOfEnumTypeToEnumType _$SimpleClassOfEnumTypeToEnumTypeFromJson( Map json) => SimpleClassOfEnumTypeToEnumType( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), - $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2795,8 +2828,9 @@ SimpleClassNullableOfEnumTypeToEnumType Map json) => SimpleClassNullableOfEnumTypeToEnumType( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), - $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2811,7 +2845,8 @@ SimpleClassOfIntToEnumType _$SimpleClassOfIntToEnumTypeFromJson( Map json) => SimpleClassOfIntToEnumType( (json['value'] as Map).map( - (k, e) => MapEntry(int.parse(k), $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + int.parse(k), $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2826,7 +2861,8 @@ SimpleClassNullableOfIntToEnumType _$SimpleClassNullableOfIntToEnumTypeFromJson( Map json) => SimpleClassNullableOfIntToEnumType( (json['value'] as Map?)?.map( - (k, e) => MapEntry(int.parse(k), $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + int.parse(k), $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2841,7 +2877,8 @@ SimpleClassOfObjectToEnumType _$SimpleClassOfObjectToEnumTypeFromJson( Map json) => SimpleClassOfObjectToEnumType( (json['value'] as Map).map( - (k, e) => MapEntry(k, $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => + MapEntry(k, $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2856,7 +2893,8 @@ SimpleClassNullableOfObjectToEnumType Map json) => SimpleClassNullableOfObjectToEnumType( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + k, $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2871,7 +2909,8 @@ SimpleClassOfStringToEnumType _$SimpleClassOfStringToEnumTypeFromJson( Map json) => SimpleClassOfStringToEnumType( (json['value'] as Map).map( - (k, e) => MapEntry(k, $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => + MapEntry(k, $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2886,7 +2925,8 @@ SimpleClassNullableOfStringToEnumType Map json) => SimpleClassNullableOfStringToEnumType( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + k, $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2901,7 +2941,8 @@ SimpleClassOfUriToEnumType _$SimpleClassOfUriToEnumTypeFromJson( Map json) => SimpleClassOfUriToEnumType( (json['value'] as Map).map( - (k, e) => MapEntry(Uri.parse(k), $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + Uri.parse(k), $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2916,7 +2957,8 @@ SimpleClassNullableOfUriToEnumType _$SimpleClassNullableOfUriToEnumTypeFromJson( Map json) => SimpleClassNullableOfUriToEnumType( (json['value'] as Map?)?.map( - (k, e) => MapEntry(Uri.parse(k), $enumDecode(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + Uri.parse(k), $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2932,8 +2974,8 @@ SimpleClassOfBigIntToEnumTypeNullable Map json) => SimpleClassOfBigIntToEnumTypeNullable( (json['value'] as Map).map( - (k, e) => MapEntry( - BigInt.parse(k), $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(BigInt.parse(k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2949,8 +2991,8 @@ SimpleClassNullableOfBigIntToEnumTypeNullable Map json) => SimpleClassNullableOfBigIntToEnumTypeNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry( - BigInt.parse(k), $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(BigInt.parse(k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2966,8 +3008,8 @@ SimpleClassOfDateTimeToEnumTypeNullable Map json) => SimpleClassOfDateTimeToEnumTypeNullable( (json['value'] as Map).map( - (k, e) => MapEntry( - DateTime.parse(k), $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(DateTime.parse(k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -2983,8 +3025,8 @@ SimpleClassNullableOfDateTimeToEnumTypeNullable Map json) => SimpleClassNullableOfDateTimeToEnumTypeNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry( - DateTime.parse(k), $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(DateTime.parse(k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3000,7 +3042,8 @@ SimpleClassOfDynamicToEnumTypeNullable Map json) => SimpleClassOfDynamicToEnumTypeNullable( (json['value'] as Map).map( - (k, e) => MapEntry(k, $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(k, + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3015,7 +3058,8 @@ SimpleClassNullableOfDynamicToEnumTypeNullable Map json) => SimpleClassNullableOfDynamicToEnumTypeNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(k, + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3030,8 +3074,9 @@ SimpleClassOfEnumTypeToEnumTypeNullable Map json) => SimpleClassOfEnumTypeToEnumTypeNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), - $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3047,8 +3092,9 @@ SimpleClassNullableOfEnumTypeToEnumTypeNullable Map json) => SimpleClassNullableOfEnumTypeToEnumTypeNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), - $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3063,8 +3109,8 @@ SimpleClassOfIntToEnumTypeNullable _$SimpleClassOfIntToEnumTypeNullableFromJson( Map json) => SimpleClassOfIntToEnumTypeNullable( (json['value'] as Map).map( - (k, e) => - MapEntry(int.parse(k), $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(int.parse(k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3080,8 +3126,8 @@ SimpleClassNullableOfIntToEnumTypeNullable Map json) => SimpleClassNullableOfIntToEnumTypeNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry( - int.parse(k), $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(int.parse(k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3097,7 +3143,8 @@ SimpleClassOfObjectToEnumTypeNullable Map json) => SimpleClassOfObjectToEnumTypeNullable( (json['value'] as Map).map( - (k, e) => MapEntry(k, $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(k, + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3112,7 +3159,8 @@ SimpleClassNullableOfObjectToEnumTypeNullable Map json) => SimpleClassNullableOfObjectToEnumTypeNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(k, + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3127,7 +3175,8 @@ SimpleClassOfStringToEnumTypeNullable Map json) => SimpleClassOfStringToEnumTypeNullable( (json['value'] as Map).map( - (k, e) => MapEntry(k, $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(k, + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3142,7 +3191,8 @@ SimpleClassNullableOfStringToEnumTypeNullable Map json) => SimpleClassNullableOfStringToEnumTypeNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(k, + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3156,8 +3206,8 @@ SimpleClassOfUriToEnumTypeNullable _$SimpleClassOfUriToEnumTypeNullableFromJson( Map json) => SimpleClassOfUriToEnumTypeNullable( (json['value'] as Map).map( - (k, e) => - MapEntry(Uri.parse(k), $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(Uri.parse(k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3173,8 +3223,8 @@ SimpleClassNullableOfUriToEnumTypeNullable Map json) => SimpleClassNullableOfUriToEnumTypeNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry( - Uri.parse(k), $enumDecodeNullable(_$EnumTypeEnumMap, e)), + (k, e) => MapEntry(Uri.parse(k), + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)), ), ); @@ -3286,7 +3336,8 @@ SimpleClassOfEnumTypeToFromJsonDynamicParam Map json) => SimpleClassOfEnumTypeToFromJsonDynamicParam( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), FromJsonDynamicParam.fromJson(e)), ), ); @@ -3302,7 +3353,8 @@ SimpleClassNullableOfEnumTypeToFromJsonDynamicParam Map json) => SimpleClassNullableOfEnumTypeToFromJsonDynamicParam( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), FromJsonDynamicParam.fromJson(e)), ), ); @@ -3537,7 +3589,8 @@ SimpleClassOfEnumTypeToFromJsonNullableObjectParam Map json) => SimpleClassOfEnumTypeToFromJsonNullableObjectParam( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), FromJsonNullableObjectParam.fromJson(e)), ), ); @@ -3553,7 +3606,8 @@ SimpleClassNullableOfEnumTypeToFromJsonNullableObjectParam Map json) => SimpleClassNullableOfEnumTypeToFromJsonNullableObjectParam( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), FromJsonNullableObjectParam.fromJson(e)), ), ); @@ -3793,7 +3847,8 @@ SimpleClassOfEnumTypeToFromJsonObjectParam Map json) => SimpleClassOfEnumTypeToFromJsonObjectParam( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), FromJsonObjectParam.fromJson(e as Object)), ), ); @@ -3809,7 +3864,8 @@ SimpleClassNullableOfEnumTypeToFromJsonObjectParam Map json) => SimpleClassNullableOfEnumTypeToFromJsonObjectParam( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), FromJsonObjectParam.fromJson(e as Object)), ), ); @@ -4031,8 +4087,8 @@ SimpleClassOfEnumTypeToInt _$SimpleClassOfEnumTypeToIntFromJson( Map json) => SimpleClassOfEnumTypeToInt( (json['value'] as Map).map( - (k, e) => - MapEntry($enumDecode(_$EnumTypeEnumMap, k), (e as num).toInt()), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + (e as num).toInt()), ), ); @@ -4046,8 +4102,8 @@ SimpleClassNullableOfEnumTypeToInt _$SimpleClassNullableOfEnumTypeToIntFromJson( Map json) => SimpleClassNullableOfEnumTypeToInt( (json['value'] as Map?)?.map( - (k, e) => - MapEntry($enumDecode(_$EnumTypeEnumMap, k), (e as num).toInt()), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + (e as num).toInt()), ), ); @@ -4255,8 +4311,8 @@ SimpleClassOfEnumTypeToIntNullable _$SimpleClassOfEnumTypeToIntNullableFromJson( Map json) => SimpleClassOfEnumTypeToIntNullable( (json['value'] as Map).map( - (k, e) => - MapEntry($enumDecode(_$EnumTypeEnumMap, k), (e as num?)?.toInt()), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + (e as num?)?.toInt()), ), ); @@ -4272,7 +4328,8 @@ SimpleClassNullableOfEnumTypeToIntNullable SimpleClassNullableOfEnumTypeToIntNullable( (json['value'] as Map?)?.map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), (e as num?)?.toInt()), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + (e as num?)?.toInt()), ), ); @@ -4481,7 +4538,8 @@ SimpleClassOfEnumTypeToNum _$SimpleClassOfEnumTypeToNumFromJson( Map json) => SimpleClassOfEnumTypeToNum( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as num), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e as num), ), ); @@ -4495,7 +4553,8 @@ SimpleClassNullableOfEnumTypeToNum _$SimpleClassNullableOfEnumTypeToNumFromJson( Map json) => SimpleClassNullableOfEnumTypeToNum( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as num), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e as num), ), ); @@ -4703,7 +4762,8 @@ SimpleClassOfEnumTypeToNumNullable _$SimpleClassOfEnumTypeToNumNullableFromJson( Map json) => SimpleClassOfEnumTypeToNumNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as num?), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e as num?), ), ); @@ -4718,7 +4778,9 @@ SimpleClassNullableOfEnumTypeToNumNullable Map json) => SimpleClassNullableOfEnumTypeToNumNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as num?), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + e as num?), ), ); @@ -4930,7 +4992,8 @@ SimpleClassOfEnumTypeToObject _$SimpleClassOfEnumTypeToObjectFromJson( Map json) => SimpleClassOfEnumTypeToObject( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as Object), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e as Object), ), ); @@ -4945,7 +5008,9 @@ SimpleClassNullableOfEnumTypeToObject Map json) => SimpleClassNullableOfEnumTypeToObject( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as Object), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + e as Object), ), ); @@ -5157,7 +5222,8 @@ SimpleClassOfEnumTypeToObjectNullable Map json) => SimpleClassOfEnumTypeToObjectNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e), ), ); @@ -5172,7 +5238,8 @@ SimpleClassNullableOfEnumTypeToObjectNullable Map json) => SimpleClassNullableOfEnumTypeToObjectNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e), ), ); @@ -5469,7 +5536,7 @@ SimpleClassOfEnumTypeToRecord _$SimpleClassOfEnumTypeToRecordFromJson( SimpleClassOfEnumTypeToRecord( (json['value'] as Map).map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), _$recordConvert( e, ($jsonValue) => ( @@ -5498,7 +5565,7 @@ SimpleClassNullableOfEnumTypeToRecord SimpleClassNullableOfEnumTypeToRecord( (json['value'] as Map?)?.map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), _$recordConvert( e, ($jsonValue) => ( @@ -5828,7 +5895,8 @@ SimpleClassOfEnumTypeToString _$SimpleClassOfEnumTypeToStringFromJson( Map json) => SimpleClassOfEnumTypeToString( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as String), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e as String), ), ); @@ -5843,7 +5911,9 @@ SimpleClassNullableOfEnumTypeToString Map json) => SimpleClassNullableOfEnumTypeToString( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as String), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + e as String), ), ); @@ -6053,7 +6123,9 @@ SimpleClassOfEnumTypeToStringNullable Map json) => SimpleClassOfEnumTypeToStringNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as String?), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + e as String?), ), ); @@ -6068,7 +6140,9 @@ SimpleClassNullableOfEnumTypeToStringNullable Map json) => SimpleClassNullableOfEnumTypeToStringNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as String?), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + e as String?), ), ); @@ -6283,8 +6357,8 @@ SimpleClassOfEnumTypeToUri _$SimpleClassOfEnumTypeToUriFromJson( Map json) => SimpleClassOfEnumTypeToUri( (json['value'] as Map).map( - (k, e) => - MapEntry($enumDecode(_$EnumTypeEnumMap, k), Uri.parse(e as String)), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + Uri.parse(e as String)), ), ); @@ -6299,8 +6373,8 @@ SimpleClassNullableOfEnumTypeToUri _$SimpleClassNullableOfEnumTypeToUriFromJson( Map json) => SimpleClassNullableOfEnumTypeToUri( (json['value'] as Map?)?.map( - (k, e) => - MapEntry($enumDecode(_$EnumTypeEnumMap, k), Uri.parse(e as String)), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), + Uri.parse(e as String)), ), ); @@ -6526,7 +6600,7 @@ SimpleClassOfEnumTypeToUriNullable _$SimpleClassOfEnumTypeToUriNullableFromJson( Map json) => SimpleClassOfEnumTypeToUriNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry($enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e == null ? null : Uri.parse(e as String)), ), ); @@ -6543,7 +6617,8 @@ SimpleClassNullableOfEnumTypeToUriNullable Map json) => SimpleClassNullableOfEnumTypeToUriNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), + (k, e) => MapEntry( + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), e == null ? null : Uri.parse(e as String)), ), ); diff --git a/json_serializable/test/supported_types/input.type_record.g.dart b/json_serializable/test/supported_types/input.type_record.g.dart index bc37b7cd2..dbf67e472 100644 --- a/json_serializable/test/supported_types/input.type_record.g.dart +++ b/json_serializable/test/supported_types/input.type_record.g.dart @@ -553,8 +553,9 @@ SimpleClassOfEnumType _$SimpleClassOfEnumTypeFromJson( _$recordConvert( json['value'], ($jsonValue) => ( - $enumDecode(_$EnumTypeEnumMap, $jsonValue[r'$1']), - named: $enumDecode(_$EnumTypeEnumMap, $jsonValue['named']), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, $jsonValue[r'$1']), + named: $enumDecodeWithDecodeMap( + _$EnumTypeEnumDecodeMap, $jsonValue['named']), ), ), ); @@ -568,6 +569,7 @@ Map _$SimpleClassOfEnumTypeToJson( }, }; +// ignore: unused_element const _$EnumTypeEnumMap = { EnumType.alpha: 'alpha', EnumType.beta: 'beta', @@ -575,14 +577,23 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element +const _$EnumTypeEnumDecodeMap = { + 'alpha': EnumType.alpha, + 'beta': EnumType.beta, + 'gamma': EnumType.gamma, + 'delta': EnumType.delta, +}; + SimpleClassNullableOfEnumType _$SimpleClassNullableOfEnumTypeFromJson( Map json) => SimpleClassNullableOfEnumType( _$recordConvertNullable( json['value'], ($jsonValue) => ( - $enumDecode(_$EnumTypeEnumMap, $jsonValue[r'$1']), - named: $enumDecode(_$EnumTypeEnumMap, $jsonValue['named']), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, $jsonValue[r'$1']), + named: $enumDecodeWithDecodeMap( + _$EnumTypeEnumDecodeMap, $jsonValue['named']), ), ), ); @@ -604,8 +615,10 @@ SimpleClassOfEnumTypeNullable _$SimpleClassOfEnumTypeNullableFromJson( _$recordConvert( json['value'], ($jsonValue) => ( - $enumDecodeNullable(_$EnumTypeEnumMap, $jsonValue[r'$1']), - named: $enumDecodeNullable(_$EnumTypeEnumMap, $jsonValue['named']), + $enumDecodeNullableWithDecodeMap( + _$EnumTypeEnumDecodeMap, $jsonValue[r'$1']), + named: $enumDecodeNullableWithDecodeMap( + _$EnumTypeEnumDecodeMap, $jsonValue['named']), ), ), ); @@ -626,9 +639,10 @@ SimpleClassNullableOfEnumTypeNullable _$recordConvertNullable( json['value'], ($jsonValue) => ( - $enumDecodeNullable(_$EnumTypeEnumMap, $jsonValue[r'$1']), - named: - $enumDecodeNullable(_$EnumTypeEnumMap, $jsonValue['named']), + $enumDecodeNullableWithDecodeMap( + _$EnumTypeEnumDecodeMap, $jsonValue[r'$1']), + named: $enumDecodeNullableWithDecodeMap( + _$EnumTypeEnumDecodeMap, $jsonValue['named']), ), ), ); diff --git a/json_serializable/test/supported_types/input.type_set.g.dart b/json_serializable/test/supported_types/input.type_set.g.dart index 234afad93..282e1fc16 100644 --- a/json_serializable/test/supported_types/input.type_set.g.dart +++ b/json_serializable/test/supported_types/input.type_set.g.dart @@ -334,7 +334,7 @@ SimpleClassOfEnumType _$SimpleClassOfEnumTypeFromJson( Map json) => SimpleClassOfEnumType( (json['value'] as List) - .map((e) => $enumDecode(_$EnumTypeEnumMap, e)) + .map((e) => $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toSet(), ); @@ -344,6 +344,7 @@ Map _$SimpleClassOfEnumTypeToJson( 'value': instance.value.map((e) => _$EnumTypeEnumMap[e]!).toList(), }; +// ignore: unused_element const _$EnumTypeEnumMap = { EnumType.alpha: 'alpha', EnumType.beta: 'beta', @@ -351,11 +352,19 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element +const _$EnumTypeEnumDecodeMap = { + 'alpha': EnumType.alpha, + 'beta': EnumType.beta, + 'gamma': EnumType.gamma, + 'delta': EnumType.delta, +}; + SimpleClassNullableOfEnumType _$SimpleClassNullableOfEnumTypeFromJson( Map json) => SimpleClassNullableOfEnumType( (json['value'] as List?) - ?.map((e) => $enumDecode(_$EnumTypeEnumMap, e)) + ?.map((e) => $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toSet(), ); @@ -369,7 +378,8 @@ SimpleClassOfEnumTypeNullable _$SimpleClassOfEnumTypeNullableFromJson( Map json) => SimpleClassOfEnumTypeNullable( (json['value'] as List) - .map((e) => $enumDecodeNullable(_$EnumTypeEnumMap, e)) + .map((e) => + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toSet(), ); @@ -384,7 +394,8 @@ SimpleClassNullableOfEnumTypeNullable Map json) => SimpleClassNullableOfEnumTypeNullable( (json['value'] as List?) - ?.map((e) => $enumDecodeNullable(_$EnumTypeEnumMap, e)) + ?.map((e) => + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toSet(), );