Skip to content

Commit c78b548

Browse files
committed
Simplify handling of nullable maps
1 parent 68b3f24 commit c78b548

File tree

5 files changed

+22
-27
lines changed

5 files changed

+22
-27
lines changed

json_serializable/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.5
2+
3+
* Small change to how nullable `Map` values are deserialized.
4+
15
## 0.5.4+1
26

37
* Fixed a bug introduced in `0.5.4` in some cases where enum values are nested

json_serializable/lib/src/type_helpers/map_helper.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import '../constants.dart';
88
import '../shared_checkers.dart';
99
import '../type_helper.dart';
1010
import '../type_helper_context.dart';
11-
import '../utils.dart';
1211

1312
class MapHelper extends TypeHelper {
1413
const MapHelper();
@@ -77,11 +76,11 @@ class MapHelper extends TypeHelper {
7776
}
7877
}
7978

80-
if (valueArgIsAny || simpleJsonTypeChecker.isAssignableFromType(valueArg)) {
81-
// No mapping of the values is required!
82-
83-
var result = 'new Map<String, $valueArg>.from($expression as Map)';
84-
return commonNullPrefix(context.nullable, expression, result);
79+
if (!context.nullable &&
80+
(valueArgIsAny ||
81+
simpleJsonTypeChecker.isAssignableFromType(valueArg))) {
82+
// No mapping of the values or null check required!
83+
return 'new Map<String, $valueArg>.from($expression as Map)';
8584
}
8685

8786
// In this case, we're going to create a new Map with matching reified

json_serializable/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 0.5.4+1
2+
version: 0.5.5-dev
33
author: Dart Team <misc@dartlang.org>
44
description: Generates utilities to aid in serializing to/from JSON.
55
homepage: https://github.com/dart-lang/json_serializable

json_serializable/test/kitchen_sink_test_files/kitchen_sink.g.dart

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,17 @@ KitchenSink _$KitchenSinkFromJson(Map json) => new KitchenSink(
2929
?.map((e) => e == null ? null : DateTime.parse(e as String))
3030
?.toList()
3131
..map = json['map'] as Map
32-
..stringStringMap = json['stringStringMap'] == null
33-
? null
34-
: new Map<String, String>.from(json['stringStringMap'] as Map)
35-
..dynamicIntMap = json['dynamicIntMap'] == null
36-
? null
37-
: new Map<String, int>.from(json['dynamicIntMap'] as Map)
32+
..stringStringMap = (json['stringStringMap'] as Map)
33+
?.map((k, e) => new MapEntry(k as String, e as String))
34+
..dynamicIntMap =
35+
(json['dynamicIntMap'] as Map)?.map((k, e) => new MapEntry(k, e as int))
3836
..objectDateTimeMap = (json['objectDateTimeMap'] as Map)?.map(
3937
(k, e) => new MapEntry(k, e == null ? null : DateTime.parse(e as String)))
4038
..crazyComplex = (json['crazyComplex'] as List)
4139
?.map((e) => (e as Map)?.map((k, e) => new MapEntry(
42-
k as String,
43-
(e as Map)?.map(
44-
(k, e) => new MapEntry(k as String, (e as List)?.map((e) => (e as List)?.map((e) => e == null ? null : DateTime.parse(e as String))?.toList())?.toList())))))
40+
k as String, (e as Map)?.map((k, e) => new MapEntry(k as String, (e as List)?.map((e) => (e as List)?.map((e) => e == null ? null : DateTime.parse(e as String))?.toList())?.toList())))))
4541
?.toList()
46-
..val = json['val'] == null ? null : new Map<String, bool>.from(json['val'] as Map)
42+
..val = (json['val'] as Map)?.map((k, e) => new MapEntry(k as String, e as bool))
4743
..writeNotNull = json['writeNotNull'] as bool
4844
..string = json[r'$string'] as String
4945
..simpleObject = json['simpleObject'] == null ? null : new SimpleObject.fromJson(json['simpleObject'] as Map)

json_serializable/test/kitchen_sink_test_files/kitchen_sink.wrapped.g.dart

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,17 @@ KitchenSink _$KitchenSinkFromJson(Map json) => new KitchenSink(
2929
?.map((e) => e == null ? null : DateTime.parse(e as String))
3030
?.toList()
3131
..map = json['map'] as Map
32-
..stringStringMap = json['stringStringMap'] == null
33-
? null
34-
: new Map<String, String>.from(json['stringStringMap'] as Map)
35-
..dynamicIntMap = json['dynamicIntMap'] == null
36-
? null
37-
: new Map<String, int>.from(json['dynamicIntMap'] as Map)
32+
..stringStringMap = (json['stringStringMap'] as Map)
33+
?.map((k, e) => new MapEntry(k as String, e as String))
34+
..dynamicIntMap =
35+
(json['dynamicIntMap'] as Map)?.map((k, e) => new MapEntry(k, e as int))
3836
..objectDateTimeMap = (json['objectDateTimeMap'] as Map)?.map(
3937
(k, e) => new MapEntry(k, e == null ? null : DateTime.parse(e as String)))
4038
..crazyComplex = (json['crazyComplex'] as List)
4139
?.map((e) => (e as Map)?.map((k, e) => new MapEntry(
42-
k as String,
43-
(e as Map)?.map(
44-
(k, e) => new MapEntry(k as String, (e as List)?.map((e) => (e as List)?.map((e) => e == null ? null : DateTime.parse(e as String))?.toList())?.toList())))))
40+
k as String, (e as Map)?.map((k, e) => new MapEntry(k as String, (e as List)?.map((e) => (e as List)?.map((e) => e == null ? null : DateTime.parse(e as String))?.toList())?.toList())))))
4541
?.toList()
46-
..val = json['val'] == null ? null : new Map<String, bool>.from(json['val'] as Map)
42+
..val = (json['val'] as Map)?.map((k, e) => new MapEntry(k as String, e as bool))
4743
..writeNotNull = json['writeNotNull'] as bool
4844
..string = json[r'$string'] as String
4945
..simpleObject = json['simpleObject'] == null ? null : new SimpleObject.fromJson(json['simpleObject'] as Map)

0 commit comments

Comments
 (0)