Skip to content

Contextual custom types/encoding (for DateTime & Firestore Timestamps) #1132

@jimmyff

Description

@jimmyff

Hi, I have a large app using built_value and I'm migrating it to using freezed (and with that json_serializable). This would allow me to cut out a huge amount of boilerplate and give me all the lovely freezed features like deep copy etc.

The app has data models that are shared between the client and server. When they are serialized to/from Firestore the DateTime objects are converted to Timestamps. With built_value I have two sets of serializers, one results in pure Dart DateTime objects and the other (firestoreSerializers) handles the pesky Timestamps, as these are passed to and from the json methods I have the flexibility to use the necessary serializers based on the context:

// Current solution of passing serializers (with built_value)
FooBar.fromJsonMap(
      Serializers serializers, Map<String, dynamic> data) => 
         serializers.deserializeWith(ChatMessage.serializer, data)!;

 Map<String, dynamic> toJsonMap(Serializers serializers) =>
         Map.of(serializers.serialize(this, //....

Is there a way to accomplish something similar to this with freezed? I basically need handle the serialization & deserialization of DateTime's differently according to context requirements? There are situations where both the client and server need to serialize & deserialize objects to/from firestore as well as none-Firestore contexts (sending/receiving the objects with cloud-endpoint, caching etc).

I've looked at using package:flutterfire_json_converters which seems to address my issue but then there isn't any way to handle dart only environments.

Thanks

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationquestionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions