Search code examples
flutterfreezedjson-serializable

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

  • 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