From dbe976a864c599774e64aaa478aef0f0d44cb8e0 Mon Sep 17 00:00:00 2001 From: Jackson Gardner Date: Wed, 24 Apr 2024 11:38:18 -0700 Subject: [PATCH] Make casts to int safe in dart2wasm. (#1416) Co-authored-by: Kevin Moore --- _test_yaml/pubspec.yaml | 2 +- _test_yaml/test/src/build_config.g.dart | 4 +- example/lib/example.g.dart | 13 +- .../lib/generic_response_class_example.g.dart | 8 +- example/lib/json_converter_example.g.dart | 10 +- example/lib/tuple_example.g.dart | 8 +- .../lib/src/json_serializable.dart | 2 +- json_serializable/CHANGELOG.md | 5 +- json_serializable/lib/src/lambda_result.dart | 35 ++-- .../lib/src/type_helpers/duration_helper.dart | 2 +- .../lib/src/type_helpers/value_helper.dart | 22 +-- json_serializable/lib/src/utils.dart | 28 +++ .../test/default_value/default_value.g.dart | 10 +- .../default_value.g_any_map__checked.g.dart | 15 +- .../implicit_default_value.g.dart | 10 +- .../field_matrix_test.field_matrix.g.dart | 90 ++++----- .../generic_argument_factories.g.dart | 2 +- ...generic_argument_factories_nullable.g.dart | 2 +- .../test/generic_files/generic_class.dart | 3 + .../test/generic_files/generic_class.g.dart | 6 +- .../test/generic_files/generic_test.dart | 6 +- .../integration/converter_examples.g.dart | 2 +- .../create_per_field_to_json_example.g.dart | 2 +- .../test/integration/integration_test.dart | 15 +- .../test/integration/json_test_common.dart | 5 + .../test/integration/json_test_example.dart | 3 + .../test/integration/json_test_example.g.dart | 35 ++-- .../json_test_example.g_any_map.dart | 3 + .../json_test_example.g_any_map.g.dart | 35 ++-- .../test/kitchen_sink/kitchen_sink.g.dart | 39 ++-- .../kitchen_sink.g_any_map.g.dart | 39 ++-- .../kitchen_sink.g_any_map__checked.g.dart | 51 ++--- .../kitchen_sink.g_exclude_null.g.dart | 39 ++-- .../kitchen_sink.g_explicit_to_json.g.dart | 39 ++-- .../test/kitchen_sink/simple_object.g.dart | 2 +- .../kitchen_sink/strict_keys_object.g.dart | 2 +- .../src/_json_serializable_test_input.dart | 21 ++- .../test/src/default_value_input.dart | 6 +- .../test/src/inheritance_test_input.dart | 8 +- .../test/src/json_converter_test_input.dart | 22 +-- .../test/src/map_key_variety_test_input.dart | 2 +- .../test/src/to_from_json_test_input.dart | 2 +- .../test/supported_types/enum_type.dart | 6 +- .../input.type_duration.g.dart | 4 +- .../supported_types/input.type_int.g.dart | 8 +- .../input.type_iterable.g.dart | 24 +-- .../supported_types/input.type_list.g.dart | 28 ++- .../supported_types/input.type_map.g.dart | 178 +++++++++--------- .../supported_types/input.type_record.g.dart | 41 ++-- .../supported_types/input.type_set.g.dart | 24 ++- .../tool/readme/readme_examples.g.dart | 6 +- 51 files changed, 551 insertions(+), 423 deletions(-) diff --git a/_test_yaml/pubspec.yaml b/_test_yaml/pubspec.yaml index f682e6e88..57f7a275b 100644 --- a/_test_yaml/pubspec.yaml +++ b/_test_yaml/pubspec.yaml @@ -11,7 +11,7 @@ dev_dependencies: build_verify: ^3.0.0 checked_yaml: any dart_flutter_team_lints: ^2.0.0 - json_annotation: ^4.7.0 + json_annotation: ^4.8.1 json_serializable: any path: ^1.8.2 test: ^1.6.0 diff --git a/_test_yaml/test/src/build_config.g.dart b/_test_yaml/test/src/build_config.g.dart index 332000dd1..61e578ec4 100644 --- a/_test_yaml/test/src/build_config.g.dart +++ b/_test_yaml/test/src/build_config.g.dart @@ -24,8 +24,8 @@ Config _$ConfigFromJson(Map json) => $checkedCreate( $checkedConvert( 'weights', (v) => val.weights = (v as Map?)?.map( - (k, e) => - MapEntry($enumDecode(_$AutoApplyEnumMap, k), e as int?), + (k, e) => MapEntry( + $enumDecode(_$AutoApplyEnumMap, k), (e as num?)?.toInt()), )); return val; }, diff --git a/example/lib/example.g.dart b/example/lib/example.g.dart index 40e7e4e9c..b4cf7de11 100644 --- a/example/lib/example.g.dart +++ b/example/lib/example.g.dart @@ -39,15 +39,16 @@ Map _$PersonToJson(Person instance) { } Order _$OrderFromJson(Map json) => Order( - Order._dateTimeFromEpochUs(json['date'] as int), + Order._dateTimeFromEpochUs((json['date'] as num).toInt()), ) - ..count = json['count'] as int? - ..itemNumber = json['itemNumber'] as int? + ..count = (json['count'] as num?)?.toInt() + ..itemNumber = (json['itemNumber'] as num?)?.toInt() ..isRushed = json['isRushed'] as bool? ..item = json['item'] == null ? null : Item.fromJson(json['item'] as Map) - ..prepTime = Order._durationFromMilliseconds(json['prep-time'] as int?); + ..prepTime = + Order._durationFromMilliseconds((json['prep-time'] as num?)?.toInt()); Map _$OrderToJson(Order instance) { final val = {}; @@ -68,8 +69,8 @@ Map _$OrderToJson(Order instance) { } Item _$ItemFromJson(Map json) => Item() - ..count = json['count'] as int? - ..itemNumber = json['itemNumber'] as int? + ..count = (json['count'] as num?)?.toInt() + ..itemNumber = (json['itemNumber'] as num?)?.toInt() ..isRushed = json['isRushed'] as bool?; Map _$ItemToJson(Item instance) => { diff --git a/example/lib/generic_response_class_example.g.dart b/example/lib/generic_response_class_example.g.dart index 6b2e9325a..e61f09efc 100644 --- a/example/lib/generic_response_class_example.g.dart +++ b/example/lib/generic_response_class_example.g.dart @@ -8,13 +8,13 @@ part of 'generic_response_class_example.dart'; BaseResponse _$BaseResponseFromJson(Map json) => BaseResponse( - status: json['status'] as int?, + status: (json['status'] as num?)?.toInt(), msg: json['msg'] as String?, data: BaseResponse._dataFromJson(json['data'] as Object), ); Article _$ArticleFromJson(Map json) => Article( - id: json['id'] as int, + id: (json['id'] as num).toInt(), title: json['title'] as String, author: json['author'] == null ? null @@ -25,11 +25,11 @@ Article _$ArticleFromJson(Map json) => Article( ); User _$UserFromJson(Map json) => User( - id: json['id'] as int?, + id: (json['id'] as num?)?.toInt(), email: json['email'] as String?, ); Comment _$CommentFromJson(Map json) => Comment( - id: json['id'] as int?, + id: (json['id'] as num?)?.toInt(), content: json['content'] as String?, ); diff --git a/example/lib/json_converter_example.g.dart b/example/lib/json_converter_example.g.dart index 526177be7..01b2e46f5 100644 --- a/example/lib/json_converter_example.g.dart +++ b/example/lib/json_converter_example.g.dart @@ -8,7 +8,7 @@ part of 'json_converter_example.dart'; DateTimeExample _$DateTimeExampleFromJson(Map json) => DateTimeExample( - const _DateTimeEpochConverter().fromJson(json['when'] as int), + const _DateTimeEpochConverter().fromJson((json['when'] as num).toInt()), ); Map _$DateTimeExampleToJson(DateTimeExample instance) => @@ -19,9 +19,9 @@ Map _$DateTimeExampleToJson(DateTimeExample instance) => GenericCollection _$GenericCollectionFromJson( Map json) => GenericCollection( - page: json['page'] as int?, - totalResults: json['total_results'] as int?, - totalPages: json['total_pages'] as int?, + page: (json['page'] as num?)?.toInt(), + totalResults: (json['total_results'] as num?)?.toInt(), + totalPages: (json['total_pages'] as num?)?.toInt(), results: (json['results'] as List?) ?.map(_Converter().fromJson) .toList(), @@ -38,7 +38,7 @@ Map _$GenericCollectionToJson( CustomResult _$CustomResultFromJson(Map json) => CustomResult( json['name'] as String, - json['size'] as int, + (json['size'] as num).toInt(), ); Map _$CustomResultToJson(CustomResult instance) => diff --git a/example/lib/tuple_example.g.dart b/example/lib/tuple_example.g.dart index e90d91a1d..f4a537901 100644 --- a/example/lib/tuple_example.g.dart +++ b/example/lib/tuple_example.g.dart @@ -28,11 +28,13 @@ Map _$TupleToJson( ConcreteClass _$ConcreteClassFromJson(Map json) => ConcreteClass( - Tuple.fromJson(json['tuple1'] as Map, - (value) => value as int, (value) => DateTime.parse(value as String)), + Tuple.fromJson( + json['tuple1'] as Map, + (value) => (value as num).toInt(), + (value) => DateTime.parse(value as String)), Tuple.fromJson( json['tuple2'] as Map, - (value) => Duration(microseconds: value as int), + (value) => Duration(microseconds: (value as num).toInt()), (value) => BigInt.parse(value as String)), ); diff --git a/json_annotation/lib/src/json_serializable.dart b/json_annotation/lib/src/json_serializable.dart index fe9b5cc98..91600f097 100644 --- a/json_annotation/lib/src/json_serializable.dart +++ b/json_annotation/lib/src/json_serializable.dart @@ -181,7 +181,7 @@ class JsonSerializable { /// T Function(Object json) fromJsonT, /// ) { /// return Response() - /// ..status = json['status'] as int + /// ..status = (json['status'] as num).toInt() /// ..value = fromJsonT(json['value']); /// } /// diff --git a/json_serializable/CHANGELOG.md b/json_serializable/CHANGELOG.md index 845d563b2..9ae8f098d 100644 --- a/json_serializable/CHANGELOG.md +++ b/json_serializable/CHANGELOG.md @@ -1,8 +1,11 @@ ## 6.8.0-wip - Add type arguments to `Map` literals used for `Record` serialization. -- Added support for generating `ExampleJsonKeys`, exposing a secured way to access the json keys from the properties. +- Added support for generating `ExampleJsonKeys`, exposing a secured way to + access the json keys from the properties. ([#1164](https://github.com/google/json_serializable.dart/pull/1164)) +- Handle decoding an `int` value from a `double` literal. + This now matches the behavior of `double` values being encoded as `int`. ## 6.7.1 diff --git a/json_serializable/lib/src/lambda_result.dart b/json_serializable/lib/src/lambda_result.dart index 0531cc20f..c5aa510ff 100644 --- a/json_serializable/lib/src/lambda_result.dart +++ b/json_serializable/lib/src/lambda_result.dart @@ -20,9 +20,8 @@ class LambdaResult { final String lambda; final DartType? asContent; - String get _asContent => asContent == null ? '' : _asStatement(asContent!); - - String get _fullExpression => '$expression$_asContent'; + String get _fullExpression => + asContent != null ? _cast(expression, asContent!) : expression; LambdaResult(this.expression, this.lambda, {this.asContent}); @@ -35,29 +34,35 @@ class LambdaResult { : '($closureArg) => $subField'; } -String _asStatement(DartType type) { - if (type.isLikeDynamic) { - return ''; +String _cast(String expression, DartType targetType) { + if (targetType.isLikeDynamic) { + return expression; } - final nullableSuffix = type.isNullableType ? '?' : ''; + final nullableSuffix = targetType.isNullableType ? '?' : ''; - if (coreIterableTypeChecker.isAssignableFromType(type)) { - final itemType = coreIterableGenericType(type); + if (coreIterableTypeChecker.isAssignableFromType(targetType)) { + final itemType = coreIterableGenericType(targetType); if (itemType.isLikeDynamic) { - return ' as List$nullableSuffix'; + return '$expression as List$nullableSuffix'; } } - if (coreMapTypeChecker.isAssignableFromType(type)) { - final args = type.typeArgumentsOf(coreMapTypeChecker)!; + if (coreMapTypeChecker.isAssignableFromType(targetType)) { + final args = targetType.typeArgumentsOf(coreMapTypeChecker)!; assert(args.length == 2); if (args.every((e) => e.isLikeDynamic)) { - return ' as Map$nullableSuffix'; + return '$expression as Map$nullableSuffix'; } } - final typeCode = typeToCode(type); - return ' as $typeCode'; + final defaultDecodeValue = defaultDecodeLogic(targetType, expression); + + if (defaultDecodeValue != null) { + return defaultDecodeValue; + } + + final typeCode = typeToCode(targetType); + return '$expression as $typeCode'; } diff --git a/json_serializable/lib/src/type_helpers/duration_helper.dart b/json_serializable/lib/src/type_helpers/duration_helper.dart index d45cb4222..6cf4d6bdd 100644 --- a/json_serializable/lib/src/type_helpers/duration_helper.dart +++ b/json_serializable/lib/src/type_helpers/duration_helper.dart @@ -46,7 +46,7 @@ class DurationHelper extends TypeHelper { return DefaultContainer( expression, - 'Duration(microseconds: $expression as int)', + 'Duration(microseconds: ($expression as num).toInt())', ); } } diff --git a/json_serializable/lib/src/type_helpers/value_helper.dart b/json_serializable/lib/src/type_helpers/value_helper.dart index 49f7372f4..d9f8411db 100644 --- a/json_serializable/lib/src/type_helpers/value_helper.dart +++ b/json_serializable/lib/src/type_helpers/value_helper.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/dart/element/type.dart'; -import 'package:source_helper/source_helper.dart'; import '../shared_checkers.dart'; import '../type_helper.dart'; @@ -35,22 +34,7 @@ class ValueHelper extends TypeHelper { String expression, TypeHelperContext context, bool defaultProvided, - ) { - if (targetType.isDartCoreObject && !targetType.isNullableType) { - final question = defaultProvided ? '?' : ''; - return '$expression as Object$question'; - } else if (targetType.isDartCoreObject || targetType is DynamicType) { - // just return it as-is. We'll hope it's safe. - return expression; - } else if (targetType.isDartCoreDouble) { - final targetTypeNullable = defaultProvided || targetType.isNullableType; - final question = targetTypeNullable ? '?' : ''; - return '($expression as num$question)$question.toDouble()'; - } else if (simpleJsonTypeChecker.isAssignableFromType(targetType)) { - final typeCode = typeToCode(targetType, forceNullable: defaultProvided); - return '$expression as $typeCode'; - } - - return null; - } + ) => + defaultDecodeLogic(targetType, expression, + defaultProvided: defaultProvided); } diff --git a/json_serializable/lib/src/utils.dart b/json_serializable/lib/src/utils.dart index c74cda361..0bf6dba64 100644 --- a/json_serializable/lib/src/utils.dart +++ b/json_serializable/lib/src/utils.dart @@ -9,6 +9,7 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:source_gen/source_gen.dart'; import 'package:source_helper/source_helper.dart'; +import 'shared_checkers.dart'; import 'type_helpers/config_types.dart'; const _jsonKeyChecker = TypeChecker.fromRuntime(JsonKey); @@ -220,6 +221,33 @@ String typeToCode( throw UnimplementedError('(${type.runtimeType}) $type'); } +String? defaultDecodeLogic( + DartType targetType, + String expression, { + bool defaultProvided = false, +}) { + if (targetType.isDartCoreObject && !targetType.isNullableType) { + final question = defaultProvided ? '?' : ''; + return '$expression as Object$question'; + } else if (targetType.isDartCoreObject || targetType is DynamicType) { + // just return it as-is. We'll hope it's safe. + return expression; + } else if (targetType.isDartCoreDouble) { + final targetTypeNullable = defaultProvided || targetType.isNullableType; + final question = targetTypeNullable ? '?' : ''; + return '($expression as num$question)$question.toDouble()'; + } else if (targetType.isDartCoreInt) { + final targetTypeNullable = defaultProvided || targetType.isNullableType; + final question = targetTypeNullable ? '?' : ''; + return '($expression as num$question)$question.toInt()'; + } else if (simpleJsonTypeChecker.isAssignableFromType(targetType)) { + final typeCode = typeToCode(targetType, forceNullable: defaultProvided); + return '$expression as $typeCode'; + } + + return null; +} + extension ExecutableElementExtension on ExecutableElement { /// Returns the name of `this` qualified with the class name if it's a /// [MethodElement]. diff --git a/json_serializable/test/default_value/default_value.g.dart b/json_serializable/test/default_value/default_value.g.dart index 0614284e6..6bc4525d5 100644 --- a/json_serializable/test/default_value/default_value.g.dart +++ b/json_serializable/test/default_value/default_value.g.dart @@ -11,13 +11,13 @@ part of 'default_value.dart'; DefaultValue _$DefaultValueFromJson(Map json) => DefaultValue( json['fieldBool'] as bool? ?? true, json['fieldString'] as String? ?? 'string', - json['fieldInt'] as int? ?? 42, + (json['fieldInt'] as num?)?.toInt() ?? 42, (json['fieldDouble'] as num?)?.toDouble() ?? 3.14, json['fieldListEmpty'] as List? ?? [], (json['fieldSetEmpty'] as List?)?.toSet() ?? {}, json['fieldMapEmpty'] as Map? ?? {}, (json['fieldListSimple'] as List?) - ?.map((e) => e as int) + ?.map((e) => (e as num).toInt()) .toList() ?? [1, 2, 3], (json['fieldSetSimple'] as List?) @@ -25,7 +25,7 @@ DefaultValue _$DefaultValueFromJson(Map json) => DefaultValue( .toSet() ?? {'entry1', 'entry2'}, (json['fieldMapSimple'] as Map?)?.map( - (k, e) => MapEntry(k, e as int), + (k, e) => MapEntry(k, (e as num).toInt()), ) ?? {'answer': 42}, (json['fieldMapListString'] as Map?)?.map( @@ -38,7 +38,7 @@ DefaultValue _$DefaultValueFromJson(Map json) => DefaultValue( $enumDecodeNullable(_$GreekEnumMap, json['fieldEnum']) ?? Greek.beta, durationField: json['durationField'] == null ? Duration.zero - : Duration(microseconds: json['durationField'] as int), + : Duration(microseconds: (json['durationField'] as num).toInt()), constClass: json['constClass'] == null ? const ConstClass('value') : ConstClass.fromJson(json['constClass'] as Map), @@ -50,7 +50,7 @@ DefaultValue _$DefaultValueFromJson(Map json) => DefaultValue( ? const ConstClass('value') : constClassFromJson(json['valueFromFunction'] as String), intDefaultValueFromFunction: - json['intDefaultValueFromFunction'] as int? ?? + (json['intDefaultValueFromFunction'] as num?)?.toInt() ?? intDefaultValueFunction(), valueFromDefaultValueDefaultConstructor: json['valueFromDefaultValueDefaultConstructor'] == null 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 24be86d8a..9ce12321f 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 @@ -15,7 +15,7 @@ DefaultValue _$DefaultValueFromJson(Map json) => $checkedCreate( final val = DefaultValue( $checkedConvert('fieldBool', (v) => v as bool? ?? true), $checkedConvert('fieldString', (v) => v as String? ?? 'string'), - $checkedConvert('fieldInt', (v) => v as int? ?? 42), + $checkedConvert('fieldInt', (v) => (v as num?)?.toInt() ?? 42), $checkedConvert( 'fieldDouble', (v) => (v as num?)?.toDouble() ?? 3.14), $checkedConvert('fieldListEmpty', (v) => v as List? ?? []), @@ -25,7 +25,9 @@ DefaultValue _$DefaultValueFromJson(Map json) => $checkedCreate( $checkedConvert( 'fieldListSimple', (v) => - (v as List?)?.map((e) => e as int).toList() ?? + (v as List?) + ?.map((e) => (e as num).toInt()) + .toList() ?? [1, 2, 3]), $checkedConvert( 'fieldSetSimple', @@ -36,7 +38,7 @@ DefaultValue _$DefaultValueFromJson(Map json) => $checkedCreate( 'fieldMapSimple', (v) => (v as Map?)?.map( - (k, e) => MapEntry(k as String, e as int), + (k, e) => MapEntry(k as String, (e as num).toInt()), ) ?? {'answer': 42}), $checkedConvert( @@ -53,8 +55,9 @@ DefaultValue _$DefaultValueFromJson(Map json) => $checkedCreate( (v) => $enumDecodeNullable(_$GreekEnumMap, v) ?? Greek.beta), durationField: $checkedConvert( 'durationField', - (v) => - v == null ? Duration.zero : Duration(microseconds: v as int)), + (v) => v == null + ? Duration.zero + : Duration(microseconds: (v as num).toInt())), constClass: $checkedConvert( 'constClass', (v) => v == null @@ -72,7 +75,7 @@ DefaultValue _$DefaultValueFromJson(Map json) => $checkedCreate( : constClassFromJson(v as String)), intDefaultValueFromFunction: $checkedConvert( 'intDefaultValueFromFunction', - (v) => v as int? ?? intDefaultValueFunction()), + (v) => (v as num?)?.toInt() ?? intDefaultValueFunction()), valueFromDefaultValueDefaultConstructor: $checkedConvert( 'valueFromDefaultValueDefaultConstructor', (v) => v == null 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 9bd1f19e0..408c1f0b2 100644 --- a/json_serializable/test/default_value/implicit_default_value.g.dart +++ b/json_serializable/test/default_value/implicit_default_value.g.dart @@ -13,14 +13,14 @@ DefaultValueImplicit _$DefaultValueImplicitFromJson( DefaultValueImplicit( fieldBool: json['fieldBool'] as bool? ?? true, fieldString: json['fieldString'] as String? ?? 'string', - fieldInt: json['fieldInt'] as int? ?? 42, + fieldInt: (json['fieldInt'] as num?)?.toInt() ?? 42, fieldDouble: (json['fieldDouble'] as num?)?.toDouble() ?? 3.14, fieldListEmpty: json['fieldListEmpty'] as List? ?? const [], fieldSetEmpty: (json['fieldSetEmpty'] as List?)?.toSet() ?? const {}, fieldMapEmpty: json['fieldMapEmpty'] as Map? ?? const {}, fieldListSimple: (json['fieldListSimple'] as List?) - ?.map((e) => e as int) + ?.map((e) => (e as num).toInt()) .toList() ?? const [1, 2, 3], fieldSetSimple: (json['fieldSetSimple'] as List?) @@ -28,7 +28,7 @@ DefaultValueImplicit _$DefaultValueImplicitFromJson( .toSet() ?? const {'entry1', 'entry2'}, fieldMapSimple: (json['fieldMapSimple'] as Map?)?.map( - (k, e) => MapEntry(k, e as int), + (k, e) => MapEntry(k, (e as num).toInt()), ) ?? const {'answer': 42}, fieldMapListString: @@ -43,7 +43,7 @@ DefaultValueImplicit _$DefaultValueImplicitFromJson( $enumDecodeNullable(_$GreekEnumMap, json['fieldEnum']) ?? Greek.beta, durationField: json['durationField'] == null ? const Duration() - : Duration(microseconds: json['durationField'] as int), + : Duration(microseconds: (json['durationField'] as num).toInt()), constClass: json['constClass'] == null ? const ConstClass('value') : ConstClass.fromJson(json['constClass'] as Map), @@ -55,7 +55,7 @@ DefaultValueImplicit _$DefaultValueImplicitFromJson( ? const ConstClass('value') : constClassFromJson(json['valueFromFunction'] as String), intDefaultValueFromFunction: - json['intDefaultValueFromFunction'] as int? ?? 43, + (json['intDefaultValueFromFunction'] as num?)?.toInt() ?? 43, valueFromDefaultValueDefaultConstructor: json['valueFromDefaultValueDefaultConstructor'] == null ? const ConstClass() diff --git a/json_serializable/test/field_matrix_test.field_matrix.g.dart b/json_serializable/test/field_matrix_test.field_matrix.g.dart index 84891ef9a..aa18728ba 100644 --- a/json_serializable/test/field_matrix_test.field_matrix.g.dart +++ b/json_serializable/test/field_matrix_test.field_matrix.g.dart @@ -11,9 +11,9 @@ part of 'field_matrix_test.field_matrix.dart'; ToJsonNullFromJsonNullPublic _$ToJsonNullFromJsonNullPublicFromJson( Map json) => ToJsonNullFromJsonNullPublic() - ..aField = json['aField'] as int? - ..field = json['field'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..field = (json['field'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonNullFromJsonNullPublicToJson( ToJsonNullFromJsonNullPublic instance) => @@ -26,9 +26,9 @@ Map _$ToJsonNullFromJsonNullPublicToJson( ToJsonNullFromJsonTruePublic _$ToJsonNullFromJsonTruePublicFromJson( Map json) => ToJsonNullFromJsonTruePublic() - ..aField = json['aField'] as int? - ..field = json['field'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..field = (json['field'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonNullFromJsonTruePublicToJson( ToJsonNullFromJsonTruePublic instance) => @@ -41,8 +41,8 @@ Map _$ToJsonNullFromJsonTruePublicToJson( ToJsonNullFromJsonFalsePublic _$ToJsonNullFromJsonFalsePublicFromJson( Map json) => ToJsonNullFromJsonFalsePublic() - ..aField = json['aField'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonNullFromJsonFalsePublicToJson( ToJsonNullFromJsonFalsePublic instance) => @@ -54,9 +54,9 @@ Map _$ToJsonNullFromJsonFalsePublicToJson( ToJsonTrueFromJsonNullPublic _$ToJsonTrueFromJsonNullPublicFromJson( Map json) => ToJsonTrueFromJsonNullPublic() - ..aField = json['aField'] as int? - ..field = json['field'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..field = (json['field'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonTrueFromJsonNullPublicToJson( ToJsonTrueFromJsonNullPublic instance) => @@ -69,9 +69,9 @@ Map _$ToJsonTrueFromJsonNullPublicToJson( ToJsonTrueFromJsonTruePublic _$ToJsonTrueFromJsonTruePublicFromJson( Map json) => ToJsonTrueFromJsonTruePublic() - ..aField = json['aField'] as int? - ..field = json['field'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..field = (json['field'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonTrueFromJsonTruePublicToJson( ToJsonTrueFromJsonTruePublic instance) => @@ -84,8 +84,8 @@ Map _$ToJsonTrueFromJsonTruePublicToJson( ToJsonTrueFromJsonFalsePublic _$ToJsonTrueFromJsonFalsePublicFromJson( Map json) => ToJsonTrueFromJsonFalsePublic() - ..aField = json['aField'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonTrueFromJsonFalsePublicToJson( ToJsonTrueFromJsonFalsePublic instance) => @@ -98,9 +98,9 @@ Map _$ToJsonTrueFromJsonFalsePublicToJson( ToJsonFalseFromJsonNullPublic _$ToJsonFalseFromJsonNullPublicFromJson( Map json) => ToJsonFalseFromJsonNullPublic() - ..aField = json['aField'] as int? - ..field = json['field'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..field = (json['field'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonFalseFromJsonNullPublicToJson( ToJsonFalseFromJsonNullPublic instance) => @@ -112,9 +112,9 @@ Map _$ToJsonFalseFromJsonNullPublicToJson( ToJsonFalseFromJsonTruePublic _$ToJsonFalseFromJsonTruePublicFromJson( Map json) => ToJsonFalseFromJsonTruePublic() - ..aField = json['aField'] as int? - ..field = json['field'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..field = (json['field'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonFalseFromJsonTruePublicToJson( ToJsonFalseFromJsonTruePublic instance) => @@ -126,8 +126,8 @@ Map _$ToJsonFalseFromJsonTruePublicToJson( ToJsonFalseFromJsonFalsePublic _$ToJsonFalseFromJsonFalsePublicFromJson( Map json) => ToJsonFalseFromJsonFalsePublic() - ..aField = json['aField'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonFalseFromJsonFalsePublicToJson( ToJsonFalseFromJsonFalsePublic instance) => @@ -139,8 +139,8 @@ Map _$ToJsonFalseFromJsonFalsePublicToJson( ToJsonNullFromJsonNullPrivate _$ToJsonNullFromJsonNullPrivateFromJson( Map json) => ToJsonNullFromJsonNullPrivate() - ..aField = json['aField'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonNullFromJsonNullPrivateToJson( ToJsonNullFromJsonNullPrivate instance) => @@ -152,9 +152,9 @@ Map _$ToJsonNullFromJsonNullPrivateToJson( ToJsonNullFromJsonTruePrivate _$ToJsonNullFromJsonTruePrivateFromJson( Map json) => ToJsonNullFromJsonTruePrivate() - ..aField = json['aField'] as int? - .._field = json['field'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + .._field = (json['field'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonNullFromJsonTruePrivateToJson( ToJsonNullFromJsonTruePrivate instance) => @@ -167,8 +167,8 @@ Map _$ToJsonNullFromJsonTruePrivateToJson( ToJsonNullFromJsonFalsePrivate _$ToJsonNullFromJsonFalsePrivateFromJson( Map json) => ToJsonNullFromJsonFalsePrivate() - ..aField = json['aField'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonNullFromJsonFalsePrivateToJson( ToJsonNullFromJsonFalsePrivate instance) => @@ -180,8 +180,8 @@ Map _$ToJsonNullFromJsonFalsePrivateToJson( ToJsonTrueFromJsonNullPrivate _$ToJsonTrueFromJsonNullPrivateFromJson( Map json) => ToJsonTrueFromJsonNullPrivate() - ..aField = json['aField'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonTrueFromJsonNullPrivateToJson( ToJsonTrueFromJsonNullPrivate instance) => @@ -194,9 +194,9 @@ Map _$ToJsonTrueFromJsonNullPrivateToJson( ToJsonTrueFromJsonTruePrivate _$ToJsonTrueFromJsonTruePrivateFromJson( Map json) => ToJsonTrueFromJsonTruePrivate() - ..aField = json['aField'] as int? - .._field = json['field'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + .._field = (json['field'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonTrueFromJsonTruePrivateToJson( ToJsonTrueFromJsonTruePrivate instance) => @@ -209,8 +209,8 @@ Map _$ToJsonTrueFromJsonTruePrivateToJson( ToJsonTrueFromJsonFalsePrivate _$ToJsonTrueFromJsonFalsePrivateFromJson( Map json) => ToJsonTrueFromJsonFalsePrivate() - ..aField = json['aField'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonTrueFromJsonFalsePrivateToJson( ToJsonTrueFromJsonFalsePrivate instance) => @@ -223,8 +223,8 @@ Map _$ToJsonTrueFromJsonFalsePrivateToJson( ToJsonFalseFromJsonNullPrivate _$ToJsonFalseFromJsonNullPrivateFromJson( Map json) => ToJsonFalseFromJsonNullPrivate() - ..aField = json['aField'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonFalseFromJsonNullPrivateToJson( ToJsonFalseFromJsonNullPrivate instance) => @@ -236,9 +236,9 @@ Map _$ToJsonFalseFromJsonNullPrivateToJson( ToJsonFalseFromJsonTruePrivate _$ToJsonFalseFromJsonTruePrivateFromJson( Map json) => ToJsonFalseFromJsonTruePrivate() - ..aField = json['aField'] as int? - .._field = json['field'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + .._field = (json['field'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonFalseFromJsonTruePrivateToJson( ToJsonFalseFromJsonTruePrivate instance) => @@ -250,8 +250,8 @@ Map _$ToJsonFalseFromJsonTruePrivateToJson( ToJsonFalseFromJsonFalsePrivate _$ToJsonFalseFromJsonFalsePrivateFromJson( Map json) => ToJsonFalseFromJsonFalsePrivate() - ..aField = json['aField'] as int? - ..zField = json['zField'] as int?; + ..aField = (json['aField'] as num?)?.toInt() + ..zField = (json['zField'] as num?)?.toInt(); Map _$ToJsonFalseFromJsonFalsePrivateToJson( ToJsonFalseFromJsonFalsePrivate instance) => diff --git a/json_serializable/test/generic_files/generic_argument_factories.g.dart b/json_serializable/test/generic_files/generic_argument_factories.g.dart index 188ac7116..3ae8ac8e1 100644 --- a/json_serializable/test/generic_files/generic_argument_factories.g.dart +++ b/json_serializable/test/generic_files/generic_argument_factories.g.dart @@ -34,7 +34,7 @@ ConcreteClass _$ConcreteClassFromJson(Map json) => ConcreteClass( GenericClassWithHelpers.fromJson( json['value'] as Map, - (value) => value as int, + (value) => (value as num).toInt(), (value) => value as String), GenericClassWithHelpers.fromJson( json['value2'] as Map, diff --git a/json_serializable/test/generic_files/generic_argument_factories_nullable.g.dart b/json_serializable/test/generic_files/generic_argument_factories_nullable.g.dart index 17b278eed..626fa171a 100644 --- a/json_serializable/test/generic_files/generic_argument_factories_nullable.g.dart +++ b/json_serializable/test/generic_files/generic_argument_factories_nullable.g.dart @@ -56,7 +56,7 @@ ConcreteClassNullable _$ConcreteClassNullableFromJson( ConcreteClassNullable( GenericClassWithHelpersNullable.fromJson( json['value'] as Map, - (value) => value as int, + (value) => (value as num).toInt(), (value) => value as String), GenericClassWithHelpersNullable.fromJson( json['value2'] as Map, diff --git a/json_serializable/test/generic_files/generic_class.dart b/json_serializable/test/generic_files/generic_class.dart index 0870c956f..0ebfe7d1e 100644 --- a/json_serializable/test/generic_files/generic_class.dart +++ b/json_serializable/test/generic_files/generic_class.dart @@ -48,12 +48,15 @@ class GenericClass { @_DurationMillisecondConverter.named() @_DurationListMillisecondConverter() class GenericClassWithConverter { + // TODO: this annotation is a no-op. Need to figure out what to do about it! @_SimpleConverter() Object? fieldObject; + // TODO: this annotation is a no-op. Need to figure out what to do about it! @_SimpleConverter() dynamic fieldDynamic; + // TODO: this annotation is a no-op. Need to figure out what to do about it! @_SimpleConverter() int? fieldInt; diff --git a/json_serializable/test/generic_files/generic_class.g.dart b/json_serializable/test/generic_files/generic_class.g.dart index c5809241c..104eec1d6 100644 --- a/json_serializable/test/generic_files/generic_class.g.dart +++ b/json_serializable/test/generic_files/generic_class.g.dart @@ -38,15 +38,15 @@ GenericClassWithConverter GenericClassWithConverter() ..fieldObject = json['fieldObject'] ..fieldDynamic = json['fieldDynamic'] - ..fieldInt = json['fieldInt'] as int? + ..fieldInt = (json['fieldInt'] as num?)?.toInt() ..fieldT = _$JsonConverterFromJson, T>( json['fieldT'], _SimpleConverter().fromJson) ..fieldS = _$JsonConverterFromJson, S>( json['fieldS'], _SimpleConverter().fromJson) ..duration = const _DurationMillisecondConverter.named() - .fromJson(json['duration'] as int?) + .fromJson((json['duration'] as num?)?.toInt()) ..listDuration = const _DurationListMillisecondConverter() - .fromJson(json['listDuration'] as int?); + .fromJson((json['listDuration'] as num?)?.toInt()); Map _$GenericClassWithConverterToJson( GenericClassWithConverter instance) => diff --git a/json_serializable/test/generic_files/generic_test.dart b/json_serializable/test/generic_files/generic_test.dart index e4f82d6f3..ea509a471 100644 --- a/json_serializable/test/generic_files/generic_test.dart +++ b/json_serializable/test/generic_files/generic_test.dart @@ -76,7 +76,7 @@ void main() { final decoded = GenericClassWithHelpers.fromJson( jsonDecode(encodedJson) as Map, (value) => DateTime.parse(value as String), - (value) => Duration(milliseconds: value as int), + (value) => Duration(milliseconds: (value as num).toInt()), ); final encodedJson2 = loudEncode( @@ -194,7 +194,7 @@ void main() { GenericClassWithHelpersNullable.fromJson( jsonDecode(encodedJson) as Map, (value) => DateTime.parse(value as String), - (value) => Duration(milliseconds: value as int), + (value) => Duration(milliseconds: (value as num).toInt()), ); final encodedJson2 = loudEncode( @@ -225,7 +225,7 @@ void main() { GenericClassWithHelpersNullable.fromJson( jsonDecode(encodedJson) as Map, (value) => DateTime.parse(value as String), - (value) => Duration(milliseconds: value as int), + (value) => Duration(milliseconds: (value as num).toInt()), ); final encodedJson2 = loudEncode( diff --git a/json_serializable/test/integration/converter_examples.g.dart b/json_serializable/test/integration/converter_examples.g.dart index 26da277f0..2829672a9 100644 --- a/json_serializable/test/integration/converter_examples.g.dart +++ b/json_serializable/test/integration/converter_examples.g.dart @@ -12,7 +12,7 @@ Issue1202RegressionClass _$Issue1202RegressionClassFromJson( Map json) => Issue1202RegressionClass( value: $enumDecode(_$Issue1202RegressionEnumEnumMap, json['value']), - normalNullableValue: json['normalNullableValue'] as int?, + normalNullableValue: (json['normalNullableValue'] as num?)?.toInt(), notNullableValueWithNullableConverter: const _Issue1202RegressionConverter().fromJson( json['notNullableValueWithNullableConverter'] as String?), 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 0587c26ba..1c4b0fe88 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 @@ -23,7 +23,7 @@ Model _$ModelFromJson(Map json) => Model( ? null : GenericFactory.fromJson( json['nestedGeneric'] as Map, - (value) => value as int), + (value) => (value as num).toInt()), ); // ignore: unused_element diff --git a/json_serializable/test/integration/integration_test.dart b/json_serializable/test/integration/integration_test.dart index 8c5827ee8..6aef150f2 100644 --- a/json_serializable/test/integration/integration_test.dart +++ b/json_serializable/test/integration/integration_test.dart @@ -258,18 +258,23 @@ void main() { test('support ints as doubles', () { final value = { 'doubles': [0, 0.0], - 'nnDoubles': [0, 0.0] + 'nnDoubles': [0, 0.0], + 'doubleAsString': 3, }; - roundTripNumber(Numbers.fromJson(value)); + final output = roundTripObject(Numbers.fromJson(value), Numbers.fromJson); + expect(output.doubleAsString, 3.0.toString()); }); - test('does not support doubles as ints', () { + test('support doubles as ints', () { final value = { - 'ints': [3.14, 0], + 'ints': [3, 3.0, 3.14, 0], }; - expect(() => Numbers.fromJson(value), throwsTypeError); + final output = roundTripObject(Numbers.fromJson(value), Numbers.fromJson); + + // NOTE: all of the double values are truncated + expect(output.ints, [3, 3, 3, 0]); }); }); diff --git a/json_serializable/test/integration/json_test_common.dart b/json_serializable/test/integration/json_test_common.dart index d16c3b954..27415fb81 100644 --- a/json_serializable/test/integration/json_test_common.dart +++ b/json_serializable/test/integration/json_test_common.dart @@ -38,6 +38,11 @@ Duration? durationFromInt(int? ms) => int? durationToInt(Duration? duration) => duration?.inMilliseconds; +String? stringFromDouble(double? value) => value?.toString(); + +double? stringToDouble(String? value) => + value == null ? null : double.parse(value); + DateTime? dateTimeFromEpochUs(int? us) => us == null ? null : DateTime.fromMicrosecondsSinceEpoch(us); diff --git a/json_serializable/test/integration/json_test_example.dart b/json_serializable/test/integration/json_test_example.dart index d69be6022..7f3482935 100644 --- a/json_serializable/test/integration/json_test_example.dart +++ b/json_serializable/test/integration/json_test_example.dart @@ -155,6 +155,9 @@ class Numbers { @JsonKey(fromJson: durationFromInt, toJson: durationToInt) Duration? duration; + @JsonKey(fromJson: stringFromDouble, toJson: stringToDouble) + String? doubleAsString; + @JsonKey(fromJson: dateTimeFromEpochUs, toJson: dateTimeToEpochUs) DateTime? date; diff --git a/json_serializable/test/integration/json_test_example.g.dart b/json_serializable/test/integration/json_test_example.g.dart index b6a5330d4..ec12eb554 100644 --- a/json_serializable/test/integration/json_test_example.g.dart +++ b/json_serializable/test/integration/json_test_example.g.dart @@ -29,7 +29,8 @@ Person _$PersonFromJson(Map json) => Person( (k, e) => MapEntry(k, $enumDecode(_$CategoryEnumMap, e)), ) ..categoryCounts = (json['categoryCounts'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$CategoryEnumMap, k), e as int), + (k, e) => + MapEntry($enumDecode(_$CategoryEnumMap, k), (e as num).toInt()), ); Map _$PersonToJson(Person instance) => { @@ -66,11 +67,11 @@ Order _$OrderFromJson(Map json) { (json['items'] as List?) ?.map((e) => Item.fromJson(e as Map)), ) - ..count = json['count'] as int? + ..count = (json['count'] as num?)?.toInt() ..isRushed = json['isRushed'] as bool? ..duration = json['duration'] == null ? null - : Duration(microseconds: json['duration'] as int) + : Duration(microseconds: (json['duration'] as num).toInt()) ..platform = json['platform'] == null ? null : Platform.fromJson(json['platform'] as String) @@ -113,13 +114,15 @@ const _$StatusCodeEnumMap = { }; Item _$ItemFromJson(Map json) => Item( - json['price'] as int?, + (json['price'] as num?)?.toInt(), ) - ..itemNumber = json['item-number'] as int? + ..itemNumber = (json['item-number'] as num?)?.toInt() ..saleDates = (json['saleDates'] as List?) ?.map((e) => DateTime.parse(e as String)) .toList() - ..rates = (json['rates'] as List?)?.map((e) => e as int).toList() + ..rates = (json['rates'] as List?) + ?.map((e) => (e as num).toInt()) + .toList() ..geoPoint = _fromJsonGeoPoint(json['geoPoint'] as Map?); Map _$ItemToJson(Item instance) { @@ -142,7 +145,8 @@ Map _$ItemToJson(Item instance) { } Numbers _$NumbersFromJson(Map json) => Numbers() - ..ints = (json['ints'] as List?)?.map((e) => e as int).toList() + ..ints = + (json['ints'] as List?)?.map((e) => (e as num).toInt()).toList() ..nums = (json['nums'] as List?)?.map((e) => e as num).toList() ..doubles = (json['doubles'] as List?) ?.map((e) => (e as num).toDouble()) @@ -150,8 +154,10 @@ Numbers _$NumbersFromJson(Map json) => Numbers() ..nnDoubles = (json['nnDoubles'] as List?) ?.map((e) => (e as num).toDouble()) .toList() - ..duration = durationFromInt(json['duration'] as int?) - ..date = dateTimeFromEpochUs(json['date'] as int?); + ..duration = durationFromInt((json['duration'] as num?)?.toInt()) + ..doubleAsString = + stringFromDouble((json['doubleAsString'] as num?)?.toDouble()) + ..date = dateTimeFromEpochUs((json['date'] as num?)?.toInt()); Map _$NumbersToJson(Numbers instance) => { 'ints': instance.ints, @@ -159,22 +165,23 @@ Map _$NumbersToJson(Numbers instance) => { 'doubles': instance.doubles, 'nnDoubles': instance.nnDoubles, 'duration': durationToInt(instance.duration), + 'doubleAsString': stringToDouble(instance.doubleAsString), 'date': dateTimeToEpochUs(instance.date), }; MapKeyVariety _$MapKeyVarietyFromJson(Map json) => MapKeyVariety() ..intIntMap = (json['intIntMap'] as Map?)?.map( - (k, e) => MapEntry(int.parse(k), e as int), + (k, e) => MapEntry(int.parse(k), (e as num).toInt()), ) ..uriIntMap = (json['uriIntMap'] as Map?)?.map( - (k, e) => MapEntry(Uri.parse(k), e as int), + (k, e) => MapEntry(Uri.parse(k), (e as num).toInt()), ) ..dateTimeIntMap = (json['dateTimeIntMap'] as Map?)?.map( - (k, e) => MapEntry(DateTime.parse(k), e as int), + (k, e) => MapEntry(DateTime.parse(k), (e as num).toInt()), ) ..bigIntMap = (json['bigIntMap'] as Map?)?.map( - (k, e) => MapEntry(BigInt.parse(k), e as int), + (k, e) => MapEntry(BigInt.parse(k), (e as num).toInt()), ); Map _$MapKeyVarietyToJson(MapKeyVariety instance) => @@ -204,7 +211,7 @@ UnknownEnumValue _$UnknownEnumValueFromJson(Map json) => PrivateConstructor _$PrivateConstructorFromJson(Map json) => PrivateConstructor._( - json['id'] as int, + (json['id'] as num).toInt(), json['value'] as String, ); diff --git a/json_serializable/test/integration/json_test_example.g_any_map.dart b/json_serializable/test/integration/json_test_example.g_any_map.dart index 2c6b51bda..b4789725d 100644 --- a/json_serializable/test/integration/json_test_example.g_any_map.dart +++ b/json_serializable/test/integration/json_test_example.g_any_map.dart @@ -161,6 +161,9 @@ class Numbers { @JsonKey(fromJson: durationFromInt, toJson: durationToInt) Duration? duration; + @JsonKey(fromJson: stringFromDouble, toJson: stringToDouble) + String? doubleAsString; + @JsonKey(fromJson: dateTimeFromEpochUs, toJson: dateTimeToEpochUs) DateTime? date; 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 b8ba1f9f4..5fe79152e 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 @@ -29,7 +29,8 @@ Person _$PersonFromJson(Map json) => Person( (k, e) => MapEntry(k as String, $enumDecode(_$CategoryEnumMap, e)), ) ..categoryCounts = (json['categoryCounts'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$CategoryEnumMap, k), e as int), + (k, e) => + MapEntry($enumDecode(_$CategoryEnumMap, k), (e as num).toInt()), ); Map _$PersonToJson(Person instance) => { @@ -66,11 +67,11 @@ Order _$OrderFromJson(Map json) { (json['items'] as List?) ?.map((e) => Item.fromJson(Map.from(e as Map))), ) - ..count = json['count'] as int? + ..count = (json['count'] as num?)?.toInt() ..isRushed = json['isRushed'] as bool? ..duration = json['duration'] == null ? null - : Duration(microseconds: json['duration'] as int) + : Duration(microseconds: (json['duration'] as num).toInt()) ..platform = json['platform'] == null ? null : Platform.fromJson(json['platform'] as String) @@ -113,13 +114,15 @@ const _$StatusCodeEnumMap = { }; Item _$ItemFromJson(Map json) => Item( - json['price'] as int?, + (json['price'] as num?)?.toInt(), ) - ..itemNumber = json['item-number'] as int? + ..itemNumber = (json['item-number'] as num?)?.toInt() ..saleDates = (json['saleDates'] as List?) ?.map((e) => DateTime.parse(e as String)) .toList() - ..rates = (json['rates'] as List?)?.map((e) => e as int).toList() + ..rates = (json['rates'] as List?) + ?.map((e) => (e as num).toInt()) + .toList() ..geoPoint = _fromJsonGeoPoint(json['geoPoint'] as Map?); Map _$ItemToJson(Item instance) { @@ -142,7 +145,8 @@ Map _$ItemToJson(Item instance) { } Numbers _$NumbersFromJson(Map json) => Numbers() - ..ints = (json['ints'] as List?)?.map((e) => e as int).toList() + ..ints = + (json['ints'] as List?)?.map((e) => (e as num).toInt()).toList() ..nums = (json['nums'] as List?)?.map((e) => e as num).toList() ..doubles = (json['doubles'] as List?) ?.map((e) => (e as num).toDouble()) @@ -150,8 +154,10 @@ Numbers _$NumbersFromJson(Map json) => Numbers() ..nnDoubles = (json['nnDoubles'] as List?) ?.map((e) => (e as num).toDouble()) .toList() - ..duration = durationFromInt(json['duration'] as int?) - ..date = dateTimeFromEpochUs(json['date'] as int?); + ..duration = durationFromInt((json['duration'] as num?)?.toInt()) + ..doubleAsString = + stringFromDouble((json['doubleAsString'] as num?)?.toDouble()) + ..date = dateTimeFromEpochUs((json['date'] as num?)?.toInt()); Map _$NumbersToJson(Numbers instance) => { 'ints': instance.ints, @@ -159,21 +165,22 @@ Map _$NumbersToJson(Numbers instance) => { 'doubles': instance.doubles, 'nnDoubles': instance.nnDoubles, 'duration': durationToInt(instance.duration), + 'doubleAsString': stringToDouble(instance.doubleAsString), 'date': dateTimeToEpochUs(instance.date), }; MapKeyVariety _$MapKeyVarietyFromJson(Map json) => MapKeyVariety() ..intIntMap = (json['intIntMap'] as Map?)?.map( - (k, e) => MapEntry(int.parse(k as String), e as int), + (k, e) => MapEntry(int.parse(k as String), (e as num).toInt()), ) ..uriIntMap = (json['uriIntMap'] as Map?)?.map( - (k, e) => MapEntry(Uri.parse(k as String), e as int), + (k, e) => MapEntry(Uri.parse(k as String), (e as num).toInt()), ) ..dateTimeIntMap = (json['dateTimeIntMap'] as Map?)?.map( - (k, e) => MapEntry(DateTime.parse(k as String), e as int), + (k, e) => MapEntry(DateTime.parse(k as String), (e as num).toInt()), ) ..bigIntMap = (json['bigIntMap'] as Map?)?.map( - (k, e) => MapEntry(BigInt.parse(k as String), e as int), + (k, e) => MapEntry(BigInt.parse(k as String), (e as num).toInt()), ); Map _$MapKeyVarietyToJson(MapKeyVariety instance) => @@ -202,7 +209,7 @@ UnknownEnumValue _$UnknownEnumValueFromJson(Map json) => UnknownEnumValue() PrivateConstructor _$PrivateConstructorFromJson(Map json) => PrivateConstructor._( - json['id'] as int, + (json['id'] as num).toInt(), json['value'] as String, ); diff --git a/json_serializable/test/kitchen_sink/kitchen_sink.g.dart b/json_serializable/test/kitchen_sink/kitchen_sink.g.dart index d9230fbc5..6d376fdfe 100644 --- a/json_serializable/test/kitchen_sink/kitchen_sink.g.dart +++ b/json_serializable/test/kitchen_sink/kitchen_sink.g.dart @@ -9,13 +9,13 @@ part of 'kitchen_sink.dart'; // ************************************************************************** KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( - ctorValidatedNo42: json['no-42'] as int?, + ctorValidatedNo42: (json['no-42'] as num?)?.toInt(), iterable: _valueAccessor(json, 'iterable') as List?, dynamicIterable: json['dynamicIterable'] as List?, objectIterable: (json['objectIterable'] as List?)?.map((e) => e as Object), - intIterable: - (json['intIterable'] as List?)?.map((e) => e as int), + intIterable: (json['intIterable'] as List?) + ?.map((e) => (e as num).toInt()), dateTimeIterable: (json['datetime-iterable'] as List?) ?.map((e) => DateTime.parse(e as String)), ) @@ -28,7 +28,9 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( ..dynamicSet = (json['dynamicSet'] as List).toSet() ..objectSet = (json['objectSet'] as List).map((e) => e as Object).toSet() - ..intSet = (json['intSet'] as List).map((e) => e as int).toSet() + ..intSet = (json['intSet'] as List) + .map((e) => (e as num).toInt()) + .toSet() ..dateTimeSet = (json['dateTimeSet'] as List) .map((e) => DateTime.parse(e as String)) .toSet() @@ -36,8 +38,9 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( ..dynamicList = json['dynamicList'] as List ..objectList = (json['objectList'] as List).map((e) => e as Object).toList() - ..intList = - (json['intList'] as List).map((e) => e as int).toList() + ..intList = (json['intList'] as List) + .map((e) => (e as num).toInt()) + .toList() ..dateTimeList = (json['dateTimeList'] as List) .map((e) => DateTime.parse(e as String)) .toList() @@ -85,11 +88,11 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( SimpleObject.fromJson(json['simpleObject'] as Map) ..strictKeysObject = StrictKeysObject.fromJson( json['strictKeysObject'] as Map) - ..validatedPropertyNo42 = json['validatedPropertyNo42'] as int? + ..validatedPropertyNo42 = (json['validatedPropertyNo42'] as num?)?.toInt() ..recordField = _$recordConvertNullable( json['recordField'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -158,9 +161,11 @@ $Rec? _$recordConvertNullable<$Rec>( JsonConverterTestClass _$JsonConverterTestClassFromJson( Map json) => JsonConverterTestClass( - const DurationMillisecondConverter().fromJson(json['duration'] as int?), + const DurationMillisecondConverter() + .fromJson((json['duration'] as num?)?.toInt()), (json['durationList'] as List) - .map((e) => const DurationMillisecondConverter().fromJson(e as int?)) + .map((e) => const DurationMillisecondConverter() + .fromJson((e as num?)?.toInt())) .toList(), const BigIntStringConverter().fromJson(json['bigInt'] as String), (json['bigIntMap'] as Map).map( @@ -175,16 +180,20 @@ JsonConverterTestClass _$JsonConverterTestClassFromJson( _$JsonConverterFromJson( e, const BigIntStringConverter().fromJson)), ), - TrivialNumberConverter.instance.fromJson(json['numberSilly'] as int?), + TrivialNumberConverter.instance + .fromJson((json['numberSilly'] as num?)?.toInt()), (json['numberSillySet'] as List) - .map((e) => TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => + TrivialNumberConverter.instance.fromJson((e as num?)?.toInt())) .toSet(), - const EpochDateTimeConverter().fromJson(json['dateTime'] as int?), + const EpochDateTimeConverter() + .fromJson((json['dateTime'] as num?)?.toInt()), trivialStringConverter.fromJson(json['trivialString'] as String?), TrivialNumberConverter.instance - .fromJson(json['nullableNumberSilly'] as int?), + .fromJson((json['nullableNumberSilly'] as num?)?.toInt()), (json['nullableNumberSillySet'] as List) - .map((e) => TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => + TrivialNumberConverter.instance.fromJson((e as num?)?.toInt())) .toSet(), ); diff --git a/json_serializable/test/kitchen_sink/kitchen_sink.g_any_map.g.dart b/json_serializable/test/kitchen_sink/kitchen_sink.g_any_map.g.dart index 74a348f5e..69301303d 100644 --- a/json_serializable/test/kitchen_sink/kitchen_sink.g_any_map.g.dart +++ b/json_serializable/test/kitchen_sink/kitchen_sink.g_any_map.g.dart @@ -9,13 +9,13 @@ part of 'kitchen_sink.g_any_map.dart'; // ************************************************************************** KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( - ctorValidatedNo42: json['no-42'] as int?, + ctorValidatedNo42: (json['no-42'] as num?)?.toInt(), iterable: _valueAccessor(json, 'iterable') as List?, dynamicIterable: json['dynamicIterable'] as List?, objectIterable: (json['objectIterable'] as List?)?.map((e) => e as Object), - intIterable: - (json['intIterable'] as List?)?.map((e) => e as int), + intIterable: (json['intIterable'] as List?) + ?.map((e) => (e as num).toInt()), dateTimeIterable: (json['datetime-iterable'] as List?) ?.map((e) => DateTime.parse(e as String)), ) @@ -28,7 +28,9 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( ..dynamicSet = (json['dynamicSet'] as List).toSet() ..objectSet = (json['objectSet'] as List).map((e) => e as Object).toSet() - ..intSet = (json['intSet'] as List).map((e) => e as int).toSet() + ..intSet = (json['intSet'] as List) + .map((e) => (e as num).toInt()) + .toSet() ..dateTimeSet = (json['dateTimeSet'] as List) .map((e) => DateTime.parse(e as String)) .toSet() @@ -36,8 +38,9 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( ..dynamicList = json['dynamicList'] as List ..objectList = (json['objectList'] as List).map((e) => e as Object).toList() - ..intList = - (json['intList'] as List).map((e) => e as int).toList() + ..intList = (json['intList'] as List) + .map((e) => (e as num).toInt()) + .toList() ..dateTimeList = (json['dateTimeList'] as List) .map((e) => DateTime.parse(e as String)) .toList() @@ -77,11 +80,11 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( ..simpleObject = SimpleObject.fromJson(json['simpleObject'] as Map) ..strictKeysObject = StrictKeysObject.fromJson(json['strictKeysObject'] as Map) - ..validatedPropertyNo42 = json['validatedPropertyNo42'] as int? + ..validatedPropertyNo42 = (json['validatedPropertyNo42'] as num?)?.toInt() ..recordField = _$recordConvertAnyNullable( json['recordField'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -149,9 +152,11 @@ $Rec? _$recordConvertAnyNullable<$Rec>( JsonConverterTestClass _$JsonConverterTestClassFromJson(Map json) => JsonConverterTestClass( - const DurationMillisecondConverter().fromJson(json['duration'] as int?), + const DurationMillisecondConverter() + .fromJson((json['duration'] as num?)?.toInt()), (json['durationList'] as List) - .map((e) => const DurationMillisecondConverter().fromJson(e as int?)) + .map((e) => const DurationMillisecondConverter() + .fromJson((e as num?)?.toInt())) .toList(), const BigIntStringConverter().fromJson(json['bigInt'] as String), (json['bigIntMap'] as Map).map( @@ -166,16 +171,20 @@ JsonConverterTestClass _$JsonConverterTestClassFromJson(Map json) => _$JsonConverterFromJson( e, const BigIntStringConverter().fromJson)), ), - TrivialNumberConverter.instance.fromJson(json['numberSilly'] as int?), + TrivialNumberConverter.instance + .fromJson((json['numberSilly'] as num?)?.toInt()), (json['numberSillySet'] as List) - .map((e) => TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => + TrivialNumberConverter.instance.fromJson((e as num?)?.toInt())) .toSet(), - const EpochDateTimeConverter().fromJson(json['dateTime'] as int?), + const EpochDateTimeConverter() + .fromJson((json['dateTime'] as num?)?.toInt()), trivialStringConverter.fromJson(json['trivialString'] as String?), TrivialNumberConverter.instance - .fromJson(json['nullableNumberSilly'] as int?), + .fromJson((json['nullableNumberSilly'] as num?)?.toInt()), (json['nullableNumberSillySet'] as List) - .map((e) => TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => + TrivialNumberConverter.instance.fromJson((e as num?)?.toInt())) .toSet(), ); diff --git a/json_serializable/test/kitchen_sink/kitchen_sink.g_any_map__checked.g.dart b/json_serializable/test/kitchen_sink/kitchen_sink.g_any_map__checked.g.dart index d08da32c5..c004dcc74 100644 --- a/json_serializable/test/kitchen_sink/kitchen_sink.g_any_map__checked.g.dart +++ b/json_serializable/test/kitchen_sink/kitchen_sink.g_any_map__checked.g.dart @@ -13,7 +13,8 @@ KitchenSink _$KitchenSinkFromJson(Map json) => $checkedCreate( json, ($checkedConvert) { final val = KitchenSink( - ctorValidatedNo42: $checkedConvert('no-42', (v) => v as int?), + ctorValidatedNo42: + $checkedConvert('no-42', (v) => (v as num?)?.toInt()), iterable: $checkedConvert( 'iterable', (v) => v as List?, @@ -24,7 +25,7 @@ KitchenSink _$KitchenSinkFromJson(Map json) => $checkedCreate( objectIterable: $checkedConvert('objectIterable', (v) => (v as List?)?.map((e) => e as Object)), intIterable: $checkedConvert('intIterable', - (v) => (v as List?)?.map((e) => e as int)), + (v) => (v as List?)?.map((e) => (e as num).toInt())), dateTimeIterable: $checkedConvert( 'datetime-iterable', (v) => (v as List?) @@ -45,8 +46,8 @@ KitchenSink _$KitchenSinkFromJson(Map json) => $checkedCreate( (v as List).map((e) => e as Object).toSet()); $checkedConvert( 'intSet', - (v) => - val.intSet = (v as List).map((e) => e as int).toSet()); + (v) => val.intSet = + (v as List).map((e) => (e as num).toInt()).toSet()); $checkedConvert( 'dateTimeSet', (v) => val.dateTimeSet = (v as List) @@ -62,7 +63,7 @@ KitchenSink _$KitchenSinkFromJson(Map json) => $checkedCreate( $checkedConvert( 'intList', (v) => val.intList = - (v as List).map((e) => e as int).toList()); + (v as List).map((e) => (e as num).toInt()).toList()); $checkedConvert( 'dateTimeList', (v) => val.dateTimeList = (v as List) @@ -120,13 +121,13 @@ KitchenSink _$KitchenSinkFromJson(Map json) => $checkedCreate( $checkedConvert('strictKeysObject', (v) => val.strictKeysObject = StrictKeysObject.fromJson(v as Map)); $checkedConvert('validatedPropertyNo42', - (v) => val.validatedPropertyNo42 = v as int?); + (v) => val.validatedPropertyNo42 = (v as num?)?.toInt()); $checkedConvert( 'recordField', (v) => val.recordField = _$recordConvertAnyNullable( v, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -206,13 +207,15 @@ JsonConverterTestClass _$JsonConverterTestClassFromJson(Map json) => json, ($checkedConvert) { final val = JsonConverterTestClass( - $checkedConvert('duration', - (v) => const DurationMillisecondConverter().fromJson(v as int?)), + $checkedConvert( + 'duration', + (v) => const DurationMillisecondConverter() + .fromJson((v as num?)?.toInt())), $checkedConvert( 'durationList', (v) => (v as List) - .map((e) => - const DurationMillisecondConverter().fromJson(e as int?)) + .map((e) => const DurationMillisecondConverter() + .fromJson((e as num?)?.toInt())) .toList()), $checkedConvert('bigInt', (v) => const BigIntStringConverter().fromJson(v as String)), @@ -234,25 +237,31 @@ JsonConverterTestClass _$JsonConverterTestClassFromJson(Map json) => _$JsonConverterFromJson( e, const BigIntStringConverter().fromJson)), )), - $checkedConvert('numberSilly', - (v) => TrivialNumberConverter.instance.fromJson(v as int?)), + $checkedConvert( + 'numberSilly', + (v) => TrivialNumberConverter.instance + .fromJson((v as num?)?.toInt())), $checkedConvert( 'numberSillySet', (v) => (v as List) - .map((e) => - TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => TrivialNumberConverter.instance + .fromJson((e as num?)?.toInt())) .toSet()), - $checkedConvert('dateTime', - (v) => const EpochDateTimeConverter().fromJson(v as int?)), + $checkedConvert( + 'dateTime', + (v) => const EpochDateTimeConverter() + .fromJson((v as num?)?.toInt())), $checkedConvert('trivialString', (v) => trivialStringConverter.fromJson(v as String?)), - $checkedConvert('nullableNumberSilly', - (v) => TrivialNumberConverter.instance.fromJson(v as int?)), + $checkedConvert( + 'nullableNumberSilly', + (v) => TrivialNumberConverter.instance + .fromJson((v as num?)?.toInt())), $checkedConvert( 'nullableNumberSillySet', (v) => (v as List) - .map((e) => - TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => TrivialNumberConverter.instance + .fromJson((e as num?)?.toInt())) .toSet()), ); return val; diff --git a/json_serializable/test/kitchen_sink/kitchen_sink.g_exclude_null.g.dart b/json_serializable/test/kitchen_sink/kitchen_sink.g_exclude_null.g.dart index 76504317c..a3a8f8550 100644 --- a/json_serializable/test/kitchen_sink/kitchen_sink.g_exclude_null.g.dart +++ b/json_serializable/test/kitchen_sink/kitchen_sink.g_exclude_null.g.dart @@ -9,13 +9,13 @@ part of 'kitchen_sink.g_exclude_null.dart'; // ************************************************************************** KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( - ctorValidatedNo42: json['no-42'] as int?, + ctorValidatedNo42: (json['no-42'] as num?)?.toInt(), iterable: _valueAccessor(json, 'iterable') as List?, dynamicIterable: json['dynamicIterable'] as List?, objectIterable: (json['objectIterable'] as List?)?.map((e) => e as Object), - intIterable: - (json['intIterable'] as List?)?.map((e) => e as int), + intIterable: (json['intIterable'] as List?) + ?.map((e) => (e as num).toInt()), dateTimeIterable: (json['datetime-iterable'] as List?) ?.map((e) => DateTime.parse(e as String)), ) @@ -28,7 +28,9 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( ..dynamicSet = (json['dynamicSet'] as List).toSet() ..objectSet = (json['objectSet'] as List).map((e) => e as Object).toSet() - ..intSet = (json['intSet'] as List).map((e) => e as int).toSet() + ..intSet = (json['intSet'] as List) + .map((e) => (e as num).toInt()) + .toSet() ..dateTimeSet = (json['dateTimeSet'] as List) .map((e) => DateTime.parse(e as String)) .toSet() @@ -36,8 +38,9 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( ..dynamicList = json['dynamicList'] as List ..objectList = (json['objectList'] as List).map((e) => e as Object).toList() - ..intList = - (json['intList'] as List).map((e) => e as int).toList() + ..intList = (json['intList'] as List) + .map((e) => (e as num).toInt()) + .toList() ..dateTimeList = (json['dateTimeList'] as List) .map((e) => DateTime.parse(e as String)) .toList() @@ -85,11 +88,11 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( SimpleObject.fromJson(json['simpleObject'] as Map) ..strictKeysObject = StrictKeysObject.fromJson( json['strictKeysObject'] as Map) - ..validatedPropertyNo42 = json['validatedPropertyNo42'] as int? + ..validatedPropertyNo42 = (json['validatedPropertyNo42'] as num?)?.toInt() ..recordField = _$recordConvertNullable( json['recordField'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -168,9 +171,11 @@ $Rec? _$recordConvertNullable<$Rec>( JsonConverterTestClass _$JsonConverterTestClassFromJson( Map json) => JsonConverterTestClass( - const DurationMillisecondConverter().fromJson(json['duration'] as int?), + const DurationMillisecondConverter() + .fromJson((json['duration'] as num?)?.toInt()), (json['durationList'] as List) - .map((e) => const DurationMillisecondConverter().fromJson(e as int?)) + .map((e) => const DurationMillisecondConverter() + .fromJson((e as num?)?.toInt())) .toList(), const BigIntStringConverter().fromJson(json['bigInt'] as String), (json['bigIntMap'] as Map).map( @@ -185,16 +190,20 @@ JsonConverterTestClass _$JsonConverterTestClassFromJson( _$JsonConverterFromJson( e, const BigIntStringConverter().fromJson)), ), - TrivialNumberConverter.instance.fromJson(json['numberSilly'] as int?), + TrivialNumberConverter.instance + .fromJson((json['numberSilly'] as num?)?.toInt()), (json['numberSillySet'] as List) - .map((e) => TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => + TrivialNumberConverter.instance.fromJson((e as num?)?.toInt())) .toSet(), - const EpochDateTimeConverter().fromJson(json['dateTime'] as int?), + const EpochDateTimeConverter() + .fromJson((json['dateTime'] as num?)?.toInt()), trivialStringConverter.fromJson(json['trivialString'] as String?), TrivialNumberConverter.instance - .fromJson(json['nullableNumberSilly'] as int?), + .fromJson((json['nullableNumberSilly'] as num?)?.toInt()), (json['nullableNumberSillySet'] as List) - .map((e) => TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => + TrivialNumberConverter.instance.fromJson((e as num?)?.toInt())) .toSet(), ); diff --git a/json_serializable/test/kitchen_sink/kitchen_sink.g_explicit_to_json.g.dart b/json_serializable/test/kitchen_sink/kitchen_sink.g_explicit_to_json.g.dart index ff092269c..0115b3fff 100644 --- a/json_serializable/test/kitchen_sink/kitchen_sink.g_explicit_to_json.g.dart +++ b/json_serializable/test/kitchen_sink/kitchen_sink.g_explicit_to_json.g.dart @@ -9,13 +9,13 @@ part of 'kitchen_sink.g_explicit_to_json.dart'; // ************************************************************************** KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( - ctorValidatedNo42: json['no-42'] as int?, + ctorValidatedNo42: (json['no-42'] as num?)?.toInt(), iterable: _valueAccessor(json, 'iterable') as List?, dynamicIterable: json['dynamicIterable'] as List?, objectIterable: (json['objectIterable'] as List?)?.map((e) => e as Object), - intIterable: - (json['intIterable'] as List?)?.map((e) => e as int), + intIterable: (json['intIterable'] as List?) + ?.map((e) => (e as num).toInt()), dateTimeIterable: (json['datetime-iterable'] as List?) ?.map((e) => DateTime.parse(e as String)), ) @@ -28,7 +28,9 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( ..dynamicSet = (json['dynamicSet'] as List).toSet() ..objectSet = (json['objectSet'] as List).map((e) => e as Object).toSet() - ..intSet = (json['intSet'] as List).map((e) => e as int).toSet() + ..intSet = (json['intSet'] as List) + .map((e) => (e as num).toInt()) + .toSet() ..dateTimeSet = (json['dateTimeSet'] as List) .map((e) => DateTime.parse(e as String)) .toSet() @@ -36,8 +38,9 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( ..dynamicList = json['dynamicList'] as List ..objectList = (json['objectList'] as List).map((e) => e as Object).toList() - ..intList = - (json['intList'] as List).map((e) => e as int).toList() + ..intList = (json['intList'] as List) + .map((e) => (e as num).toInt()) + .toList() ..dateTimeList = (json['dateTimeList'] as List) .map((e) => DateTime.parse(e as String)) .toList() @@ -85,11 +88,11 @@ KitchenSink _$KitchenSinkFromJson(Map json) => KitchenSink( SimpleObject.fromJson(json['simpleObject'] as Map) ..strictKeysObject = StrictKeysObject.fromJson( json['strictKeysObject'] as Map) - ..validatedPropertyNo42 = json['validatedPropertyNo42'] as int? + ..validatedPropertyNo42 = (json['validatedPropertyNo42'] as num?)?.toInt() ..recordField = _$recordConvertNullable( json['recordField'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -160,9 +163,11 @@ $Rec? _$recordConvertNullable<$Rec>( JsonConverterTestClass _$JsonConverterTestClassFromJson( Map json) => JsonConverterTestClass( - const DurationMillisecondConverter().fromJson(json['duration'] as int?), + const DurationMillisecondConverter() + .fromJson((json['duration'] as num?)?.toInt()), (json['durationList'] as List) - .map((e) => const DurationMillisecondConverter().fromJson(e as int?)) + .map((e) => const DurationMillisecondConverter() + .fromJson((e as num?)?.toInt())) .toList(), const BigIntStringConverter().fromJson(json['bigInt'] as String), (json['bigIntMap'] as Map).map( @@ -177,16 +182,20 @@ JsonConverterTestClass _$JsonConverterTestClassFromJson( _$JsonConverterFromJson( e, const BigIntStringConverter().fromJson)), ), - TrivialNumberConverter.instance.fromJson(json['numberSilly'] as int?), + TrivialNumberConverter.instance + .fromJson((json['numberSilly'] as num?)?.toInt()), (json['numberSillySet'] as List) - .map((e) => TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => + TrivialNumberConverter.instance.fromJson((e as num?)?.toInt())) .toSet(), - const EpochDateTimeConverter().fromJson(json['dateTime'] as int?), + const EpochDateTimeConverter() + .fromJson((json['dateTime'] as num?)?.toInt()), trivialStringConverter.fromJson(json['trivialString'] as String?), TrivialNumberConverter.instance - .fromJson(json['nullableNumberSilly'] as int?), + .fromJson((json['nullableNumberSilly'] as num?)?.toInt()), (json['nullableNumberSillySet'] as List) - .map((e) => TrivialNumberConverter.instance.fromJson(e as int?)) + .map((e) => + TrivialNumberConverter.instance.fromJson((e as num?)?.toInt())) .toSet(), ); diff --git a/json_serializable/test/kitchen_sink/simple_object.g.dart b/json_serializable/test/kitchen_sink/simple_object.g.dart index b0bbfbef0..53442bc1b 100644 --- a/json_serializable/test/kitchen_sink/simple_object.g.dart +++ b/json_serializable/test/kitchen_sink/simple_object.g.dart @@ -9,7 +9,7 @@ part of 'simple_object.dart'; // ************************************************************************** SimpleObject _$SimpleObjectFromJson(Map json) => SimpleObject( - json['value'] as int, + (json['value'] as num).toInt(), ); Map _$SimpleObjectToJson(SimpleObject instance) => diff --git a/json_serializable/test/kitchen_sink/strict_keys_object.g.dart b/json_serializable/test/kitchen_sink/strict_keys_object.g.dart index b6b00cce3..820d237ee 100644 --- a/json_serializable/test/kitchen_sink/strict_keys_object.g.dart +++ b/json_serializable/test/kitchen_sink/strict_keys_object.g.dart @@ -15,7 +15,7 @@ StrictKeysObject _$StrictKeysObjectFromJson(Map json) { requiredKeys: const ['value', 'custom_field'], ); return StrictKeysObject( - json['value'] as int, + (json['value'] as num).toInt(), json['custom_field'] as String, ); } diff --git a/json_serializable/test/src/_json_serializable_test_input.dart b/json_serializable/test/src/_json_serializable_test_input.dart index af82c8440..1458630ff 100644 --- a/json_serializable/test/src/_json_serializable_test_input.dart +++ b/json_serializable/test/src/_json_serializable_test_input.dart @@ -55,12 +55,13 @@ GeneralTestClass1 _$GeneralTestClass1FromJson(Map json) => GeneralTestClass1() ..firstName = json['firstName'] as String ..lastName = json['lastName'] as String - ..height = json['h'] as int + ..height = (json['h'] as num).toInt() ..dateOfBirth = DateTime.parse(json['dateOfBirth'] as String) ..dynamicType = json['dynamicType'] ..varType = json['varType'] - ..listOfInts = - (json['listOfInts'] as List).map((e) => e as int).toList(); + ..listOfInts = (json['listOfInts'] as List) + .map((e) => (e as num).toInt()) + .toList(); Map _$GeneralTestClass1ToJson(GeneralTestClass1 instance) => { @@ -89,7 +90,7 @@ class GeneralTestClass1 { @ShouldGenerate(r''' GeneralTestClass2 _$GeneralTestClass2FromJson(Map json) => GeneralTestClass2( - json['height'] as int, + (json['height'] as num).toInt(), json['firstName'] as String, json['lastName'] as String?, )..dateOfBirth = DateTime.parse(json['dateOfBirth'] as String); @@ -117,7 +118,7 @@ class GeneralTestClass2 { @ShouldGenerate(r''' FinalFields _$FinalFieldsFromJson(Map json) => FinalFields( - json['a'] as int, + (json['a'] as num).toInt(), ); Map _$FinalFieldsToJson(FinalFields instance) => @@ -154,7 +155,7 @@ class FinalFieldsNotSetInCtor { @ShouldGenerate(r''' SetSupport _$SetSupportFromJson(Map json) => SetSupport( - (json['values'] as List).map((e) => e as int).toSet(), + (json['values'] as List).map((e) => (e as num).toInt()).toSet(), ); Map _$SetSupportToJson(SetSupport instance) => @@ -455,8 +456,8 @@ mixin _PropInMixinI448RegressionMixin { PropInMixinI448Regression _$PropInMixinI448RegressionFromJson( Map json) => PropInMixinI448Regression() - ..nullable = json['nullable'] as int - ..notNullable = json['notNullable'] as int; + ..nullable = (json['nullable'] as num).toInt() + ..notNullable = (json['notNullable'] as num).toInt(); Map _$PropInMixinI448RegressionToJson( PropInMixinI448Regression instance) => @@ -474,7 +475,7 @@ class PropInMixinI448Regression with _PropInMixinI448RegressionMixin { @ShouldGenerate( r''' IgnoreUnannotated _$IgnoreUnannotatedFromJson(Map json) => - IgnoreUnannotated()..annotated = json['annotated'] as int; + IgnoreUnannotated()..annotated = (json['annotated'] as num).toInt(); Map _$IgnoreUnannotatedToJson(IgnoreUnannotated instance) => { @@ -493,7 +494,7 @@ class IgnoreUnannotated { @ShouldGenerate( r''' SubclassedJsonKey _$SubclassedJsonKeyFromJson(Map json) => - SubclassedJsonKey()..annotatedWithSubclass = json['bob'] as int; + SubclassedJsonKey()..annotatedWithSubclass = (json['bob'] as num).toInt(); Map _$SubclassedJsonKeyToJson(SubclassedJsonKey instance) => { diff --git a/json_serializable/test/src/default_value_input.dart b/json_serializable/test/src/default_value_input.dart index a594aa761..7bdd1a35b 100644 --- a/json_serializable/test/src/default_value_input.dart +++ b/json_serializable/test/src/default_value_input.dart @@ -137,7 +137,7 @@ DefaultWithDisallowNullRequiredClass disallowNullValues: const ['theField'], ); return DefaultWithDisallowNullRequiredClass() - ..theField = json['theField'] as int? ?? 7; + ..theField = (json['theField'] as num?)?.toInt() ?? 7; } ''', expectedLogItems: [ @@ -159,7 +159,7 @@ CtorDefaultValueAndJsonKeyDefaultValue _$CtorDefaultValueAndJsonKeyDefaultValueFromJson( Map json) => CtorDefaultValueAndJsonKeyDefaultValue( - json['theField'] as int? ?? 7, + (json['theField'] as num?)?.toInt() ?? 7, ); ''', expectedLogItems: [ @@ -181,7 +181,7 @@ class CtorDefaultValueAndJsonKeyDefaultValue { SameCtorAndJsonKeyDefaultValue _$SameCtorAndJsonKeyDefaultValueFromJson( Map json) => SameCtorAndJsonKeyDefaultValue( - json['theField'] as int? ?? 3, + (json['theField'] as num?)?.toInt() ?? 3, ); ''', expectedLogItems: [ diff --git a/json_serializable/test/src/inheritance_test_input.dart b/json_serializable/test/src/inheritance_test_input.dart index afd6a6552..875743fc7 100644 --- a/json_serializable/test/src/inheritance_test_input.dart +++ b/json_serializable/test/src/inheritance_test_input.dart @@ -2,11 +2,11 @@ part of '_json_serializable_test_input.dart'; @ShouldGenerate(r''' SubType _$SubTypeFromJson(Map json) => SubType( - json['subTypeViaCtor'] as int, - json['super-final-field'] as int, + (json['subTypeViaCtor'] as num).toInt(), + (json['super-final-field'] as num).toInt(), ) - ..superReadWriteField = json['superReadWriteField'] as int? - ..subTypeReadWrite = json['subTypeReadWrite'] as int; + ..superReadWriteField = (json['superReadWriteField'] as num?)?.toInt() + ..subTypeReadWrite = (json['subTypeReadWrite'] as num).toInt(); Map _$SubTypeToJson(SubType instance) { final val = { diff --git a/json_serializable/test/src/json_converter_test_input.dart b/json_serializable/test/src/json_converter_test_input.dart index 865ee06a6..5f4075b82 100644 --- a/json_serializable/test/src/json_converter_test_input.dart +++ b/json_serializable/test/src/json_converter_test_input.dart @@ -11,11 +11,11 @@ JsonConverterNamedCtor _$JsonConverterNamedCtorFromJson( Map json) => JsonConverterNamedCtor() ..value = const _DurationMillisecondConverter.named() - .fromJson(json['value'] as int) - ..genericValue = - _GenericConverter.named().fromJson(json['genericValue'] as int) - ..keyAnnotationFirst = - JsonConverterNamedCtor._fromJson(json['keyAnnotationFirst'] as int); + .fromJson((json['value'] as num).toInt()) + ..genericValue = _GenericConverter.named() + .fromJson((json['genericValue'] as num).toInt()) + ..keyAnnotationFirst = JsonConverterNamedCtor._fromJson( + (json['keyAnnotationFirst'] as num).toInt()); Map _$JsonConverterNamedCtorToJson( JsonConverterNamedCtor instance) => @@ -49,13 +49,13 @@ JsonConvertOnField _$JsonConvertOnFieldFromJson( Map json) => JsonConvertOnField() ..annotatedField = const _DurationMillisecondConverter() - .fromJson(json['annotatedField'] as int) + .fromJson((json['annotatedField'] as num).toInt()) ..annotatedWithNamedCtor = const _DurationMillisecondConverter.named() - .fromJson(json['annotatedWithNamedCtor'] as int) - ..classAnnotatedWithField = - _durationConverter.fromJson(json['classAnnotatedWithField'] as int) - ..genericValue = - _GenericConverter().fromJson(json['genericValue'] as int); + .fromJson((json['annotatedWithNamedCtor'] as num).toInt()) + ..classAnnotatedWithField = _durationConverter + .fromJson((json['classAnnotatedWithField'] as num).toInt()) + ..genericValue = _GenericConverter() + .fromJson((json['genericValue'] as num).toInt()); Map _$JsonConvertOnFieldToJson( JsonConvertOnField instance) => diff --git a/json_serializable/test/src/map_key_variety_test_input.dart b/json_serializable/test/src/map_key_variety_test_input.dart index 861512f18..2f07cf703 100644 --- a/json_serializable/test/src/map_key_variety_test_input.dart +++ b/json_serializable/test/src/map_key_variety_test_input.dart @@ -4,7 +4,7 @@ part of '_json_serializable_test_input.dart'; MapKeyVariety _$MapKeyVarietyFromJson(Map json) => MapKeyVariety() ..intIntMap = (json['intIntMap'] as Map).map( - (k, e) => MapEntry(int.parse(k), e as int), + (k, e) => MapEntry(int.parse(k), (e as num).toInt()), ); Map _$MapKeyVarietyToJson(MapKeyVariety instance) => diff --git a/json_serializable/test/src/to_from_json_test_input.dart b/json_serializable/test/src/to_from_json_test_input.dart index 11ce9f447..7e5f57965 100644 --- a/json_serializable/test/src/to_from_json_test_input.dart +++ b/json_serializable/test/src/to_from_json_test_input.dart @@ -122,7 +122,7 @@ class Reproduce869NullableGenericTypeWithDefault { } List? _fromList(List? pairs) => - pairs?.map((it) => it as int).toList(growable: false); + pairs?.map((it) => (it as num).toInt()).toList(growable: false); List? _toList(List? pairs) => pairs?.map((it) => [it]).toList(growable: false); diff --git a/json_serializable/test/supported_types/enum_type.dart b/json_serializable/test/supported_types/enum_type.dart index 6e31deaf2..91750dd6b 100644 --- a/json_serializable/test/supported_types/enum_type.dart +++ b/json_serializable/test/supported_types/enum_type.dart @@ -10,7 +10,7 @@ class FromJsonDynamicParam { final int value; factory FromJsonDynamicParam.fromJson(dynamic json) => - FromJsonDynamicParam(value: json as int); + FromJsonDynamicParam(value: (json as num).toInt()); dynamic toJson() => null; } @@ -21,7 +21,7 @@ class FromJsonNullableObjectParam { final int value; factory FromJsonNullableObjectParam.fromJson(Object? json) => - FromJsonNullableObjectParam(value: json as int); + FromJsonNullableObjectParam(value: (json as num).toInt()); Object? toJson() => null; } @@ -32,7 +32,7 @@ class FromJsonObjectParam { final int value; factory FromJsonObjectParam.fromJson(Object json) => - FromJsonObjectParam(value: json as int); + FromJsonObjectParam(value: (json as num).toInt()); dynamic toJson() => null; } diff --git a/json_serializable/test/supported_types/input.type_duration.g.dart b/json_serializable/test/supported_types/input.type_duration.g.dart index 7ea4c7da6..9d57d843c 100644 --- a/json_serializable/test/supported_types/input.type_duration.g.dart +++ b/json_serializable/test/supported_types/input.type_duration.g.dart @@ -9,7 +9,7 @@ part of 'input.type_duration.dart'; // ************************************************************************** SimpleClass _$SimpleClassFromJson(Map json) => SimpleClass( - Duration(microseconds: json['value'] as int), + Duration(microseconds: (json['value'] as num).toInt()), ); Map _$SimpleClassToJson(SimpleClass instance) => @@ -21,7 +21,7 @@ SimpleClassNullable _$SimpleClassNullableFromJson(Map json) => SimpleClassNullable( json['value'] == null ? null - : Duration(microseconds: json['value'] as int), + : Duration(microseconds: (json['value'] as num).toInt()), ); Map _$SimpleClassNullableToJson( diff --git a/json_serializable/test/supported_types/input.type_int.g.dart b/json_serializable/test/supported_types/input.type_int.g.dart index ddb06ed57..2f248b74a 100644 --- a/json_serializable/test/supported_types/input.type_int.g.dart +++ b/json_serializable/test/supported_types/input.type_int.g.dart @@ -9,8 +9,8 @@ part of 'input.type_int.dart'; // ************************************************************************** SimpleClass _$SimpleClassFromJson(Map json) => SimpleClass( - json['value'] as int, - json['withDefault'] as int? ?? 42, + (json['value'] as num).toInt(), + (json['withDefault'] as num?)?.toInt() ?? 42, ); Map _$SimpleClassToJson(SimpleClass instance) => @@ -21,8 +21,8 @@ Map _$SimpleClassToJson(SimpleClass instance) => SimpleClassNullable _$SimpleClassNullableFromJson(Map json) => SimpleClassNullable( - json['value'] as int?, - json['withDefault'] as int? ?? 42, + (json['value'] as num?)?.toInt(), + (json['withDefault'] as num?)?.toInt() ?? 42, ); Map _$SimpleClassNullableToJson( 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 95c3e0557..a9264f393 100644 --- a/json_serializable/test/supported_types/input.type_iterable.g.dart +++ b/json_serializable/test/supported_types/input.type_iterable.g.dart @@ -231,7 +231,7 @@ SimpleClassOfDuration _$SimpleClassOfDurationFromJson( Map json) => SimpleClassOfDuration( (json['value'] as List) - .map((e) => Duration(microseconds: e as int)), + .map((e) => Duration(microseconds: (e as num).toInt())), ); Map _$SimpleClassOfDurationToJson( @@ -244,7 +244,7 @@ SimpleClassNullableOfDuration _$SimpleClassNullableOfDurationFromJson( Map json) => SimpleClassNullableOfDuration( (json['value'] as List?) - ?.map((e) => Duration(microseconds: e as int)), + ?.map((e) => Duration(microseconds: (e as num).toInt())), ); Map _$SimpleClassNullableOfDurationToJson( @@ -256,8 +256,8 @@ Map _$SimpleClassNullableOfDurationToJson( SimpleClassOfDurationNullable _$SimpleClassOfDurationNullableFromJson( Map json) => SimpleClassOfDurationNullable( - (json['value'] as List) - .map((e) => e == null ? null : Duration(microseconds: e as int)), + (json['value'] as List).map( + (e) => e == null ? null : Duration(microseconds: (e as num).toInt())), ); Map _$SimpleClassOfDurationNullableToJson( @@ -270,8 +270,8 @@ SimpleClassNullableOfDurationNullable _$SimpleClassNullableOfDurationNullableFromJson( Map json) => SimpleClassNullableOfDurationNullable( - (json['value'] as List?) - ?.map((e) => e == null ? null : Duration(microseconds: e as int)), + (json['value'] as List?)?.map((e) => + e == null ? null : Duration(microseconds: (e as num).toInt())), ); Map _$SimpleClassNullableOfDurationNullableToJson( @@ -446,7 +446,7 @@ Map _$SimpleClassNullableOfFromJsonObjectParamToJson( SimpleClassOfInt _$SimpleClassOfIntFromJson(Map json) => SimpleClassOfInt( - (json['value'] as List).map((e) => e as int), + (json['value'] as List).map((e) => (e as num).toInt()), ); Map _$SimpleClassOfIntToJson(SimpleClassOfInt instance) => @@ -457,7 +457,7 @@ Map _$SimpleClassOfIntToJson(SimpleClassOfInt instance) => SimpleClassNullableOfInt _$SimpleClassNullableOfIntFromJson( Map json) => SimpleClassNullableOfInt( - (json['value'] as List?)?.map((e) => e as int), + (json['value'] as List?)?.map((e) => (e as num).toInt()), ); Map _$SimpleClassNullableOfIntToJson( @@ -469,7 +469,7 @@ Map _$SimpleClassNullableOfIntToJson( SimpleClassOfIntNullable _$SimpleClassOfIntNullableFromJson( Map json) => SimpleClassOfIntNullable( - (json['value'] as List).map((e) => e as int?), + (json['value'] as List).map((e) => (e as num?)?.toInt()), ); Map _$SimpleClassOfIntNullableToJson( @@ -481,7 +481,7 @@ Map _$SimpleClassOfIntNullableToJson( SimpleClassNullableOfIntNullable _$SimpleClassNullableOfIntNullableFromJson( Map json) => SimpleClassNullableOfIntNullable( - (json['value'] as List?)?.map((e) => e as int?), + (json['value'] as List?)?.map((e) => (e as num?)?.toInt()), ); Map _$SimpleClassNullableOfIntNullableToJson( @@ -588,7 +588,7 @@ SimpleClassOfRecord _$SimpleClassOfRecordFromJson(Map json) => (json['value'] as List).map((e) => _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -619,7 +619,7 @@ SimpleClassNullableOfRecord _$SimpleClassNullableOfRecordFromJson( (json['value'] as List?)?.map((e) => _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), 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 2f4497581..f86a6d00c 100644 --- a/json_serializable/test/supported_types/input.type_list.g.dart +++ b/json_serializable/test/supported_types/input.type_list.g.dart @@ -249,7 +249,7 @@ SimpleClassOfDuration _$SimpleClassOfDurationFromJson( Map json) => SimpleClassOfDuration( (json['value'] as List) - .map((e) => Duration(microseconds: e as int)) + .map((e) => Duration(microseconds: (e as num).toInt())) .toList(), ); @@ -263,7 +263,7 @@ SimpleClassNullableOfDuration _$SimpleClassNullableOfDurationFromJson( Map json) => SimpleClassNullableOfDuration( (json['value'] as List?) - ?.map((e) => Duration(microseconds: e as int)) + ?.map((e) => Duration(microseconds: (e as num).toInt())) .toList(), ); @@ -277,7 +277,8 @@ SimpleClassOfDurationNullable _$SimpleClassOfDurationNullableFromJson( Map json) => SimpleClassOfDurationNullable( (json['value'] as List) - .map((e) => e == null ? null : Duration(microseconds: e as int)) + .map((e) => + e == null ? null : Duration(microseconds: (e as num).toInt())) .toList(), ); @@ -292,7 +293,8 @@ SimpleClassNullableOfDurationNullable Map json) => SimpleClassNullableOfDurationNullable( (json['value'] as List?) - ?.map((e) => e == null ? null : Duration(microseconds: e as int)) + ?.map((e) => + e == null ? null : Duration(microseconds: (e as num).toInt())) .toList(), ); @@ -480,7 +482,7 @@ Map _$SimpleClassNullableOfFromJsonObjectParamToJson( SimpleClassOfInt _$SimpleClassOfIntFromJson(Map json) => SimpleClassOfInt( - (json['value'] as List).map((e) => e as int).toList(), + (json['value'] as List).map((e) => (e as num).toInt()).toList(), ); Map _$SimpleClassOfIntToJson(SimpleClassOfInt instance) => @@ -491,7 +493,9 @@ Map _$SimpleClassOfIntToJson(SimpleClassOfInt instance) => SimpleClassNullableOfInt _$SimpleClassNullableOfIntFromJson( Map json) => SimpleClassNullableOfInt( - (json['value'] as List?)?.map((e) => e as int).toList(), + (json['value'] as List?) + ?.map((e) => (e as num).toInt()) + .toList(), ); Map _$SimpleClassNullableOfIntToJson( @@ -503,7 +507,9 @@ Map _$SimpleClassNullableOfIntToJson( SimpleClassOfIntNullable _$SimpleClassOfIntNullableFromJson( Map json) => SimpleClassOfIntNullable( - (json['value'] as List).map((e) => e as int?).toList(), + (json['value'] as List) + .map((e) => (e as num?)?.toInt()) + .toList(), ); Map _$SimpleClassOfIntNullableToJson( @@ -515,7 +521,9 @@ Map _$SimpleClassOfIntNullableToJson( SimpleClassNullableOfIntNullable _$SimpleClassNullableOfIntNullableFromJson( Map json) => SimpleClassNullableOfIntNullable( - (json['value'] as List?)?.map((e) => e as int?).toList(), + (json['value'] as List?) + ?.map((e) => (e as num?)?.toInt()) + .toList(), ); Map _$SimpleClassNullableOfIntNullableToJson( @@ -623,7 +631,7 @@ SimpleClassOfRecord _$SimpleClassOfRecordFromJson(Map json) => .map((e) => _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -656,7 +664,7 @@ SimpleClassNullableOfRecord _$SimpleClassNullableOfRecordFromJson( ?.map((e) => _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), 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 5f29d60ab..5c8a3ea82 100644 --- a/json_serializable/test/supported_types/input.type_map.g.dart +++ b/json_serializable/test/supported_types/input.type_map.g.dart @@ -1953,7 +1953,8 @@ SimpleClassOfBigIntToDuration _$SimpleClassOfBigIntToDurationFromJson( Map json) => SimpleClassOfBigIntToDuration( (json['value'] as Map).map( - (k, e) => MapEntry(BigInt.parse(k), Duration(microseconds: e as int)), + (k, e) => MapEntry( + BigInt.parse(k), Duration(microseconds: (e as num).toInt())), ), ); @@ -1969,8 +1970,8 @@ SimpleClassNullableOfBigIntToDuration Map json) => SimpleClassNullableOfBigIntToDuration( (json['value'] as Map?)?.map( - (k, e) => - MapEntry(BigInt.parse(k), Duration(microseconds: e as int)), + (k, e) => MapEntry( + BigInt.parse(k), Duration(microseconds: (e as num).toInt())), ), ); @@ -1985,7 +1986,8 @@ SimpleClassOfDateTimeToDuration _$SimpleClassOfDateTimeToDurationFromJson( Map json) => SimpleClassOfDateTimeToDuration( (json['value'] as Map).map( - (k, e) => MapEntry(DateTime.parse(k), Duration(microseconds: e as int)), + (k, e) => MapEntry( + DateTime.parse(k), Duration(microseconds: (e as num).toInt())), ), ); @@ -2001,8 +2003,8 @@ SimpleClassNullableOfDateTimeToDuration Map json) => SimpleClassNullableOfDateTimeToDuration( (json['value'] as Map?)?.map( - (k, e) => - MapEntry(DateTime.parse(k), Duration(microseconds: e as int)), + (k, e) => MapEntry( + DateTime.parse(k), Duration(microseconds: (e as num).toInt())), ), ); @@ -2017,7 +2019,7 @@ SimpleClassOfDynamicToDuration _$SimpleClassOfDynamicToDurationFromJson( Map json) => SimpleClassOfDynamicToDuration( (json['value'] as Map).map( - (k, e) => MapEntry(k, Duration(microseconds: e as int)), + (k, e) => MapEntry(k, Duration(microseconds: (e as num).toInt())), ), ); @@ -2032,7 +2034,7 @@ SimpleClassNullableOfDynamicToDuration Map json) => SimpleClassNullableOfDynamicToDuration( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, Duration(microseconds: e as int)), + (k, e) => MapEntry(k, Duration(microseconds: (e as num).toInt())), ), ); @@ -2047,7 +2049,7 @@ SimpleClassOfEnumTypeToDuration _$SimpleClassOfEnumTypeToDurationFromJson( SimpleClassOfEnumTypeToDuration( (json['value'] as Map).map( (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), - Duration(microseconds: e as int)), + Duration(microseconds: (e as num).toInt())), ), ); @@ -2064,7 +2066,7 @@ SimpleClassNullableOfEnumTypeToDuration SimpleClassNullableOfEnumTypeToDuration( (json['value'] as Map?)?.map( (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), - Duration(microseconds: e as int)), + Duration(microseconds: (e as num).toInt())), ), ); @@ -2079,7 +2081,8 @@ SimpleClassOfIntToDuration _$SimpleClassOfIntToDurationFromJson( Map json) => SimpleClassOfIntToDuration( (json['value'] as Map).map( - (k, e) => MapEntry(int.parse(k), Duration(microseconds: e as int)), + (k, e) => + MapEntry(int.parse(k), Duration(microseconds: (e as num).toInt())), ), ); @@ -2094,7 +2097,8 @@ SimpleClassNullableOfIntToDuration _$SimpleClassNullableOfIntToDurationFromJson( Map json) => SimpleClassNullableOfIntToDuration( (json['value'] as Map?)?.map( - (k, e) => MapEntry(int.parse(k), Duration(microseconds: e as int)), + (k, e) => + MapEntry(int.parse(k), Duration(microseconds: (e as num).toInt())), ), ); @@ -2109,7 +2113,7 @@ SimpleClassOfObjectToDuration _$SimpleClassOfObjectToDurationFromJson( Map json) => SimpleClassOfObjectToDuration( (json['value'] as Map).map( - (k, e) => MapEntry(k, Duration(microseconds: e as int)), + (k, e) => MapEntry(k, Duration(microseconds: (e as num).toInt())), ), ); @@ -2124,7 +2128,7 @@ SimpleClassNullableOfObjectToDuration Map json) => SimpleClassNullableOfObjectToDuration( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, Duration(microseconds: e as int)), + (k, e) => MapEntry(k, Duration(microseconds: (e as num).toInt())), ), ); @@ -2138,7 +2142,7 @@ SimpleClassOfStringToDuration _$SimpleClassOfStringToDurationFromJson( Map json) => SimpleClassOfStringToDuration( (json['value'] as Map).map( - (k, e) => MapEntry(k, Duration(microseconds: e as int)), + (k, e) => MapEntry(k, Duration(microseconds: (e as num).toInt())), ), ); @@ -2153,7 +2157,7 @@ SimpleClassNullableOfStringToDuration Map json) => SimpleClassNullableOfStringToDuration( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, Duration(microseconds: e as int)), + (k, e) => MapEntry(k, Duration(microseconds: (e as num).toInt())), ), ); @@ -2167,7 +2171,8 @@ SimpleClassOfUriToDuration _$SimpleClassOfUriToDurationFromJson( Map json) => SimpleClassOfUriToDuration( (json['value'] as Map).map( - (k, e) => MapEntry(Uri.parse(k), Duration(microseconds: e as int)), + (k, e) => + MapEntry(Uri.parse(k), Duration(microseconds: (e as num).toInt())), ), ); @@ -2182,7 +2187,8 @@ SimpleClassNullableOfUriToDuration _$SimpleClassNullableOfUriToDurationFromJson( Map json) => SimpleClassNullableOfUriToDuration( (json['value'] as Map?)?.map( - (k, e) => MapEntry(Uri.parse(k), Duration(microseconds: e as int)), + (k, e) => + MapEntry(Uri.parse(k), Duration(microseconds: (e as num).toInt())), ), ); @@ -2199,7 +2205,7 @@ SimpleClassOfBigIntToDurationNullable SimpleClassOfBigIntToDurationNullable( (json['value'] as Map).map( (k, e) => MapEntry(BigInt.parse(k), - e == null ? null : Duration(microseconds: e as int)), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2216,7 +2222,7 @@ SimpleClassNullableOfBigIntToDurationNullable SimpleClassNullableOfBigIntToDurationNullable( (json['value'] as Map?)?.map( (k, e) => MapEntry(BigInt.parse(k), - e == null ? null : Duration(microseconds: e as int)), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2233,7 +2239,7 @@ SimpleClassOfDateTimeToDurationNullable SimpleClassOfDateTimeToDurationNullable( (json['value'] as Map).map( (k, e) => MapEntry(DateTime.parse(k), - e == null ? null : Duration(microseconds: e as int)), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2250,7 +2256,7 @@ SimpleClassNullableOfDateTimeToDurationNullable SimpleClassNullableOfDateTimeToDurationNullable( (json['value'] as Map?)?.map( (k, e) => MapEntry(DateTime.parse(k), - e == null ? null : Duration(microseconds: e as int)), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2266,8 +2272,8 @@ SimpleClassOfDynamicToDurationNullable Map json) => SimpleClassOfDynamicToDurationNullable( (json['value'] as Map).map( - (k, e) => MapEntry( - k, e == null ? null : Duration(microseconds: e as int)), + (k, e) => MapEntry(k, + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2282,8 +2288,8 @@ SimpleClassNullableOfDynamicToDurationNullable Map json) => SimpleClassNullableOfDynamicToDurationNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry( - k, e == null ? null : Duration(microseconds: e as int)), + (k, e) => MapEntry(k, + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2299,7 +2305,7 @@ SimpleClassOfEnumTypeToDurationNullable SimpleClassOfEnumTypeToDurationNullable( (json['value'] as Map).map( (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), - e == null ? null : Duration(microseconds: e as int)), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2316,7 +2322,7 @@ SimpleClassNullableOfEnumTypeToDurationNullable SimpleClassNullableOfEnumTypeToDurationNullable( (json['value'] as Map?)?.map( (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), - e == null ? null : Duration(microseconds: e as int)), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2331,8 +2337,8 @@ SimpleClassOfIntToDurationNullable _$SimpleClassOfIntToDurationNullableFromJson( Map json) => SimpleClassOfIntToDurationNullable( (json['value'] as Map).map( - (k, e) => MapEntry( - int.parse(k), e == null ? null : Duration(microseconds: e as int)), + (k, e) => MapEntry(int.parse(k), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2349,7 +2355,7 @@ SimpleClassNullableOfIntToDurationNullable SimpleClassNullableOfIntToDurationNullable( (json['value'] as Map?)?.map( (k, e) => MapEntry(int.parse(k), - e == null ? null : Duration(microseconds: e as int)), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2365,8 +2371,8 @@ SimpleClassOfObjectToDurationNullable Map json) => SimpleClassOfObjectToDurationNullable( (json['value'] as Map).map( - (k, e) => MapEntry( - k, e == null ? null : Duration(microseconds: e as int)), + (k, e) => MapEntry(k, + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2381,8 +2387,8 @@ SimpleClassNullableOfObjectToDurationNullable Map json) => SimpleClassNullableOfObjectToDurationNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry( - k, e == null ? null : Duration(microseconds: e as int)), + (k, e) => MapEntry(k, + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2397,8 +2403,8 @@ SimpleClassOfStringToDurationNullable Map json) => SimpleClassOfStringToDurationNullable( (json['value'] as Map).map( - (k, e) => MapEntry( - k, e == null ? null : Duration(microseconds: e as int)), + (k, e) => MapEntry(k, + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2413,8 +2419,8 @@ SimpleClassNullableOfStringToDurationNullable Map json) => SimpleClassNullableOfStringToDurationNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry( - k, e == null ? null : Duration(microseconds: e as int)), + (k, e) => MapEntry(k, + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2428,8 +2434,8 @@ SimpleClassOfUriToDurationNullable _$SimpleClassOfUriToDurationNullableFromJson( Map json) => SimpleClassOfUriToDurationNullable( (json['value'] as Map).map( - (k, e) => MapEntry( - Uri.parse(k), e == null ? null : Duration(microseconds: e as int)), + (k, e) => MapEntry(Uri.parse(k), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -2446,7 +2452,7 @@ SimpleClassNullableOfUriToDurationNullable SimpleClassNullableOfUriToDurationNullable( (json['value'] as Map?)?.map( (k, e) => MapEntry(Uri.parse(k), - e == null ? null : Duration(microseconds: e as int)), + e == null ? null : Duration(microseconds: (e as num).toInt())), ), ); @@ -3943,7 +3949,7 @@ SimpleClassOfBigIntToInt _$SimpleClassOfBigIntToIntFromJson( Map json) => SimpleClassOfBigIntToInt( (json['value'] as Map).map( - (k, e) => MapEntry(BigInt.parse(k), e as int), + (k, e) => MapEntry(BigInt.parse(k), (e as num).toInt()), ), ); @@ -3957,7 +3963,7 @@ SimpleClassNullableOfBigIntToInt _$SimpleClassNullableOfBigIntToIntFromJson( Map json) => SimpleClassNullableOfBigIntToInt( (json['value'] as Map?)?.map( - (k, e) => MapEntry(BigInt.parse(k), e as int), + (k, e) => MapEntry(BigInt.parse(k), (e as num).toInt()), ), ); @@ -3971,7 +3977,7 @@ SimpleClassOfDateTimeToInt _$SimpleClassOfDateTimeToIntFromJson( Map json) => SimpleClassOfDateTimeToInt( (json['value'] as Map).map( - (k, e) => MapEntry(DateTime.parse(k), e as int), + (k, e) => MapEntry(DateTime.parse(k), (e as num).toInt()), ), ); @@ -3985,7 +3991,7 @@ SimpleClassNullableOfDateTimeToInt _$SimpleClassNullableOfDateTimeToIntFromJson( Map json) => SimpleClassNullableOfDateTimeToInt( (json['value'] as Map?)?.map( - (k, e) => MapEntry(DateTime.parse(k), e as int), + (k, e) => MapEntry(DateTime.parse(k), (e as num).toInt()), ), ); @@ -4011,7 +4017,7 @@ SimpleClassNullableOfDynamicToInt _$SimpleClassNullableOfDynamicToIntFromJson( Map json) => SimpleClassNullableOfDynamicToInt( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, e as int), + (k, e) => MapEntry(k, (e as num).toInt()), ), ); @@ -4025,7 +4031,8 @@ SimpleClassOfEnumTypeToInt _$SimpleClassOfEnumTypeToIntFromJson( Map json) => SimpleClassOfEnumTypeToInt( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as int), + (k, e) => + MapEntry($enumDecode(_$EnumTypeEnumMap, k), (e as num).toInt()), ), ); @@ -4039,7 +4046,8 @@ SimpleClassNullableOfEnumTypeToInt _$SimpleClassNullableOfEnumTypeToIntFromJson( Map json) => SimpleClassNullableOfEnumTypeToInt( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as int), + (k, e) => + MapEntry($enumDecode(_$EnumTypeEnumMap, k), (e as num).toInt()), ), ); @@ -4054,7 +4062,7 @@ SimpleClassOfIntToInt _$SimpleClassOfIntToIntFromJson( Map json) => SimpleClassOfIntToInt( (json['value'] as Map).map( - (k, e) => MapEntry(int.parse(k), e as int), + (k, e) => MapEntry(int.parse(k), (e as num).toInt()), ), ); @@ -4068,7 +4076,7 @@ SimpleClassNullableOfIntToInt _$SimpleClassNullableOfIntToIntFromJson( Map json) => SimpleClassNullableOfIntToInt( (json['value'] as Map?)?.map( - (k, e) => MapEntry(int.parse(k), e as int), + (k, e) => MapEntry(int.parse(k), (e as num).toInt()), ), ); @@ -4094,7 +4102,7 @@ SimpleClassNullableOfObjectToInt _$SimpleClassNullableOfObjectToIntFromJson( Map json) => SimpleClassNullableOfObjectToInt( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, e as int), + (k, e) => MapEntry(k, (e as num).toInt()), ), ); @@ -4120,7 +4128,7 @@ SimpleClassNullableOfStringToInt _$SimpleClassNullableOfStringToIntFromJson( Map json) => SimpleClassNullableOfStringToInt( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, e as int), + (k, e) => MapEntry(k, (e as num).toInt()), ), ); @@ -4134,7 +4142,7 @@ SimpleClassOfUriToInt _$SimpleClassOfUriToIntFromJson( Map json) => SimpleClassOfUriToInt( (json['value'] as Map).map( - (k, e) => MapEntry(Uri.parse(k), e as int), + (k, e) => MapEntry(Uri.parse(k), (e as num).toInt()), ), ); @@ -4148,7 +4156,7 @@ SimpleClassNullableOfUriToInt _$SimpleClassNullableOfUriToIntFromJson( Map json) => SimpleClassNullableOfUriToInt( (json['value'] as Map?)?.map( - (k, e) => MapEntry(Uri.parse(k), e as int), + (k, e) => MapEntry(Uri.parse(k), (e as num).toInt()), ), ); @@ -4162,7 +4170,7 @@ SimpleClassOfBigIntToIntNullable _$SimpleClassOfBigIntToIntNullableFromJson( Map json) => SimpleClassOfBigIntToIntNullable( (json['value'] as Map).map( - (k, e) => MapEntry(BigInt.parse(k), e as int?), + (k, e) => MapEntry(BigInt.parse(k), (e as num?)?.toInt()), ), ); @@ -4177,7 +4185,7 @@ SimpleClassNullableOfBigIntToIntNullable Map json) => SimpleClassNullableOfBigIntToIntNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(BigInt.parse(k), e as int?), + (k, e) => MapEntry(BigInt.parse(k), (e as num?)?.toInt()), ), ); @@ -4191,7 +4199,7 @@ SimpleClassOfDateTimeToIntNullable _$SimpleClassOfDateTimeToIntNullableFromJson( Map json) => SimpleClassOfDateTimeToIntNullable( (json['value'] as Map).map( - (k, e) => MapEntry(DateTime.parse(k), e as int?), + (k, e) => MapEntry(DateTime.parse(k), (e as num?)?.toInt()), ), ); @@ -4206,7 +4214,7 @@ SimpleClassNullableOfDateTimeToIntNullable Map json) => SimpleClassNullableOfDateTimeToIntNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(DateTime.parse(k), e as int?), + (k, e) => MapEntry(DateTime.parse(k), (e as num?)?.toInt()), ), ); @@ -4233,7 +4241,7 @@ SimpleClassNullableOfDynamicToIntNullable Map json) => SimpleClassNullableOfDynamicToIntNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, e as int?), + (k, e) => MapEntry(k, (e as num?)?.toInt()), ), ); @@ -4247,7 +4255,8 @@ SimpleClassOfEnumTypeToIntNullable _$SimpleClassOfEnumTypeToIntNullableFromJson( Map json) => SimpleClassOfEnumTypeToIntNullable( (json['value'] as Map).map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as int?), + (k, e) => + MapEntry($enumDecode(_$EnumTypeEnumMap, k), (e as num?)?.toInt()), ), ); @@ -4262,7 +4271,8 @@ SimpleClassNullableOfEnumTypeToIntNullable Map json) => SimpleClassNullableOfEnumTypeToIntNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry($enumDecode(_$EnumTypeEnumMap, k), e as int?), + (k, e) => MapEntry( + $enumDecode(_$EnumTypeEnumMap, k), (e as num?)?.toInt()), ), ); @@ -4277,7 +4287,7 @@ SimpleClassOfIntToIntNullable _$SimpleClassOfIntToIntNullableFromJson( Map json) => SimpleClassOfIntToIntNullable( (json['value'] as Map).map( - (k, e) => MapEntry(int.parse(k), e as int?), + (k, e) => MapEntry(int.parse(k), (e as num?)?.toInt()), ), ); @@ -4292,7 +4302,7 @@ SimpleClassNullableOfIntToIntNullable Map json) => SimpleClassNullableOfIntToIntNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(int.parse(k), e as int?), + (k, e) => MapEntry(int.parse(k), (e as num?)?.toInt()), ), ); @@ -4319,7 +4329,7 @@ SimpleClassNullableOfObjectToIntNullable Map json) => SimpleClassNullableOfObjectToIntNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, e as int?), + (k, e) => MapEntry(k, (e as num?)?.toInt()), ), ); @@ -4346,7 +4356,7 @@ SimpleClassNullableOfStringToIntNullable Map json) => SimpleClassNullableOfStringToIntNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(k, e as int?), + (k, e) => MapEntry(k, (e as num?)?.toInt()), ), ); @@ -4360,7 +4370,7 @@ SimpleClassOfUriToIntNullable _$SimpleClassOfUriToIntNullableFromJson( Map json) => SimpleClassOfUriToIntNullable( (json['value'] as Map).map( - (k, e) => MapEntry(Uri.parse(k), e as int?), + (k, e) => MapEntry(Uri.parse(k), (e as num?)?.toInt()), ), ); @@ -4375,7 +4385,7 @@ SimpleClassNullableOfUriToIntNullable Map json) => SimpleClassNullableOfUriToIntNullable( (json['value'] as Map?)?.map( - (k, e) => MapEntry(Uri.parse(k), e as int?), + (k, e) => MapEntry(Uri.parse(k), (e as num?)?.toInt()), ), ); @@ -5290,7 +5300,7 @@ SimpleClassOfBigIntToRecord _$SimpleClassOfBigIntToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5324,7 +5334,7 @@ SimpleClassNullableOfBigIntToRecord _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5352,7 +5362,7 @@ SimpleClassOfDateTimeToRecord _$SimpleClassOfDateTimeToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5381,7 +5391,7 @@ SimpleClassNullableOfDateTimeToRecord _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5409,7 +5419,7 @@ SimpleClassOfDynamicToRecord _$SimpleClassOfDynamicToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5436,7 +5446,7 @@ SimpleClassNullableOfDynamicToRecord _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5463,7 +5473,7 @@ SimpleClassOfEnumTypeToRecord _$SimpleClassOfEnumTypeToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5492,7 +5502,7 @@ SimpleClassNullableOfEnumTypeToRecord _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5520,7 +5530,7 @@ SimpleClassOfIntToRecord _$SimpleClassOfIntToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5548,7 +5558,7 @@ SimpleClassNullableOfIntToRecord _$SimpleClassNullableOfIntToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5576,7 +5586,7 @@ SimpleClassOfObjectToRecord _$SimpleClassOfObjectToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5603,7 +5613,7 @@ SimpleClassNullableOfObjectToRecord _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5630,7 +5640,7 @@ SimpleClassOfStringToRecord _$SimpleClassOfStringToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5657,7 +5667,7 @@ SimpleClassNullableOfStringToRecord _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5684,7 +5694,7 @@ SimpleClassOfUriToRecord _$SimpleClassOfUriToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -5712,7 +5722,7 @@ SimpleClassNullableOfUriToRecord _$SimpleClassNullableOfUriToRecordFromJson( _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), 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 5c96d58c8..bc37b7cd2 100644 --- a/json_serializable/test/supported_types/input.type_record.g.dart +++ b/json_serializable/test/supported_types/input.type_record.g.dart @@ -411,8 +411,8 @@ SimpleClassOfDuration _$SimpleClassOfDurationFromJson( _$recordConvert( json['value'], ($jsonValue) => ( - Duration(microseconds: $jsonValue[r'$1'] as int), - named: Duration(microseconds: $jsonValue['named'] as int), + Duration(microseconds: ($jsonValue[r'$1'] as num).toInt()), + named: Duration(microseconds: ($jsonValue['named'] as num).toInt()), ), ), ); @@ -432,8 +432,8 @@ SimpleClassNullableOfDuration _$SimpleClassNullableOfDurationFromJson( _$recordConvertNullable( json['value'], ($jsonValue) => ( - Duration(microseconds: $jsonValue[r'$1'] as int), - named: Duration(microseconds: $jsonValue['named'] as int), + Duration(microseconds: ($jsonValue[r'$1'] as num).toInt()), + named: Duration(microseconds: ($jsonValue['named'] as num).toInt()), ), ), ); @@ -457,10 +457,10 @@ SimpleClassOfDurationNullable _$SimpleClassOfDurationNullableFromJson( ($jsonValue) => ( $jsonValue[r'$1'] == null ? null - : Duration(microseconds: $jsonValue[r'$1'] as int), + : Duration(microseconds: ($jsonValue[r'$1'] as num).toInt()), named: $jsonValue['named'] == null ? null - : Duration(microseconds: $jsonValue['named'] as int), + : Duration(microseconds: ($jsonValue['named'] as num).toInt()), ), ), ); @@ -483,10 +483,11 @@ SimpleClassNullableOfDurationNullable ($jsonValue) => ( $jsonValue[r'$1'] == null ? null - : Duration(microseconds: $jsonValue[r'$1'] as int), + : Duration(microseconds: ($jsonValue[r'$1'] as num).toInt()), named: $jsonValue['named'] == null ? null - : Duration(microseconds: $jsonValue['named'] as int), + : Duration( + microseconds: ($jsonValue['named'] as num).toInt()), ), ), ); @@ -785,8 +786,8 @@ SimpleClassOfInt _$SimpleClassOfIntFromJson(Map json) => _$recordConvert( json['value'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, - named: $jsonValue['named'] as int, + ($jsonValue[r'$1'] as num).toInt(), + named: ($jsonValue['named'] as num).toInt(), ), ), ); @@ -805,8 +806,8 @@ SimpleClassNullableOfInt _$SimpleClassNullableOfIntFromJson( _$recordConvertNullable( json['value'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, - named: $jsonValue['named'] as int, + ($jsonValue[r'$1'] as num).toInt(), + named: ($jsonValue['named'] as num).toInt(), ), ), ); @@ -828,8 +829,8 @@ SimpleClassOfIntNullable _$SimpleClassOfIntNullableFromJson( _$recordConvert( json['value'], ($jsonValue) => ( - $jsonValue[r'$1'] as int?, - named: $jsonValue['named'] as int?, + ($jsonValue[r'$1'] as num?)?.toInt(), + named: ($jsonValue['named'] as num?)?.toInt(), ), ), ); @@ -849,8 +850,8 @@ SimpleClassNullableOfIntNullable _$SimpleClassNullableOfIntNullableFromJson( _$recordConvertNullable( json['value'], ($jsonValue) => ( - $jsonValue[r'$1'] as int?, - named: $jsonValue['named'] as int?, + ($jsonValue[r'$1'] as num?)?.toInt(), + named: ($jsonValue['named'] as num?)?.toInt(), ), ), ); @@ -1047,7 +1048,7 @@ SimpleClassOfRecord _$SimpleClassOfRecordFromJson(Map json) => _$recordConvert( $jsonValue[r'$1'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -1055,7 +1056,7 @@ SimpleClassOfRecord _$SimpleClassOfRecordFromJson(Map json) => named: _$recordConvert( $jsonValue['named'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -1090,7 +1091,7 @@ SimpleClassNullableOfRecord _$SimpleClassNullableOfRecordFromJson( _$recordConvert( $jsonValue[r'$1'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -1098,7 +1099,7 @@ SimpleClassNullableOfRecord _$SimpleClassNullableOfRecordFromJson( named: _$recordConvert( $jsonValue['named'], ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), 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 73866adf9..234afad93 100644 --- a/json_serializable/test/supported_types/input.type_set.g.dart +++ b/json_serializable/test/supported_types/input.type_set.g.dart @@ -251,7 +251,7 @@ SimpleClassOfDuration _$SimpleClassOfDurationFromJson( Map json) => SimpleClassOfDuration( (json['value'] as List) - .map((e) => Duration(microseconds: e as int)) + .map((e) => Duration(microseconds: (e as num).toInt())) .toSet(), ); @@ -265,7 +265,7 @@ SimpleClassNullableOfDuration _$SimpleClassNullableOfDurationFromJson( Map json) => SimpleClassNullableOfDuration( (json['value'] as List?) - ?.map((e) => Duration(microseconds: e as int)) + ?.map((e) => Duration(microseconds: (e as num).toInt())) .toSet(), ); @@ -279,7 +279,8 @@ SimpleClassOfDurationNullable _$SimpleClassOfDurationNullableFromJson( Map json) => SimpleClassOfDurationNullable( (json['value'] as List) - .map((e) => e == null ? null : Duration(microseconds: e as int)) + .map((e) => + e == null ? null : Duration(microseconds: (e as num).toInt())) .toSet(), ); @@ -294,7 +295,8 @@ SimpleClassNullableOfDurationNullable Map json) => SimpleClassNullableOfDurationNullable( (json['value'] as List?) - ?.map((e) => e == null ? null : Duration(microseconds: e as int)) + ?.map((e) => + e == null ? null : Duration(microseconds: (e as num).toInt())) .toSet(), ); @@ -482,7 +484,7 @@ Map _$SimpleClassNullableOfFromJsonObjectParamToJson( SimpleClassOfInt _$SimpleClassOfIntFromJson(Map json) => SimpleClassOfInt( - (json['value'] as List).map((e) => e as int).toSet(), + (json['value'] as List).map((e) => (e as num).toInt()).toSet(), ); Map _$SimpleClassOfIntToJson(SimpleClassOfInt instance) => @@ -493,7 +495,7 @@ Map _$SimpleClassOfIntToJson(SimpleClassOfInt instance) => SimpleClassNullableOfInt _$SimpleClassNullableOfIntFromJson( Map json) => SimpleClassNullableOfInt( - (json['value'] as List?)?.map((e) => e as int).toSet(), + (json['value'] as List?)?.map((e) => (e as num).toInt()).toSet(), ); Map _$SimpleClassNullableOfIntToJson( @@ -505,7 +507,7 @@ Map _$SimpleClassNullableOfIntToJson( SimpleClassOfIntNullable _$SimpleClassOfIntNullableFromJson( Map json) => SimpleClassOfIntNullable( - (json['value'] as List).map((e) => e as int?).toSet(), + (json['value'] as List).map((e) => (e as num?)?.toInt()).toSet(), ); Map _$SimpleClassOfIntNullableToJson( @@ -517,7 +519,9 @@ Map _$SimpleClassOfIntNullableToJson( SimpleClassNullableOfIntNullable _$SimpleClassNullableOfIntNullableFromJson( Map json) => SimpleClassNullableOfIntNullable( - (json['value'] as List?)?.map((e) => e as int?).toSet(), + (json['value'] as List?) + ?.map((e) => (e as num?)?.toInt()) + .toSet(), ); Map _$SimpleClassNullableOfIntNullableToJson( @@ -625,7 +629,7 @@ SimpleClassOfRecord _$SimpleClassOfRecordFromJson(Map json) => .map((e) => _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), @@ -658,7 +662,7 @@ SimpleClassNullableOfRecord _$SimpleClassNullableOfRecordFromJson( ?.map((e) => _$recordConvert( e, ($jsonValue) => ( - $jsonValue[r'$1'] as int, + ($jsonValue[r'$1'] as num).toInt(), $jsonValue[r'$2'] as String, truth: $jsonValue['truth'] as bool, ), diff --git a/json_serializable/tool/readme/readme_examples.g.dart b/json_serializable/tool/readme/readme_examples.g.dart index c22e9025b..b3a60ab36 100644 --- a/json_serializable/tool/readme/readme_examples.g.dart +++ b/json_serializable/tool/readme/readme_examples.g.dart @@ -9,7 +9,7 @@ part of 'readme_examples.dart'; // ************************************************************************** Sample1 _$Sample1FromJson(Map json) => Sample1( - Sample2.fromJson(json['value'] as int), + Sample2.fromJson((json['value'] as num).toInt()), ); Map _$Sample1ToJson(Sample1 instance) => { @@ -17,7 +17,7 @@ Map _$Sample1ToJson(Sample1 instance) => { }; Sample3 _$Sample3FromJson(Map json) => Sample3( - Sample3._fromJson(json['value'] as int), + Sample3._fromJson((json['value'] as num).toInt()), ); Map _$Sample3ToJson(Sample3 instance) => { @@ -25,7 +25,7 @@ Map _$Sample3ToJson(Sample3 instance) => { }; Sample4 _$Sample4FromJson(Map json) => Sample4( - const EpochDateTimeConverter().fromJson(json['value'] as int), + const EpochDateTimeConverter().fromJson((json['value'] as num).toInt()), ); Map _$Sample4ToJson(Sample4 instance) => {