Skip to content

JsonKey.fromJson seems to be ignored #1409

Open
@busslina

Description

@busslina

My code:

@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:

Person _$PersonFromJson(Map<String, dynamic> 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions