-
Notifications
You must be signed in to change notification settings - Fork 400
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
Add json allowlist of specific types #1072
Comments
This is an abuse of JSON – but I get it. PR welcome here! |
Alternatively, do you believe there's a way to define global type-converters? We could technically have a TypeConverter simply cast the value. But ideally, it shouldn't require using the converter on every single property matching |
Was just checking if there are global type-converters, would love that. |
You can create your own json_serializable package and augment the type_converters provided. The APIs are all public-ish. |
A bit more code than adding it to the build.yaml but still fairly simple and would solve the issue 🤔 For anyone reading this who would like an example on how to do that: |
The way I do it in these packages is a bit hacky. I always wished we could specify additional TypeHelpers in the build.yaml, (like ferry does it for example: https://ferrygraphql.com/docs/custom-scalars#configure-custom-serializer ) but this would not be a trivial thing to implement in the existing architecture of json_serializable. |
This is especially relevant now that Firestore ODM is on its way, since it promotes using JsonSerializable to write the models. It would be awesome if the solution for this issue would be a small code snippet that can quickly be added to the getting started instructions over there. |
I must say I don't like the way the models are created in conjunction with firestore types. that makes the app coupled with firestore which is backend implementation details. please consider alternative solutions. |
Any progress on this one? Has an ideal solution been found and we are waiting on implementation? Running into this using Firestore ODM |
@idotalmor |
I could just put |
@kevmoo Assuming it still includes the property in the generated class, yes that should be enough. |
The really tricking thing will be if the type is used like |
Is there an example on how to use JsonConverter/JsonKey to handle this as a workaround? |
I implemented a prototype of how this can be solved with a custom builder here. The ingredients to make this work:
I did not really test this, and I did not think about inheritance/subtyping, so YMMV. But this should be able to work around the issue. |
If that's about having These can be supported late down the road if useful. I doubt these cases will be necessary Thinking about this more, instead of an allowlist what about: class MyJsonConverter extends JsonConverter<NotSerializable> {}
@JsonSerializable(converters: [MyJsonConverter()])
class Example {
NotSerializable first;
NotSerializable second;
} Then if folks want to apply the converters often, they can simply make a custom annotation: const firebaseSerializable = JsonSerializable(converters: [DocumentReferenceConverter(), <some more>]);
@firebaseSerializable
class Example {
final DocumentReference ref; // OK
} |
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:
then json_serializable will complain with the error:
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: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.The text was updated successfully, but these errors were encountered: