'Freezed and json_serializable: How to use a custom converter

I want to add a custom converter to a freezed class like in this answer.

I tried it with this code:

@freezed
class NewsPost with _$NewsPost {
  factory NewsPost({
    @JsonKey(name: "date") @TimestampConverter() DateTime? date,
  }) = _NewsPost;

  factory NewsPost.fromJson(Map<String, dynamic> json) =>
      _$NewsPostFromJson(json);
}

But it did not work. Any ideas are more than welcome!

For your interest, this is my Converter:

class TimestampConverter implements JsonConverter<DateTime, Timestamp> {
  const TimestampConverter();

  @override
  DateTime fromJson(Timestamp timestamp) {
    return timestamp.toDate();
  }

  @override
  Timestamp toJson(DateTime date) => Timestamp.fromDate(date);
}

Thank you :-)



Solution 1:[1]

Since null safety was introduced, for JsonConverter to work with the freezed generator the nullability of the types declared in JsonConverter need to match the nullability of the type in the freezed class.

If the types do not match, freezed ignores the converter.

So using your example:

@freezed
class NewsPost with _$NewsPost {
  factory NewsPost({
    @TimestampOrNullConverter() DateTime? date, // <-- this is nullable, so the converter needs to handle null
    @TimestampConverter() DateTime createdAt, // <-- not nullable, so your exsiting converter will work
  }) = _NewsPost;

  factory NewsPost.fromJson(Map<String, dynamic> json) =>
      _$NewsPostFromJson(json);
}

class TimestampConverter implements JsonConverter<DateTime, Timestamp> {
  const TimestampConverter();

  @override
  DateTime fromJson(Timestamp timestamp) {
    return timestamp.toDate();
  }

  @override
  Timestamp toJson(DateTime date) => Timestamp.fromDate(date);
}

class TimestampOrNullConverter implements JsonConverter<DateTime?, Timestamp?> {
  const TimestampOrNullConverter();

  @override
  DateTime? fromJson(Timestamp? timestamp) {
    return timestamp?.toDate();
  }

  @override
  Timestamp? toJson(DateTime? date) => date == null ? null : Timestamp.fromDate(date);
}

Tested and working on flutter 2.5.3 with the following dependency versions:

dev_dependencies:
  build_runner: ^2.1.4
  freezed: ^0.15.0+1
  json_serializable: ^5.0.2

Solution 2:[2]

Well, it´s not the most elegant solution, but it works.

@freezed
class NewsPost with _$NewsPost {
  factory NewsPost({
    @JsonKey(
       name: "date", 
       fromJson: dateTimeFromJson, 
       toJson: dateTimeToJson,
    ) DateTime? date,
  }) = _NewsPost;

  factory NewsPost.fromJson(Map<String, dynamic> json) =>
      _$NewsPostFromJson(json);
}


DateTime dateTimeFromJson(Timestamp timestamp) {
    return timestamp.toDate();
}

Timestamp dateTimeToJson(DateTime date) => Timestamp.fromDate(date);

I think it´s a solution. Using JsonConverter I can't get it to work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Benjamin
Solution 2 Neifen