google / google/json_serializable.dart

`JsonKey.fromJson` seems to be ignored

Open
#1,409 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

My code:
```dart
@JsonSerializable()
class Person {
@JsonKey(
fromJson: unixTimeToDateTime,
toJson: dateTimeToUnixTime,
includeFromJson: true,
includeToJson: true,
)
DateTime dateOfBirth;

Person({required this.dateOfBirth});
}

DateTime unixTimeToDateTime(int secondsSinceEpoch) =>
DateTime.fromMillisecondsSinceEpoch(
secondsSinceEpoch * 1000,
isUtc: true,
);

int dateTimeToUnixTime(DateTime dateTime) =>
dateTime.millisecondsSinceEpoch ~/ 1000;

```

Generated:
```dart
Person _$PersonFromJson(Map json) => Person(
dateOfBirth: DateTime.parse(json['dateOfBirth'] as String),
);
```

Any solution?

UPDATE:
`JsonConverter` is not a solution because in many cases I need to specify the `JsonKey.name` too.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.