Skip to content

Commit 07c9136

Browse files
committed
Respect the constructor param for classes w/ custom fromJson ctor
1 parent de541b1 commit 07c9136

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.0+1
2+
3+
* When serializing classes that implement their own `fromJson` constructor,
4+
honor their constructor parameter type.
5+
16
## 0.2.0
27

38
* **BREAKING** Types are now segmented into their own libraries.

lib/src/type_helpers/json_helper.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,20 @@ class JsonHelper extends TypeHelper {
2828
return null;
2929
}
3030

31+
var classElement = targetType.element as ClassElement;
32+
var fromJsonCtor =
33+
classElement.constructors.firstWhere((ce) => ce.name == 'fromJson');
34+
// TODO: should verify that this type is a valid JSON type...but for now...
35+
var asCastType = fromJsonCtor.parameters.single.type;
36+
37+
var asCast = '';
38+
if (!asCastType.isDynamic && !asCastType.isObject) {
39+
asCast = ' as $asCastType';
40+
}
41+
3142
// TODO: the type could be imported from a library with a prefix!
3243
// github.com/dart-lang/json_serializable/issues/19
33-
var result =
34-
"new ${targetType.name}.fromJson($expression as Map<String, dynamic>)";
44+
var result = "new ${targetType.name}.fromJson($expression$asCast)";
3545

3646
if (nullable) {
3747
result = "$expression == null ? null : " + result;

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.2.0
2+
version: 0.2.0+1
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

test/test_files/json_test_example.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Order extends Object with _$OrderSerializerMixin {
3434
int count;
3535
bool isRushed;
3636
final UnmodifiableListView<Item> items;
37+
Platform platform;
3738

3839
int get price => items.fold(0, (total, item) => item.price + total);
3940

@@ -69,3 +70,24 @@ class Item extends Object with _$ItemSerializerMixin {
6970
}
7071

7172
bool _deepEquals(a, b) => const DeepCollectionEquality().equals(a, b);
73+
74+
class Platform {
75+
final String description;
76+
77+
static const Platform foo = const Platform._('foo');
78+
static const Platform undefined = const Platform._('undefined');
79+
const Platform._(this.description);
80+
81+
factory Platform.fromJson(String value) {
82+
switch (value) {
83+
case 'foo':
84+
return foo;
85+
case 'undefined':
86+
return undefined;
87+
default:
88+
throw new ArgumentError.value(value, 'value', 'Not a supported value.');
89+
}
90+
}
91+
92+
String toJson() => description;
93+
}

test/test_files/json_test_example.g.dart

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

0 commit comments

Comments
 (0)