Skip to content

Commit 3103075

Browse files
committed
more inference fixes
1 parent 3048a23 commit 3103075

File tree

5 files changed

+51
-35
lines changed

5 files changed

+51
-35
lines changed

example/example.g.dart

Lines changed: 15 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/json_serializable_generator.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ class JsonSerializableGenerator
188188
// Generate the static factory method
189189
//
190190
buffer.writeln();
191-
buffer.writeln('$className ${prefix}FromJson(Map json) =>');
191+
buffer
192+
.writeln('$className ${prefix}FromJson(Map<String, dynamic> json) =>');
192193
buffer.write(' new $className(');
193194
buffer.writeAll(
194195
ctorArguments.map((paramElement) => _jsonMapAccessToField(
@@ -378,15 +379,22 @@ class JsonSerializableGenerator
378379
return "$varExpression as Map<String, dynamic>";
379380
}
380381

382+
// In this case, we're going to create a new Map with matching reified
383+
// types.
384+
381385
var itemVal = "v$depth";
382386
var itemSubVal =
383387
_writeAccessToJsonValue(itemVal, valueArg, depth: depth + 1);
384388

385-
// In this case, we're going to create a new Map with matching reified
386-
// types.
389+
if (itemVal == itemSubVal) {
390+
// No mapping of the values is required!
391+
return "$varExpression == null ? null :"
392+
"new Map<String, $valueArg>.from($varExpression)";
393+
}
394+
387395
return "$varExpression == null ? null :"
388396
"new Map<String, $valueArg>.fromIterables("
389-
"($varExpression as Map).keys,"
397+
"($varExpression as Map<String, dynamic>).keys,"
390398
"($varExpression as Map).values.map(($itemVal) => $itemSubVal))";
391399
} else if (searchType.isDynamic || searchType.isObject) {
392400
// just return it as-is. We'll hope it's safe.

lib/src/type_helper.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ class JsonHelper extends TypeHelper {
9393
@override
9494
String deserialize(DartType targetType, String expression) {
9595
// TODO: the type could be imported from a library with a prefix!
96-
return "new ${targetType.name}.fromJson($expression)";
96+
// github.com/dart-lang/json_serializable/issues/19
97+
return "new ${targetType.name}.fromJson($expression as Map<String, dynamic>)";
9798
}
9899
}
99100

@@ -114,5 +115,7 @@ class DateTimeHelper extends TypeHelper {
114115

115116
@override
116117
String deserialize(DartType targetType, String expression) =>
117-
"DateTime.parse($expression)";
118+
// TODO(kevmoo) `String` here is ignoring
119+
// github.com/dart-lang/json_serializable/issues/19
120+
"DateTime.parse($expression as String)";
118121
}

test/test_files/json_test_example.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Person extends Object with _$PersonSerializerMixin {
8484

8585
Person(this.firstName, this.lastName, {this.middleName, this.dateOfBirth});
8686

87-
factory Person.fromJson(Map json) => _$PersonFromJson(json);
87+
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
8888

8989
bool operator ==(Object other) =>
9090
other is Person &&
@@ -124,7 +124,7 @@ class Item extends Object with _$ItemSerializerMixin {
124124

125125
Item([this.price]);
126126

127-
factory Item.fromJson(json) => _$ItemFromJson(json);
127+
factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);
128128

129129
bool operator ==(Object other) =>
130130
other is Item &&

test/test_files/json_test_example.g.dart

Lines changed: 17 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)