Skip to content

Respect the constructor param for classes w/ custom fromJson ctor #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.2.0+1

* When serializing classes that implement their own `fromJson` constructor,
honor their constructor parameter type.

## 0.2.0

* **BREAKING** Types are now segmented into their own libraries.
Expand Down
14 changes: 12 additions & 2 deletions lib/src/type_helpers/json_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ class JsonHelper extends TypeHelper {
return null;
}

var classElement = targetType.element as ClassElement;
var fromJsonCtor =
classElement.constructors.firstWhere((ce) => ce.name == 'fromJson');
// TODO: should verify that this type is a valid JSON type...but for now...
var asCastType = fromJsonCtor.parameters.single.type;

var asCast = '';
if (!asCastType.isDynamic && !asCastType.isObject) {
asCast = ' as $asCastType';
}

// TODO: the type could be imported from a library with a prefix!
// github.com/dart-lang/json_serializable/issues/19
var result =
"new ${targetType.name}.fromJson($expression as Map<String, dynamic>)";
var result = "new ${targetType.name}.fromJson($expression$asCast)";

if (nullable) {
result = "$expression == null ? null : " + result;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 0.2.0
version: 0.2.0+1
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serializable
Expand Down
22 changes: 22 additions & 0 deletions test/test_files/json_test_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Order extends Object with _$OrderSerializerMixin {
int count;
bool isRushed;
final UnmodifiableListView<Item> items;
Platform platform;

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

Expand Down Expand Up @@ -69,3 +70,24 @@ class Item extends Object with _$ItemSerializerMixin {
}

bool _deepEquals(a, b) => const DeepCollectionEquality().equals(a, b);

class Platform {
final String description;

static const Platform foo = const Platform._('foo');
static const Platform undefined = const Platform._('undefined');
const Platform._(this.description);

factory Platform.fromJson(String value) {
switch (value) {
case 'foo':
return foo;
case 'undefined':
return undefined;
default:
throw new ArgumentError.value(value, 'value', 'Not a supported value.');
}
}

String toJson() => description;
}
14 changes: 11 additions & 3 deletions test/test_files/json_test_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.