Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jimmyff opened this issue Oct 15, 2024 · 4 comments
Closed
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@jimmyff
Copy link

jimmyff commented Oct 15, 2024

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

@jimmyff jimmyff added documentation Improvements or additions to documentation needs triage labels Oct 15, 2024
@rrousselGit
Copy link
Owner

Are you looking for JsonConverter? Their use is documented in the readme

@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels Oct 16, 2024
@jimmyff
Copy link
Author

jimmyff commented Oct 16, 2024

Hey @rrousselGit thanks for the response. I have looked at JsonConverter but I don't think it solves my requirement as I don't believe it gives me the ability to handle DateTime as Timestamps or DateTime's according to requirements. I might have missed a trick with it though?

I have a number of Cloud Run endpoints using the dart SDK (no flutter) using these models, in this situation I need the time fields to serialize/deserialize as DateTime's. I'm not even sure how this would look with freezed? I played around with a custom object with branching imports but I'm not sure if that would work. In the flutter client I need the same default behavior (DateTime objects) but with the ability to serialize+deserialze all the DateTIme fields to Timestamp's when communicating to/from Firestore.

Maybe my use-case is too niche?

@rrousselGit
Copy link
Owner

JsonConverter is for that purpose. It's often used to convert timestamps<>date
I'd suggest looking into it :)

This isn't a Freezed feature btw. It's a json_serializable one.

@jimmyff
Copy link
Author

jimmyff commented Oct 17, 2024

Yeah I'm aware of JsonConverter but my question/reporting lack of documentation was around how to switch which JsonConverter I'm using at runtime. I had hoped maybe there was a solution in freezed for this kind of thing but I'll try json_serializable. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants