From 3bcc58342a3554033594a188eab7e52946d9e158 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 14:51:17 -0300 Subject: [PATCH 01/14] feat: add `aliases` to `JsonValue` --- _test_yaml/test/src/build_config.g.dart | 29 +- json_annotation/lib/src/enum_helpers.dart | 85 ++++++ json_annotation/lib/src/json_value.dart | 7 +- json_serializable/README.md | 117 ++++++-- json_serializable/example/example.dart | 54 +++- json_serializable/example/example.g.dart | 63 +++- json_serializable/lib/src/enum_utils.dart | 107 ++++++- .../lib/src/json_enum_generator.dart | 2 +- .../lib/src/type_helpers/enum_helper.dart | 10 +- .../test/default_value/default_value.g.dart | 11 +- .../default_value.g_any_map__checked.g.dart | 14 +- .../implicit_default_value.g.dart | 12 +- .../integration/converter_examples.g.dart | 8 +- .../create_per_field_to_json_example.g.dart | 8 +- .../test/integration/integration_test.dart | 33 ++ .../test/integration/json_enum_example.dart | 23 ++ .../test/integration/json_enum_example.g.dart | 68 ++++- .../test/integration/json_test_example.g.dart | 40 ++- .../json_test_example.g_any_map.g.dart | 40 ++- .../test/src/_json_enum_test_input.dart | 5 + .../src/unknown_enum_value_test_input.dart | 12 +- .../input.type_enumtype.g.dart | 17 +- .../input.type_iterable.g.dart | 19 +- .../supported_types/input.type_list.g.dart | 17 +- .../supported_types/input.type_map.g.dart | 283 +++++++++++------- .../supported_types/input.type_record.g.dart | 30 +- .../supported_types/input.type_set.g.dart | 17 +- 27 files changed, 902 insertions(+), 229 deletions(-) diff --git a/_test_yaml/test/src/build_config.g.dart b/_test_yaml/test/src/build_config.g.dart index e4d832e78..be3efebc4 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; }, @@ -44,6 +45,13 @@ const _$AutoApplyEnumMap = { AutoApply.rootPackage: 'root_package', }; +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 +78,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', @@ -125,3 +139,8 @@ const _$BuildToEnumMap = { BuildTo.cache: 'cache', BuildTo.source: 'source', }; + +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..e9fdb129c 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 List aliases; + + const JsonValue(this.value, {this.aliases = const []}); } diff --git a/json_serializable/README.md b/json_serializable/README.md index 713ff4c1e..358672e97 100644 --- a/json_serializable/README.md +++ b/json_serializable/README.md @@ -31,22 +31,52 @@ import 'package:json_annotation/json_annotation.dart'; part 'example.g.dart'; @JsonSerializable() -class Person { - /// The generated code assumes these values exist in JSON. - final String firstName, lastName; +class A { + final StatusCode? statusCode; + final StatusCode2 statusCode2; + final StatusCode3 statusCode3; - /// The generated code below handles if the corresponding JSON value doesn't - /// exist or is empty. - final DateTime? dateOfBirth; + A(this.statusCode, this.statusCode2, this.statusCode3); - Person({required this.firstName, required this.lastName, this.dateOfBirth}); + factory A.fromJson(Map json) => _$AFromJson(json); - /// Connect the generated [_$PersonFromJson] function to the `fromJson` - /// factory. - factory Person.fromJson(Map json) => _$PersonFromJson(json); + Map toJson() => _$AToJson(this); +} + +enum StatusCode { + @JsonValue(200, aliases: [201, 202]) + success, + @JsonValue(301) + movedPermanently, + @JsonValue(302) + found, + @JsonValue(500) + internalServerError, +} + +@JsonEnum(valueField: 'code') +enum StatusCode2 { + success(200), + movedPermanently(301), + found(302), + internalServerError(500); - /// Connect the generated [_$PersonToJson] function to the `toJson` method. - Map toJson() => _$PersonToJson(this); + const StatusCode2(this.code); + + final int code; +} + +@JsonEnum(valueField: 'code') +enum StatusCode3 { + success(200), + movedPermanently(301), + @JsonValue(1000) + found(302), + internalServerError(500); + + const StatusCode3(this.code); + + final int code; } ``` @@ -55,19 +85,62 @@ Building creates the corresponding part `example.g.dart`: ```dart part of 'example.dart'; -Person _$PersonFromJson(Map json) => Person( - firstName: json['firstName'] as String, - lastName: json['lastName'] as String, - dateOfBirth: json['dateOfBirth'] == null - ? null - : DateTime.parse(json['dateOfBirth'] as String), +A _$AFromJson(Map json) => A( + $enumDecodeNullableWithDecodeMap( + _$StatusCodeEnumDecodeMap, json['statusCode']), + $enumDecodeWithDecodeMap(_$StatusCode2EnumDecodeMap, json['statusCode2']), + $enumDecodeWithDecodeMap(_$StatusCode3EnumDecodeMap, json['statusCode3']), ); -Map _$PersonToJson(Person instance) => { - 'firstName': instance.firstName, - 'lastName': instance.lastName, - 'dateOfBirth': instance.dateOfBirth?.toIso8601String(), +Map _$AToJson(A instance) => { + 'statusCode': _$StatusCodeEnumMap[instance.statusCode], + 'statusCode2': _$StatusCode2EnumMap[instance.statusCode2]!, + 'statusCode3': _$StatusCode3EnumMap[instance.statusCode3]!, }; + +const _$StatusCodeEnumMap = { + StatusCode.success: 200, + StatusCode.movedPermanently: 301, + StatusCode.found: 302, + StatusCode.internalServerError: 500, +}; + +const _$StatusCodeEnumDecodeMap = { + 200: StatusCode.success, + 201: StatusCode.success, + 202: StatusCode.success, + 301: StatusCode.movedPermanently, + 302: StatusCode.found, + 500: StatusCode.internalServerError, +}; + +const _$StatusCode2EnumMap = { + StatusCode2.success: 200, + StatusCode2.movedPermanently: 301, + StatusCode2.found: 302, + StatusCode2.internalServerError: 500, +}; + +const _$StatusCode2EnumDecodeMap = { + 200: StatusCode2.success, + 301: StatusCode2.movedPermanently, + 302: StatusCode2.found, + 500: StatusCode2.internalServerError, +}; + +const _$StatusCode3EnumMap = { + StatusCode3.success: 200, + StatusCode3.movedPermanently: 301, + StatusCode3.found: 1000, + StatusCode3.internalServerError: 500, +}; + +const _$StatusCode3EnumDecodeMap = { + 200: StatusCode3.success, + 301: StatusCode3.movedPermanently, + 1000: StatusCode3.found, + 500: StatusCode3.internalServerError, +}; ``` # Running the code generator diff --git a/json_serializable/example/example.dart b/json_serializable/example/example.dart index 39cc11597..36a9ea4f0 100644 --- a/json_serializable/example/example.dart +++ b/json_serializable/example/example.dart @@ -7,20 +7,50 @@ import 'package:json_annotation/json_annotation.dart'; part 'example.g.dart'; @JsonSerializable() -class Person { - /// The generated code assumes these values exist in JSON. - final String firstName, lastName; +class A { + final StatusCode? statusCode; + final StatusCode2 statusCode2; + final StatusCode3 statusCode3; - /// The generated code below handles if the corresponding JSON value doesn't - /// exist or is empty. - final DateTime? dateOfBirth; + A(this.statusCode, this.statusCode2, this.statusCode3); - Person({required this.firstName, required this.lastName, this.dateOfBirth}); + factory A.fromJson(Map json) => _$AFromJson(json); - /// Connect the generated [_$PersonFromJson] function to the `fromJson` - /// factory. - factory Person.fromJson(Map json) => _$PersonFromJson(json); + Map toJson() => _$AToJson(this); +} + +enum StatusCode { + @JsonValue(200, aliases: [201, 202]) + success, + @JsonValue(301) + movedPermanently, + @JsonValue(302) + found, + @JsonValue(500) + internalServerError, +} + +@JsonEnum(valueField: 'code') +enum StatusCode2 { + success(200), + movedPermanently(301), + found(302), + internalServerError(500); + + const StatusCode2(this.code); + + final int code; +} + +@JsonEnum(valueField: 'code') +enum StatusCode3 { + success(200), + movedPermanently(301), + @JsonValue(1000) + found(302), + internalServerError(500); + + const StatusCode3(this.code); - /// Connect the generated [_$PersonToJson] function to the `toJson` method. - Map toJson() => _$PersonToJson(this); + final int code; } diff --git a/json_serializable/example/example.g.dart b/json_serializable/example/example.g.dart index 0ed069cae..942901485 100644 --- a/json_serializable/example/example.g.dart +++ b/json_serializable/example/example.g.dart @@ -8,16 +8,59 @@ part of 'example.dart'; // JsonSerializableGenerator // ************************************************************************** -Person _$PersonFromJson(Map json) => Person( - firstName: json['firstName'] as String, - lastName: json['lastName'] as String, - dateOfBirth: json['dateOfBirth'] == null - ? null - : DateTime.parse(json['dateOfBirth'] as String), +A _$AFromJson(Map json) => A( + $enumDecodeNullableWithDecodeMap( + _$StatusCodeEnumDecodeMap, json['statusCode']), + $enumDecodeWithDecodeMap(_$StatusCode2EnumDecodeMap, json['statusCode2']), + $enumDecodeWithDecodeMap(_$StatusCode3EnumDecodeMap, json['statusCode3']), ); -Map _$PersonToJson(Person instance) => { - 'firstName': instance.firstName, - 'lastName': instance.lastName, - 'dateOfBirth': instance.dateOfBirth?.toIso8601String(), +Map _$AToJson(A instance) => { + 'statusCode': _$StatusCodeEnumMap[instance.statusCode], + 'statusCode2': _$StatusCode2EnumMap[instance.statusCode2]!, + 'statusCode3': _$StatusCode3EnumMap[instance.statusCode3]!, }; + +const _$StatusCodeEnumMap = { + StatusCode.success: 200, + StatusCode.movedPermanently: 301, + StatusCode.found: 302, + StatusCode.internalServerError: 500, +}; + +const _$StatusCodeEnumDecodeMap = { + 200: StatusCode.success, + 201: StatusCode.success, + 202: StatusCode.success, + 301: StatusCode.movedPermanently, + 302: StatusCode.found, + 500: StatusCode.internalServerError, +}; + +const _$StatusCode2EnumMap = { + StatusCode2.success: 200, + StatusCode2.movedPermanently: 301, + StatusCode2.found: 302, + StatusCode2.internalServerError: 500, +}; + +const _$StatusCode2EnumDecodeMap = { + 200: StatusCode2.success, + 301: StatusCode2.movedPermanently, + 302: StatusCode2.found, + 500: StatusCode2.internalServerError, +}; + +const _$StatusCode3EnumMap = { + StatusCode3.success: 200, + StatusCode3.movedPermanently: 301, + StatusCode3.found: 1000, + StatusCode3.internalServerError: 500, +}; + +const _$StatusCode3EnumDecodeMap = { + 200: StatusCode3.success, + 301: StatusCode3.movedPermanently, + 1000: StatusCode3.found, + 500: StatusCode3.internalServerError, +}; diff --git a/json_serializable/lib/src/enum_utils.dart b/json_serializable/lib/src/enum_utils.dart index d5aa4d2d2..8289a0fd9 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,43 @@ 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 + : '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 + : 'const ${constDecodeMapName(targetType)} = {\n$decodeItems\n};'; + + return valuesMap == null && decodeMap == null + ? null + : [valuesMap, decodeMap].join('\n\n'); } Map? _enumMap( @@ -73,6 +98,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 (final alias in _generateAlias(field: field, targetType: targetType)) + alias: field, + }, + }; +} + Object? _generateEntry({ required FieldElement field, required JsonEnum jsonEnum, @@ -138,6 +191,36 @@ Object? _generateEntry({ } } +List _generateAlias({ + 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.listValue) + 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 +237,10 @@ JsonEnum _fromAnnotation(DartObject? dartObject) { extension on ConstantReader { bool get validValueType => isString || isNull || isInt; + + bool get validAliasesType => + isList && + listValue.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..ce6df52cd 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()), @@ -98,3 +100,10 @@ const _$GreekEnumMap = { Greek.gamma: 'gamma', Greek.delta: 'delta', }; + +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..635f31c10 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 @@ -123,3 +126,10 @@ const _$GreekEnumMap = { Greek.gamma: 'gamma', Greek.delta: 'delta', }; + +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..2d2bdbd3c 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()), @@ -103,3 +104,10 @@ const _$GreekEnumMap = { Greek.gamma: 'gamma', Greek.delta: 'delta', }; + +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..a4e490adf 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( @@ -52,6 +53,11 @@ const _$Issue1202RegressionEnumEnumMap = { Issue1202RegressionEnum.nullValue: null, }; +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..1a7c5c293 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), @@ -62,6 +63,11 @@ const _$EnumValueEnumMap = { EnumValue.second: 'second', }; +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..b3b112503 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..5d8ed6713 100644 --- a/json_serializable/test/integration/json_enum_example.g.dart +++ b/json_serializable/test/integration/json_enum_example.g.dart @@ -15,7 +15,8 @@ Issue559Regression _$Issue559RegressionFromJson(Map json) { disallowNullValues: const ['status'], ); return Issue559Regression( - status: $enumDecodeNullable(_$Issue559RegressionEnumEnumMap, json['status'], + status: $enumDecodeNullableWithDecodeMap( + _$Issue559RegressionEnumEnumDecodeMap, json['status'], unknownValue: JsonKey.nullForUndefinedEnumValue), ); } @@ -26,6 +27,12 @@ const _$Issue559RegressionEnumEnumMap = { Issue559RegressionEnum.gamma: 'gamma', }; +const _$Issue559RegressionEnumEnumDecodeMap = { + 'alpha': Issue559RegressionEnum.alpha, + 'beta': Issue559RegressionEnum.beta, + 'gamma': Issue559RegressionEnum.gamma, +}; + Map _$Issue1145RegressionAToJson( Issue1145RegressionA instance) => { @@ -39,6 +46,12 @@ const _$Issue1145RegressionEnumEnumMap = { Issue1145RegressionEnum.gamma: 'gamma', }; +const _$Issue1145RegressionEnumEnumDecodeMap = { + 'alpha': Issue1145RegressionEnum.alpha, + 'beta': Issue1145RegressionEnum.beta, + 'gamma': Issue1145RegressionEnum.gamma, +}; + Map _$Issue1145RegressionBToJson( Issue1145RegressionB instance) => { @@ -49,8 +62,8 @@ Map _$Issue1145RegressionBToJson( Issue1226Regression _$Issue1226RegressionFromJson(Map json) => Issue1226Regression( - durationType: $enumDecodeNullable( - _$Issue1145RegressionEnumEnumMap, json['durationType']), + durationType: $enumDecodeNullableWithDecodeMap( + _$Issue1145RegressionEnumEnumDecodeMap, json['durationType']), ); Map _$Issue1226RegressionToJson( @@ -61,6 +74,31 @@ Map _$Issue1226RegressionToJson( 'durationType': value, }; +EnumWithAliasesData _$EnumWithAliasesDataFromJson(Map json) => + EnumWithAliasesData( + $enumDecodeWithDecodeMap(_$EnumWithAliasesEnumDecodeMap, json['value']), + ); + +Map _$EnumWithAliasesDataToJson( + EnumWithAliasesData instance) => + { + 'value': _$EnumWithAliasesEnumMap[instance.value]!, + }; + +const _$EnumWithAliasesEnumMap = { + EnumWithAliases.one: '1', + EnumWithAliases.two: '2', + EnumWithAliases.three: '3', +}; + +const _$EnumWithAliasesEnumDecodeMap = { + '1': EnumWithAliases.one, + '2': EnumWithAliases.two, + 'two': EnumWithAliases.two, + 'deux': EnumWithAliases.two, + '3': EnumWithAliases.three, +}; + const _$StandAloneEnumEnumMap = { StandAloneEnum.alpha: 'a', StandAloneEnum.beta: 'b', @@ -68,19 +106,43 @@ const _$StandAloneEnumEnumMap = { StandAloneEnum.delta: 'd', }; +const _$StandAloneEnumEnumDecodeMap = { + 'a': StandAloneEnum.alpha, + 'b': StandAloneEnum.beta, + 'g': StandAloneEnum.gamma, + 'd': StandAloneEnum.delta, +}; + const _$DayTypeEnumMap = { DayType.noGood: 'no-good', DayType.rotten: 'rotten', DayType.veryBad: 'very-bad', }; +const _$DayTypeEnumDecodeMap = { + 'no-good': DayType.noGood, + 'rotten': DayType.rotten, + 'very-bad': DayType.veryBad, +}; + const _$MyStatusCodeEnumMap = { MyStatusCode.success: 200, MyStatusCode.weird: 701, }; +const _$MyStatusCodeEnumDecodeMap = { + 200: MyStatusCode.success, + 701: MyStatusCode.weird, +}; + const _$EnumValueFieldIndexEnumMap = { EnumValueFieldIndex.success: 0, EnumValueFieldIndex.weird: 701, EnumValueFieldIndex.oneMore: 2, }; + +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..cd0fd2f48 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) => { @@ -57,13 +58,23 @@ const _$CategoryEnumMap = { Category.notDiscoveredYet: 'not_discovered_yet', }; +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 +91,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; } @@ -104,6 +116,13 @@ const _$StatusCodeEnumMap = { StatusCode.unknown: 'unknown', }; +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 +194,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..bebe7f35c 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) => { @@ -57,13 +58,23 @@ const _$CategoryEnumMap = { Category.notDiscoveredYet: 'not_discovered_yet', }; +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 +91,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; } @@ -104,6 +116,13 @@ const _$StatusCodeEnumMap = { StatusCode.unknown: 'unknown', }; +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 +192,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/src/_json_enum_test_input.dart b/json_serializable/test/src/_json_enum_test_input.dart index 594229f7b..06b9be9f7 100644 --- a/json_serializable/test/src/_json_enum_test_input.dart +++ b/json_serializable/test/src/_json_enum_test_input.dart @@ -10,6 +10,11 @@ const _$EnumValueIssue1147EnumMap = { EnumValueIssue1147.success: 200, EnumValueIssue1147.weird: 601, }; + +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..a8d18dd77 100644 --- a/json_serializable/test/src/unknown_enum_value_test_input.dart +++ b/json_serializable/test/src/unknown_enum_value_test_input.dart @@ -6,8 +6,8 @@ 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; @@ -18,6 +18,14 @@ const _$UnknownEnumValueItemsEnumMap = { UnknownEnumValueItems.vUnknown: 'vUnknown', UnknownEnumValueItems.vNull: 'vNull', }; + +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..7110c3875 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, ); @@ -27,10 +28,18 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +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..075c5503f 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( @@ -324,11 +324,18 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +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 +347,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 +361,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..2d6d38618 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(), ); @@ -349,11 +349,18 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +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 +374,8 @@ SimpleClassOfEnumTypeNullable _$SimpleClassOfEnumTypeNullableFromJson( Map json) => SimpleClassOfEnumTypeNullable( (json['value'] as List) - .map((e) => $enumDecodeNullable(_$EnumTypeEnumMap, e)) + .map((e) => + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toList(), ); @@ -382,7 +390,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..092a7a1d3 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)), ), ); @@ -144,13 +144,21 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +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 +386,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 +404,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 +627,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 +642,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 +852,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 +869,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 +1089,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 +1107,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 +1342,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 +1360,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 +1592,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 +1609,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 +1827,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 +1844,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 +2071,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 +2088,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 +2328,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 +2346,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 +2575,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 +2591,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 +2711,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 +2728,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 +2744,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 +2761,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 +2777,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 +2793,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 +2809,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 +2826,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 +2843,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 +2859,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 +2875,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 +2891,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 +2907,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 +2923,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 +2939,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 +2955,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 +2972,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 +2989,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 +3006,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 +3023,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 +3040,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 +3056,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 +3072,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 +3090,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 +3107,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 +3124,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 +3141,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 +3157,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 +3173,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 +3189,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 +3204,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 +3221,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 +3334,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 +3351,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 +3587,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 +3604,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 +3845,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 +3862,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 +4085,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 +4100,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 +4309,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 +4326,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 +4536,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 +4551,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 +4760,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 +4776,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 +4990,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 +5006,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 +5220,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 +5236,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 +5534,7 @@ SimpleClassOfEnumTypeToRecord _$SimpleClassOfEnumTypeToRecordFromJson( SimpleClassOfEnumTypeToRecord( (json['value'] as Map).map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), _$recordConvert( e, ($jsonValue) => ( @@ -5498,7 +5563,7 @@ SimpleClassNullableOfEnumTypeToRecord SimpleClassNullableOfEnumTypeToRecord( (json['value'] as Map?)?.map( (k, e) => MapEntry( - $enumDecode(_$EnumTypeEnumMap, k), + $enumDecodeWithDecodeMap(_$EnumTypeEnumDecodeMap, k), _$recordConvert( e, ($jsonValue) => ( @@ -5828,7 +5893,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 +5909,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 +6121,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 +6138,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 +6355,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 +6371,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 +6598,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 +6615,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..1fed2d307 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']), ), ), ); @@ -575,14 +576,22 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +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 +613,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 +637,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..48e52f838 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(), ); @@ -351,11 +351,18 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +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 +376,8 @@ SimpleClassOfEnumTypeNullable _$SimpleClassOfEnumTypeNullableFromJson( Map json) => SimpleClassOfEnumTypeNullable( (json['value'] as List) - .map((e) => $enumDecodeNullable(_$EnumTypeEnumMap, e)) + .map((e) => + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toSet(), ); @@ -384,7 +392,8 @@ SimpleClassNullableOfEnumTypeNullable Map json) => SimpleClassNullableOfEnumTypeNullable( (json['value'] as List?) - ?.map((e) => $enumDecodeNullable(_$EnumTypeEnumMap, e)) + ?.map((e) => + $enumDecodeNullableWithDecodeMap(_$EnumTypeEnumDecodeMap, e)) .toSet(), ); From 7e54bad7a9b207f1b8c010cc83dd6501665f42c0 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 14:52:15 -0300 Subject: [PATCH 02/14] chore: reformat project --- json_serializable/test/integration/json_enum_example.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_serializable/test/integration/json_enum_example.dart b/json_serializable/test/integration/json_enum_example.dart index b3b112503..27263260b 100644 --- a/json_serializable/test/integration/json_enum_example.dart +++ b/json_serializable/test/integration/json_enum_example.dart @@ -136,7 +136,7 @@ enum EnumWithAliases { @JsonSerializable() class EnumWithAliasesData { final EnumWithAliases value; - + EnumWithAliasesData(this.value); factory EnumWithAliasesData.fromJson(Map json) => From e632181029cf28c6f0d334548e70467fefa8aca7 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 14:55:43 -0300 Subject: [PATCH 03/14] fix: revert example that was being used for testing purposes --- json_serializable/README.md | 117 +++++------------------ json_serializable/example/example.dart | 54 +++-------- json_serializable/example/example.g.dart | 63 ++---------- 3 files changed, 44 insertions(+), 190 deletions(-) diff --git a/json_serializable/README.md b/json_serializable/README.md index 358672e97..713ff4c1e 100644 --- a/json_serializable/README.md +++ b/json_serializable/README.md @@ -31,52 +31,22 @@ import 'package:json_annotation/json_annotation.dart'; part 'example.g.dart'; @JsonSerializable() -class A { - final StatusCode? statusCode; - final StatusCode2 statusCode2; - final StatusCode3 statusCode3; +class Person { + /// The generated code assumes these values exist in JSON. + final String firstName, lastName; - A(this.statusCode, this.statusCode2, this.statusCode3); + /// The generated code below handles if the corresponding JSON value doesn't + /// exist or is empty. + final DateTime? dateOfBirth; - factory A.fromJson(Map json) => _$AFromJson(json); + Person({required this.firstName, required this.lastName, this.dateOfBirth}); - Map toJson() => _$AToJson(this); -} - -enum StatusCode { - @JsonValue(200, aliases: [201, 202]) - success, - @JsonValue(301) - movedPermanently, - @JsonValue(302) - found, - @JsonValue(500) - internalServerError, -} - -@JsonEnum(valueField: 'code') -enum StatusCode2 { - success(200), - movedPermanently(301), - found(302), - internalServerError(500); + /// Connect the generated [_$PersonFromJson] function to the `fromJson` + /// factory. + factory Person.fromJson(Map json) => _$PersonFromJson(json); - const StatusCode2(this.code); - - final int code; -} - -@JsonEnum(valueField: 'code') -enum StatusCode3 { - success(200), - movedPermanently(301), - @JsonValue(1000) - found(302), - internalServerError(500); - - const StatusCode3(this.code); - - final int code; + /// Connect the generated [_$PersonToJson] function to the `toJson` method. + Map toJson() => _$PersonToJson(this); } ``` @@ -85,62 +55,19 @@ Building creates the corresponding part `example.g.dart`: ```dart part of 'example.dart'; -A _$AFromJson(Map json) => A( - $enumDecodeNullableWithDecodeMap( - _$StatusCodeEnumDecodeMap, json['statusCode']), - $enumDecodeWithDecodeMap(_$StatusCode2EnumDecodeMap, json['statusCode2']), - $enumDecodeWithDecodeMap(_$StatusCode3EnumDecodeMap, json['statusCode3']), +Person _$PersonFromJson(Map json) => Person( + firstName: json['firstName'] as String, + lastName: json['lastName'] as String, + dateOfBirth: json['dateOfBirth'] == null + ? null + : DateTime.parse(json['dateOfBirth'] as String), ); -Map _$AToJson(A instance) => { - 'statusCode': _$StatusCodeEnumMap[instance.statusCode], - 'statusCode2': _$StatusCode2EnumMap[instance.statusCode2]!, - 'statusCode3': _$StatusCode3EnumMap[instance.statusCode3]!, +Map _$PersonToJson(Person instance) => { + 'firstName': instance.firstName, + 'lastName': instance.lastName, + 'dateOfBirth': instance.dateOfBirth?.toIso8601String(), }; - -const _$StatusCodeEnumMap = { - StatusCode.success: 200, - StatusCode.movedPermanently: 301, - StatusCode.found: 302, - StatusCode.internalServerError: 500, -}; - -const _$StatusCodeEnumDecodeMap = { - 200: StatusCode.success, - 201: StatusCode.success, - 202: StatusCode.success, - 301: StatusCode.movedPermanently, - 302: StatusCode.found, - 500: StatusCode.internalServerError, -}; - -const _$StatusCode2EnumMap = { - StatusCode2.success: 200, - StatusCode2.movedPermanently: 301, - StatusCode2.found: 302, - StatusCode2.internalServerError: 500, -}; - -const _$StatusCode2EnumDecodeMap = { - 200: StatusCode2.success, - 301: StatusCode2.movedPermanently, - 302: StatusCode2.found, - 500: StatusCode2.internalServerError, -}; - -const _$StatusCode3EnumMap = { - StatusCode3.success: 200, - StatusCode3.movedPermanently: 301, - StatusCode3.found: 1000, - StatusCode3.internalServerError: 500, -}; - -const _$StatusCode3EnumDecodeMap = { - 200: StatusCode3.success, - 301: StatusCode3.movedPermanently, - 1000: StatusCode3.found, - 500: StatusCode3.internalServerError, -}; ``` # Running the code generator diff --git a/json_serializable/example/example.dart b/json_serializable/example/example.dart index 36a9ea4f0..39cc11597 100644 --- a/json_serializable/example/example.dart +++ b/json_serializable/example/example.dart @@ -7,50 +7,20 @@ import 'package:json_annotation/json_annotation.dart'; part 'example.g.dart'; @JsonSerializable() -class A { - final StatusCode? statusCode; - final StatusCode2 statusCode2; - final StatusCode3 statusCode3; +class Person { + /// The generated code assumes these values exist in JSON. + final String firstName, lastName; - A(this.statusCode, this.statusCode2, this.statusCode3); + /// The generated code below handles if the corresponding JSON value doesn't + /// exist or is empty. + final DateTime? dateOfBirth; - factory A.fromJson(Map json) => _$AFromJson(json); + Person({required this.firstName, required this.lastName, this.dateOfBirth}); - Map toJson() => _$AToJson(this); -} - -enum StatusCode { - @JsonValue(200, aliases: [201, 202]) - success, - @JsonValue(301) - movedPermanently, - @JsonValue(302) - found, - @JsonValue(500) - internalServerError, -} - -@JsonEnum(valueField: 'code') -enum StatusCode2 { - success(200), - movedPermanently(301), - found(302), - internalServerError(500); - - const StatusCode2(this.code); - - final int code; -} - -@JsonEnum(valueField: 'code') -enum StatusCode3 { - success(200), - movedPermanently(301), - @JsonValue(1000) - found(302), - internalServerError(500); - - const StatusCode3(this.code); + /// Connect the generated [_$PersonFromJson] function to the `fromJson` + /// factory. + factory Person.fromJson(Map json) => _$PersonFromJson(json); - final int code; + /// Connect the generated [_$PersonToJson] function to the `toJson` method. + Map toJson() => _$PersonToJson(this); } diff --git a/json_serializable/example/example.g.dart b/json_serializable/example/example.g.dart index 942901485..0ed069cae 100644 --- a/json_serializable/example/example.g.dart +++ b/json_serializable/example/example.g.dart @@ -8,59 +8,16 @@ part of 'example.dart'; // JsonSerializableGenerator // ************************************************************************** -A _$AFromJson(Map json) => A( - $enumDecodeNullableWithDecodeMap( - _$StatusCodeEnumDecodeMap, json['statusCode']), - $enumDecodeWithDecodeMap(_$StatusCode2EnumDecodeMap, json['statusCode2']), - $enumDecodeWithDecodeMap(_$StatusCode3EnumDecodeMap, json['statusCode3']), +Person _$PersonFromJson(Map json) => Person( + firstName: json['firstName'] as String, + lastName: json['lastName'] as String, + dateOfBirth: json['dateOfBirth'] == null + ? null + : DateTime.parse(json['dateOfBirth'] as String), ); -Map _$AToJson(A instance) => { - 'statusCode': _$StatusCodeEnumMap[instance.statusCode], - 'statusCode2': _$StatusCode2EnumMap[instance.statusCode2]!, - 'statusCode3': _$StatusCode3EnumMap[instance.statusCode3]!, +Map _$PersonToJson(Person instance) => { + 'firstName': instance.firstName, + 'lastName': instance.lastName, + 'dateOfBirth': instance.dateOfBirth?.toIso8601String(), }; - -const _$StatusCodeEnumMap = { - StatusCode.success: 200, - StatusCode.movedPermanently: 301, - StatusCode.found: 302, - StatusCode.internalServerError: 500, -}; - -const _$StatusCodeEnumDecodeMap = { - 200: StatusCode.success, - 201: StatusCode.success, - 202: StatusCode.success, - 301: StatusCode.movedPermanently, - 302: StatusCode.found, - 500: StatusCode.internalServerError, -}; - -const _$StatusCode2EnumMap = { - StatusCode2.success: 200, - StatusCode2.movedPermanently: 301, - StatusCode2.found: 302, - StatusCode2.internalServerError: 500, -}; - -const _$StatusCode2EnumDecodeMap = { - 200: StatusCode2.success, - 301: StatusCode2.movedPermanently, - 302: StatusCode2.found, - 500: StatusCode2.internalServerError, -}; - -const _$StatusCode3EnumMap = { - StatusCode3.success: 200, - StatusCode3.movedPermanently: 301, - StatusCode3.found: 1000, - StatusCode3.internalServerError: 500, -}; - -const _$StatusCode3EnumDecodeMap = { - 200: StatusCode3.success, - 301: StatusCode3.movedPermanently, - 1000: StatusCode3.found, - 500: StatusCode3.internalServerError, -}; From 14e362cebf6b8ed2fa7729ddddf6eda7548359aa Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 15:50:20 -0300 Subject: [PATCH 04/14] fix: add unused_element to decode map --- json_serializable/lib/src/enum_utils.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/json_serializable/lib/src/enum_utils.dart b/json_serializable/lib/src/enum_utils.dart index 8289a0fd9..1e7d962e5 100644 --- a/json_serializable/lib/src/enum_utils.dart +++ b/json_serializable/lib/src/enum_utils.dart @@ -66,7 +66,8 @@ String? enumMapsFromType( final decodeMap = decodeItems == null ? null - : 'const ${constDecodeMapName(targetType)} = {\n$decodeItems\n};'; + : '// ignore: unused_element\n' + 'const ${constDecodeMapName(targetType)} = {\n$decodeItems\n};'; return valuesMap == null && decodeMap == null ? null From 844260332d23d9df22d567b51806e7aac723e264 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 15:53:51 -0300 Subject: [PATCH 05/14] chore: regenerate generated files --- _test_yaml/test/src/build_config.g.dart | 2 ++ json_serializable/test/default_value/default_value.g.dart | 1 + .../default_value/default_value.g_any_map__checked.g.dart | 1 + .../test/default_value/implicit_default_value.g.dart | 1 + .../test/integration/converter_examples.g.dart | 1 + .../integration/create_per_field_to_json_example.g.dart | 1 + .../test/integration/json_enum_example.g.dart | 7 +++++++ .../test/integration/json_test_example.g.dart | 2 ++ .../test/integration/json_test_example.g_any_map.g.dart | 2 ++ .../test/supported_types/input.type_enumtype.g.dart | 1 + .../test/supported_types/input.type_iterable.g.dart | 1 + .../test/supported_types/input.type_list.g.dart | 1 + .../test/supported_types/input.type_map.g.dart | 1 + .../test/supported_types/input.type_record.g.dart | 1 + .../test/supported_types/input.type_set.g.dart | 1 + 15 files changed, 24 insertions(+) diff --git a/_test_yaml/test/src/build_config.g.dart b/_test_yaml/test/src/build_config.g.dart index be3efebc4..89e4868db 100644 --- a/_test_yaml/test/src/build_config.g.dart +++ b/_test_yaml/test/src/build_config.g.dart @@ -45,6 +45,7 @@ const _$AutoApplyEnumMap = { AutoApply.rootPackage: 'root_package', }; +// ignore: unused_element const _$AutoApplyEnumDecodeMap = { 'none': AutoApply.none, 'dependents': AutoApply.dependents, @@ -140,6 +141,7 @@ const _$BuildToEnumMap = { BuildTo.source: 'source', }; +// ignore: unused_element const _$BuildToEnumDecodeMap = { 'cache': BuildTo.cache, 'source': BuildTo.source, diff --git a/json_serializable/test/default_value/default_value.g.dart b/json_serializable/test/default_value/default_value.g.dart index ce6df52cd..fc03dfe0d 100644 --- a/json_serializable/test/default_value/default_value.g.dart +++ b/json_serializable/test/default_value/default_value.g.dart @@ -101,6 +101,7 @@ const _$GreekEnumMap = { Greek.delta: 'delta', }; +// ignore: unused_element const _$GreekEnumDecodeMap = { 'alpha': Greek.alpha, 'beta': Greek.beta, 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 635f31c10..d0d34c4b6 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 @@ -127,6 +127,7 @@ const _$GreekEnumMap = { Greek.delta: 'delta', }; +// ignore: unused_element const _$GreekEnumDecodeMap = { 'alpha': Greek.alpha, 'beta': Greek.beta, 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 2d2bdbd3c..512128d05 100644 --- a/json_serializable/test/default_value/implicit_default_value.g.dart +++ b/json_serializable/test/default_value/implicit_default_value.g.dart @@ -105,6 +105,7 @@ const _$GreekEnumMap = { Greek.delta: 'delta', }; +// ignore: unused_element const _$GreekEnumDecodeMap = { 'alpha': Greek.alpha, 'beta': Greek.beta, diff --git a/json_serializable/test/integration/converter_examples.g.dart b/json_serializable/test/integration/converter_examples.g.dart index a4e490adf..a4c16ed11 100644 --- a/json_serializable/test/integration/converter_examples.g.dart +++ b/json_serializable/test/integration/converter_examples.g.dart @@ -53,6 +53,7 @@ const _$Issue1202RegressionEnumEnumMap = { Issue1202RegressionEnum.nullValue: null, }; +// ignore: unused_element const _$Issue1202RegressionEnumEnumDecodeMap = { 42: Issue1202RegressionEnum.normalValue, null: Issue1202RegressionEnum.nullValue, 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 1a7c5c293..0f40163c5 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 @@ -63,6 +63,7 @@ const _$EnumValueEnumMap = { EnumValue.second: 'second', }; +// ignore: unused_element const _$EnumValueEnumDecodeMap = { '1': EnumValue.first, 'second': EnumValue.second, diff --git a/json_serializable/test/integration/json_enum_example.g.dart b/json_serializable/test/integration/json_enum_example.g.dart index 5d8ed6713..321262fcf 100644 --- a/json_serializable/test/integration/json_enum_example.g.dart +++ b/json_serializable/test/integration/json_enum_example.g.dart @@ -27,6 +27,7 @@ const _$Issue559RegressionEnumEnumMap = { Issue559RegressionEnum.gamma: 'gamma', }; +// ignore: unused_element const _$Issue559RegressionEnumEnumDecodeMap = { 'alpha': Issue559RegressionEnum.alpha, 'beta': Issue559RegressionEnum.beta, @@ -46,6 +47,7 @@ const _$Issue1145RegressionEnumEnumMap = { Issue1145RegressionEnum.gamma: 'gamma', }; +// ignore: unused_element const _$Issue1145RegressionEnumEnumDecodeMap = { 'alpha': Issue1145RegressionEnum.alpha, 'beta': Issue1145RegressionEnum.beta, @@ -91,6 +93,7 @@ const _$EnumWithAliasesEnumMap = { EnumWithAliases.three: '3', }; +// ignore: unused_element const _$EnumWithAliasesEnumDecodeMap = { '1': EnumWithAliases.one, '2': EnumWithAliases.two, @@ -106,6 +109,7 @@ const _$StandAloneEnumEnumMap = { StandAloneEnum.delta: 'd', }; +// ignore: unused_element const _$StandAloneEnumEnumDecodeMap = { 'a': StandAloneEnum.alpha, 'b': StandAloneEnum.beta, @@ -119,6 +123,7 @@ const _$DayTypeEnumMap = { DayType.veryBad: 'very-bad', }; +// ignore: unused_element const _$DayTypeEnumDecodeMap = { 'no-good': DayType.noGood, 'rotten': DayType.rotten, @@ -130,6 +135,7 @@ const _$MyStatusCodeEnumMap = { MyStatusCode.weird: 701, }; +// ignore: unused_element const _$MyStatusCodeEnumDecodeMap = { 200: MyStatusCode.success, 701: MyStatusCode.weird, @@ -141,6 +147,7 @@ const _$EnumValueFieldIndexEnumMap = { EnumValueFieldIndex.oneMore: 2, }; +// ignore: unused_element const _$EnumValueFieldIndexEnumDecodeMap = { 0: EnumValueFieldIndex.success, 701: EnumValueFieldIndex.weird, diff --git a/json_serializable/test/integration/json_test_example.g.dart b/json_serializable/test/integration/json_test_example.g.dart index cd0fd2f48..342172c0a 100644 --- a/json_serializable/test/integration/json_test_example.g.dart +++ b/json_serializable/test/integration/json_test_example.g.dart @@ -58,6 +58,7 @@ const _$CategoryEnumMap = { Category.notDiscoveredYet: 'not_discovered_yet', }; +// ignore: unused_element const _$CategoryEnumDecodeMap = { 'top': Category.top, 'bottom': Category.bottom, @@ -116,6 +117,7 @@ const _$StatusCodeEnumMap = { StatusCode.unknown: 'unknown', }; +// ignore: unused_element const _$StatusCodeEnumDecodeMap = { 200: StatusCode.success, 404: StatusCode.notFound, 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 bebe7f35c..51ff5422b 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 @@ -58,6 +58,7 @@ const _$CategoryEnumMap = { Category.notDiscoveredYet: 'not_discovered_yet', }; +// ignore: unused_element const _$CategoryEnumDecodeMap = { 'top': Category.top, 'bottom': Category.bottom, @@ -116,6 +117,7 @@ const _$StatusCodeEnumMap = { StatusCode.unknown: 'unknown', }; +// ignore: unused_element const _$StatusCodeEnumDecodeMap = { 200: StatusCode.success, 404: StatusCode.notFound, 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 7110c3875..56e83fae1 100644 --- a/json_serializable/test/supported_types/input.type_enumtype.g.dart +++ b/json_serializable/test/supported_types/input.type_enumtype.g.dart @@ -28,6 +28,7 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element const _$EnumTypeEnumDecodeMap = { 'alpha': EnumType.alpha, 'beta': EnumType.beta, 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 075c5503f..11753eaeb 100644 --- a/json_serializable/test/supported_types/input.type_iterable.g.dart +++ b/json_serializable/test/supported_types/input.type_iterable.g.dart @@ -324,6 +324,7 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element const _$EnumTypeEnumDecodeMap = { 'alpha': EnumType.alpha, 'beta': EnumType.beta, 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 2d6d38618..3ccfa961f 100644 --- a/json_serializable/test/supported_types/input.type_list.g.dart +++ b/json_serializable/test/supported_types/input.type_list.g.dart @@ -349,6 +349,7 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element const _$EnumTypeEnumDecodeMap = { 'alpha': EnumType.alpha, 'beta': EnumType.beta, 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 092a7a1d3..8a03cb78c 100644 --- a/json_serializable/test/supported_types/input.type_map.g.dart +++ b/json_serializable/test/supported_types/input.type_map.g.dart @@ -144,6 +144,7 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element const _$EnumTypeEnumDecodeMap = { 'alpha': EnumType.alpha, 'beta': EnumType.beta, 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 1fed2d307..8bb99e23c 100644 --- a/json_serializable/test/supported_types/input.type_record.g.dart +++ b/json_serializable/test/supported_types/input.type_record.g.dart @@ -576,6 +576,7 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element const _$EnumTypeEnumDecodeMap = { 'alpha': EnumType.alpha, 'beta': EnumType.beta, 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 48e52f838..5a6c44111 100644 --- a/json_serializable/test/supported_types/input.type_set.g.dart +++ b/json_serializable/test/supported_types/input.type_set.g.dart @@ -351,6 +351,7 @@ const _$EnumTypeEnumMap = { EnumType.delta: 'delta', }; +// ignore: unused_element const _$EnumTypeEnumDecodeMap = { 'alpha': EnumType.alpha, 'beta': EnumType.beta, From 89775ce654eab9a6cbe2c0ead58f1fa9d235274d Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 17:27:20 -0300 Subject: [PATCH 06/14] chore: change `aliases` from `List` to `Set` --- json_annotation/lib/src/json_value.dart | 4 ++-- json_serializable/lib/src/enum_utils.dart | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/json_annotation/lib/src/json_value.dart b/json_annotation/lib/src/json_value.dart index e9fdb129c..9ada231e5 100644 --- a/json_annotation/lib/src/json_value.dart +++ b/json_annotation/lib/src/json_value.dart @@ -12,7 +12,7 @@ class JsonValue { /// Optional values that can be used when deserializing. /// /// The elements of [aliases] must be either [String] or [int]. - final List aliases; + final Set aliases; - const JsonValue(this.value, {this.aliases = const []}); + 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 1e7d962e5..833124ee8 100644 --- a/json_serializable/lib/src/enum_utils.dart +++ b/json_serializable/lib/src/enum_utils.dart @@ -121,7 +121,7 @@ Map? _enumAliases( jsonEnum: jsonEnum, targetType: targetType, ): field, - for (final alias in _generateAlias(field: field, targetType: targetType)) + for (final alias in _generateAliases(field: field, targetType: targetType)) alias: field, }, }; @@ -192,7 +192,7 @@ Object? _generateEntry({ } } -List _generateAlias({ +List _generateAliases({ required FieldElement field, required DartType targetType, }) { @@ -208,7 +208,7 @@ List _generateAlias({ if (valueReader.validAliasesType) { return [ - for (final value in valueReader.listValue) + for (final value in valueReader.setValue) ConstantReader(value).literalValue, ]; } else { From f17b91936fa0ceeaaefcf5f6597b3a9e1fe59472 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 17:28:31 -0300 Subject: [PATCH 07/14] fix: fix `json_enum_example.dart` --- json_serializable/test/integration/json_enum_example.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_serializable/test/integration/json_enum_example.dart b/json_serializable/test/integration/json_enum_example.dart index 27263260b..f0b81f7d7 100644 --- a/json_serializable/test/integration/json_enum_example.dart +++ b/json_serializable/test/integration/json_enum_example.dart @@ -126,7 +126,7 @@ enum EnumWithAliases { @JsonValue('1') one, - @JsonValue('2', aliases: ['two', 'deux']) + @JsonValue('2', aliases: {'two', 'deux'}) two, @JsonValue('3') From cd8c239bca8ac940ec52010e2429b539f5254706 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 17:29:35 -0300 Subject: [PATCH 08/14] fix: fix `validAliasesType` --- json_serializable/lib/src/enum_utils.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/json_serializable/lib/src/enum_utils.dart b/json_serializable/lib/src/enum_utils.dart index 833124ee8..d2e7e09c3 100644 --- a/json_serializable/lib/src/enum_utils.dart +++ b/json_serializable/lib/src/enum_utils.dart @@ -240,8 +240,8 @@ extension on ConstantReader { bool get validValueType => isString || isNull || isInt; bool get validAliasesType => - isList && - listValue.every((element) => + isSet && + setValue.every((element) => (element.type?.isDartCoreString ?? false) || (element.type?.isDartCoreInt ?? false)); } From 3a98811780f75fb7e08aaabbfebe77c6afba54a2 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 17:33:05 -0300 Subject: [PATCH 09/14] fix: fix tests --- json_serializable/test/src/_json_enum_test_input.dart | 1 + json_serializable/test/src/unknown_enum_value_test_input.dart | 1 + 2 files changed, 2 insertions(+) diff --git a/json_serializable/test/src/_json_enum_test_input.dart b/json_serializable/test/src/_json_enum_test_input.dart index 06b9be9f7..02e126010 100644 --- a/json_serializable/test/src/_json_enum_test_input.dart +++ b/json_serializable/test/src/_json_enum_test_input.dart @@ -11,6 +11,7 @@ const _$EnumValueIssue1147EnumMap = { EnumValueIssue1147.weird: 601, }; +// ignore: unused_element const _$EnumValueIssue1147EnumDecodeMap = { 200: EnumValueIssue1147.success, 601: EnumValueIssue1147.weird, 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 a8d18dd77..0e62cbc7b 100644 --- a/json_serializable/test/src/unknown_enum_value_test_input.dart +++ b/json_serializable/test/src/unknown_enum_value_test_input.dart @@ -19,6 +19,7 @@ const _$UnknownEnumValueItemsEnumMap = { UnknownEnumValueItems.vNull: 'vNull', }; +// ignore: unused_element const _$UnknownEnumValueItemsEnumDecodeMap = { 'v0': UnknownEnumValueItems.v0, 'v1': UnknownEnumValueItems.v1, From c42b66462741bbb1aa2e010b0acd877e8d25424c Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 17:34:37 -0300 Subject: [PATCH 10/14] fix: fix warning about line length --- json_serializable/lib/src/enum_utils.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_serializable/lib/src/enum_utils.dart b/json_serializable/lib/src/enum_utils.dart index d2e7e09c3..f47f4a012 100644 --- a/json_serializable/lib/src/enum_utils.dart +++ b/json_serializable/lib/src/enum_utils.dart @@ -121,7 +121,7 @@ Map? _enumAliases( jsonEnum: jsonEnum, targetType: targetType, ): field, - for (final alias in _generateAliases(field: field, targetType: targetType)) + for (var alias in _generateAliases(field: field, targetType: targetType)) alias: field, }, }; From 00881a517f34f3ed16f9bfdf7b472ce328e4a74f Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 17:35:35 -0300 Subject: [PATCH 11/14] fix: also add `unused_element` to `constMapName` --- json_serializable/lib/src/enum_utils.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/json_serializable/lib/src/enum_utils.dart b/json_serializable/lib/src/enum_utils.dart index f47f4a012..050f09905 100644 --- a/json_serializable/lib/src/enum_utils.dart +++ b/json_serializable/lib/src/enum_utils.dart @@ -54,7 +54,8 @@ String? enumMapsFromType( final valuesMap = valuesItems == null ? null - : 'const ${constMapName(targetType)} = {\n$valuesItems\n};'; + : '// ignore: unused_element\n' + 'const ${constMapName(targetType)} = {\n$valuesItems\n};'; final decodeItems = enumAliases == null ? null From fbf216c3b0f91b24715f33832bbfa247fcd0a1a3 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 17:36:35 -0300 Subject: [PATCH 12/14] chore: regenerate generated files --- _test_yaml/test/src/build_config.g.dart | 2 ++ json_serializable/test/default_value/default_value.g.dart | 1 + .../default_value/default_value.g_any_map__checked.g.dart | 1 + .../test/default_value/implicit_default_value.g.dart | 1 + .../test/integration/converter_examples.g.dart | 1 + .../integration/create_per_field_to_json_example.g.dart | 1 + .../test/integration/json_enum_example.g.dart | 7 +++++++ .../test/integration/json_test_example.g.dart | 2 ++ .../test/integration/json_test_example.g_any_map.g.dart | 2 ++ .../test/supported_types/input.type_enumtype.g.dart | 1 + .../test/supported_types/input.type_iterable.g.dart | 1 + .../test/supported_types/input.type_list.g.dart | 1 + .../test/supported_types/input.type_map.g.dart | 1 + .../test/supported_types/input.type_record.g.dart | 1 + .../test/supported_types/input.type_set.g.dart | 1 + 15 files changed, 24 insertions(+) diff --git a/_test_yaml/test/src/build_config.g.dart b/_test_yaml/test/src/build_config.g.dart index 89e4868db..42127cb29 100644 --- a/_test_yaml/test/src/build_config.g.dart +++ b/_test_yaml/test/src/build_config.g.dart @@ -38,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', @@ -136,6 +137,7 @@ Map _$BuilderToJson(Builder instance) => { if (instance.buildExtensions case final value?) 'build_extensions': value, }; +// ignore: unused_element const _$BuildToEnumMap = { BuildTo.cache: 'cache', BuildTo.source: 'source', diff --git a/json_serializable/test/default_value/default_value.g.dart b/json_serializable/test/default_value/default_value.g.dart index fc03dfe0d..6e49d0c6c 100644 --- a/json_serializable/test/default_value/default_value.g.dart +++ b/json_serializable/test/default_value/default_value.g.dart @@ -94,6 +94,7 @@ Map _$DefaultValueToJson(DefaultValue instance) => instance.valueFromDefaultValueNamedConstructor, }; +// ignore: unused_element const _$GreekEnumMap = { Greek.alpha: 'alpha', Greek.beta: 'beta', 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 d0d34c4b6..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 @@ -120,6 +120,7 @@ Map _$DefaultValueToJson(DefaultValue instance) => instance.valueFromDefaultValueNamedConstructor, }; +// ignore: unused_element const _$GreekEnumMap = { Greek.alpha: 'alpha', Greek.beta: 'beta', 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 512128d05..701135858 100644 --- a/json_serializable/test/default_value/implicit_default_value.g.dart +++ b/json_serializable/test/default_value/implicit_default_value.g.dart @@ -98,6 +98,7 @@ Map _$DefaultValueImplicitToJson( instance.valueFromDefaultValueNamedConstructor, }; +// ignore: unused_element const _$GreekEnumMap = { Greek.alpha: 'alpha', Greek.beta: 'beta', diff --git a/json_serializable/test/integration/converter_examples.g.dart b/json_serializable/test/integration/converter_examples.g.dart index a4c16ed11..6c4fc4d21 100644 --- a/json_serializable/test/integration/converter_examples.g.dart +++ b/json_serializable/test/integration/converter_examples.g.dart @@ -48,6 +48,7 @@ Map _$Issue1202RegressionClassToJson( 'valueWithNullableFunctions': value, }; +// ignore: unused_element const _$Issue1202RegressionEnumEnumMap = { Issue1202RegressionEnum.normalValue: 42, Issue1202RegressionEnum.nullValue: null, 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 0f40163c5..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 @@ -58,6 +58,7 @@ Map _$ModelToJson(Model instance) => { 'nestedExcludeIfNull': value, }; +// ignore: unused_element const _$EnumValueEnumMap = { EnumValue.first: '1', EnumValue.second: 'second', diff --git a/json_serializable/test/integration/json_enum_example.g.dart b/json_serializable/test/integration/json_enum_example.g.dart index 321262fcf..1e78f169b 100644 --- a/json_serializable/test/integration/json_enum_example.g.dart +++ b/json_serializable/test/integration/json_enum_example.g.dart @@ -21,6 +21,7 @@ Issue559Regression _$Issue559RegressionFromJson(Map json) { ); } +// ignore: unused_element const _$Issue559RegressionEnumEnumMap = { Issue559RegressionEnum.alpha: 'alpha', Issue559RegressionEnum.beta: 'beta', @@ -41,6 +42,7 @@ Map _$Issue1145RegressionAToJson( .map((k, e) => MapEntry(_$Issue1145RegressionEnumEnumMap[k]!, e)), }; +// ignore: unused_element const _$Issue1145RegressionEnumEnumMap = { Issue1145RegressionEnum.alpha: 'alpha', Issue1145RegressionEnum.beta: 'beta', @@ -87,6 +89,7 @@ Map _$EnumWithAliasesDataToJson( 'value': _$EnumWithAliasesEnumMap[instance.value]!, }; +// ignore: unused_element const _$EnumWithAliasesEnumMap = { EnumWithAliases.one: '1', EnumWithAliases.two: '2', @@ -102,6 +105,7 @@ const _$EnumWithAliasesEnumDecodeMap = { '3': EnumWithAliases.three, }; +// ignore: unused_element const _$StandAloneEnumEnumMap = { StandAloneEnum.alpha: 'a', StandAloneEnum.beta: 'b', @@ -117,6 +121,7 @@ const _$StandAloneEnumEnumDecodeMap = { 'd': StandAloneEnum.delta, }; +// ignore: unused_element const _$DayTypeEnumMap = { DayType.noGood: 'no-good', DayType.rotten: 'rotten', @@ -130,6 +135,7 @@ const _$DayTypeEnumDecodeMap = { 'very-bad': DayType.veryBad, }; +// ignore: unused_element const _$MyStatusCodeEnumMap = { MyStatusCode.success: 200, MyStatusCode.weird: 701, @@ -141,6 +147,7 @@ const _$MyStatusCodeEnumDecodeMap = { 701: MyStatusCode.weird, }; +// ignore: unused_element const _$EnumValueFieldIndexEnumMap = { EnumValueFieldIndex.success: 0, EnumValueFieldIndex.weird: 701, diff --git a/json_serializable/test/integration/json_test_example.g.dart b/json_serializable/test/integration/json_test_example.g.dart index 342172c0a..03f3b53ba 100644 --- a/json_serializable/test/integration/json_test_example.g.dart +++ b/json_serializable/test/integration/json_test_example.g.dart @@ -48,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', @@ -110,6 +111,7 @@ Map _$OrderToJson(Order instance) => { 'status_code': _$StatusCodeEnumMap[instance.statusCode], }; +// ignore: unused_element const _$StatusCodeEnumMap = { StatusCode.success: 200, StatusCode.notFound: 404, 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 51ff5422b..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 @@ -48,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', @@ -110,6 +111,7 @@ Map _$OrderToJson(Order instance) => { 'status_code': _$StatusCodeEnumMap[instance.statusCode], }; +// ignore: unused_element const _$StatusCodeEnumMap = { StatusCode.success: 200, StatusCode.notFound: 404, 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 56e83fae1..841d889f1 100644 --- a/json_serializable/test/supported_types/input.type_enumtype.g.dart +++ b/json_serializable/test/supported_types/input.type_enumtype.g.dart @@ -21,6 +21,7 @@ Map _$SimpleClassToJson(SimpleClass instance) => 'withDefault': _$EnumTypeEnumMap[instance.withDefault]!, }; +// ignore: unused_element const _$EnumTypeEnumMap = { EnumType.alpha: 'alpha', EnumType.beta: 'beta', 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 11753eaeb..104f16100 100644 --- a/json_serializable/test/supported_types/input.type_iterable.g.dart +++ b/json_serializable/test/supported_types/input.type_iterable.g.dart @@ -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', 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 3ccfa961f..b4f17a06f 100644 --- a/json_serializable/test/supported_types/input.type_list.g.dart +++ b/json_serializable/test/supported_types/input.type_list.g.dart @@ -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', 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 8a03cb78c..cf1f77e4e 100644 --- a/json_serializable/test/supported_types/input.type_map.g.dart +++ b/json_serializable/test/supported_types/input.type_map.g.dart @@ -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', 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 8bb99e23c..dbf67e472 100644 --- a/json_serializable/test/supported_types/input.type_record.g.dart +++ b/json_serializable/test/supported_types/input.type_record.g.dart @@ -569,6 +569,7 @@ Map _$SimpleClassOfEnumTypeToJson( }, }; +// ignore: unused_element const _$EnumTypeEnumMap = { EnumType.alpha: 'alpha', EnumType.beta: 'beta', 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 5a6c44111..282e1fc16 100644 --- a/json_serializable/test/supported_types/input.type_set.g.dart +++ b/json_serializable/test/supported_types/input.type_set.g.dart @@ -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', From 0632c5bac2b23a03fb0aebc8734c29cbc37493d8 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 26 Nov 2024 18:38:05 -0300 Subject: [PATCH 13/14] fix: fix tests --- json_serializable/test/src/_json_enum_test_input.dart | 1 + json_serializable/test/src/unknown_enum_value_test_input.dart | 1 + 2 files changed, 2 insertions(+) diff --git a/json_serializable/test/src/_json_enum_test_input.dart b/json_serializable/test/src/_json_enum_test_input.dart index 02e126010..fed99e481 100644 --- a/json_serializable/test/src/_json_enum_test_input.dart +++ b/json_serializable/test/src/_json_enum_test_input.dart @@ -6,6 +6,7 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:source_gen_test/annotations.dart'; @ShouldGenerate(r''' +// ignore: unused_element const _$EnumValueIssue1147EnumMap = { EnumValueIssue1147.success: 200, EnumValueIssue1147.weird: 601, 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 0e62cbc7b..245bb3f55 100644 --- a/json_serializable/test/src/unknown_enum_value_test_input.dart +++ b/json_serializable/test/src/unknown_enum_value_test_input.dart @@ -11,6 +11,7 @@ UnknownEnumValue _$UnknownEnumValueFromJson(Map json) => unknownValue: UnknownEnumValueItems.vUnknown) ?? UnknownEnumValueItems.vNull; +// ignore: unused_element const _$UnknownEnumValueItemsEnumMap = { UnknownEnumValueItems.v0: 'v0', UnknownEnumValueItems.v1: 'v1', From 79f4ff66d11a352d2564c3bad26bec150a032648 Mon Sep 17 00:00:00 2001 From: "Mateus Felipe C. C. Pinto" Date: Tue, 17 Dec 2024 20:42:42 -0300 Subject: [PATCH 14/14] chore: add test for alias type --- json_annotation/lib/src/json_value.dart | 2 +- json_serializable/lib/src/enum_utils.dart | 2 +- json_serializable/test/json_serializable_test.dart | 1 + json_serializable/test/src/_json_enum_test_input.dart | 10 ++++++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/json_annotation/lib/src/json_value.dart b/json_annotation/lib/src/json_value.dart index 9ada231e5..243d85a03 100644 --- a/json_annotation/lib/src/json_value.dart +++ b/json_annotation/lib/src/json_value.dart @@ -12,7 +12,7 @@ class JsonValue { /// Optional values that can be used when deserializing. /// /// The elements of [aliases] must be either [String] or [int]. - final Set aliases; + 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 050f09905..44f68632e 100644 --- a/json_serializable/lib/src/enum_utils.dart +++ b/json_serializable/lib/src/enum_utils.dart @@ -216,7 +216,7 @@ List _generateAliases({ final targetTypeCode = typeToCode(targetType); throw InvalidGenerationSourceError( 'The `JsonValue` annotation on `$targetTypeCode.${field.name}` aliases ' - 'should all be of type String, or int.', + 'should all be of type String or int.', element: field, ); } 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 fed99e481..91a797c5a 100644 --- a/json_serializable/test/src/_json_enum_test_input.dart +++ b/json_serializable/test/src/_json_enum_test_input.dart @@ -5,6 +5,16 @@ 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 = {