google / google/json_serializable.dart

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

Open
#1,449 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

Hi, I have a large app using `built_value` and I'm migrating it to using `freezed` and `json_serializable`. 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:

```dart
// Current solution of passing serializers (not using json_serializable)
FooBar.fromJsonMap(
Serializers serializers, Map data) =>
serializers.deserializeWith(ChatMessage.serializer, data)!;

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

```

Is there a Json Serializable recommended way to give me the flexibility to share the same data models but 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).

Thanks

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.