-
Notifications
You must be signed in to change notification settings - Fork 420
Description
Currently, json_serializable does not support custom JSON format which contains classes other than Map/String/num/...
An example would be Firestore, which on top of your typical String/bool/... may also have Timestamp/GeoPoint/DocumentReference as Map values.
Meaning that if we're doing:
@JsonSerializable
class Foo {
<...>
final Timestamp timestamp;
}
then json_serializable will complain with the error:
Could not generate `fromJson` code for `timestamp`.
To support the type `Timestamp` you can:
* Use `JsonConverter`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
package:cloud_firestore_odm_generator_integration_test/simple.dart:15:19
The problem is, this error is technically incorrect in this specific use-case since we don't need to encode/decode Timestamp
.
We could use JsonConverter/JsonKey, but that is not ideal since they can't be applied to the entire project.
Proposal
We could add a way to globally tell json_serializable to not do anything when encountering certain types.
This could be a configuration inside the build.yaml
file, such as:
json_serializable:
options:
allowed-types:
- name: Timestamp
source: "package:cloud_firestore/cloud_firestore.dart"
Then, when json_serializable will encounter a property typed as Timestamp
, the generated code will simply perform as cast instead of trying to call fromJson/toJson.