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

Support JsonSerializable(genericArgumentFactories: true) #696

Merged
merged 6 commits into from Jul 15, 2022

Conversation

TimWhiting
Copy link
Contributor

@TimWhiting TimWhiting commented Jun 28, 2022

New attempt at resolving #370
fixes: #331

@rrousselGit
Copy link
Owner

Sorry for the delay. I'll look into it

@@ -452,7 +452,7 @@ class Generic<T> with _$Generic<T> {
factory Generic(@DataConverter() T a) = _Generic<T>;

factory Generic.fromJson(Map<String, dynamic> json) =>
_$GenericFromJson<T>(json);
_$GenericFromJson<T>(json, (DataConverter<T>()).fromJson);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently is a negative change in my opinion.
Two ways to resolve this.

  1. Automatically detect JsonConverter annotations that handle generic types matching the surrounding class. (Very difficult)
  2. Explicit opt in / out of generic argument json factories, much easier, but unfortunate both if we make it opt in or opt out, and I'm not sure which I would prefer.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difficulty with 1 in your opinion?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that this is technically a breaking change bothers me.
I think we'd need to be opt-in.

Copy link
Contributor Author

@TimWhiting TimWhiting Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difficulty is that you have to make sure that every parameter in every constructor has a type converter for any type recursively containing any generic type of the class, even for nested types! So a parameter that has type List<T> would need a type converter for List<T>.

@TimWhiting
Copy link
Contributor Author

TimWhiting commented Jun 29, 2022

Sorry for the delay. I'll look into it

Thanks, this is something I've wanted several times. This is a much cleaner approach than before, and rather simple, probably because of the rearchitecture of the freezed code you did recently. I've highlighted one issue that I see with my current solution. I'd like your opinion on how to proceed there. I've attempted the first solution separate from this PR, but I found it rather difficult to correlate type parameters on specific annotations of constructor arguments (for JsonConverters) to the generic parameter of the surrounding class.

@rrousselGit
Copy link
Owner

We'd need a matching parameter on the annotation and a config in the build.yaml file

@TimWhiting TimWhiting force-pushed the master branch 2 times, most recently from a00ebca to c6928ab Compare July 8, 2022 04:07
@TimWhiting
Copy link
Contributor Author

TimWhiting commented Jul 8, 2022

Updated to the opt-in solution with a parameter in the annotation and config in the build.yaml file. Also updated tests to exercise the new integration test objects.

@rrousselGit
Copy link
Owner

Could you add doc for this? Including the build.yaml config, which likely will be used often

@TimWhiting
Copy link
Contributor Author

@rrousselGit Added to the readme

@TimWhiting
Copy link
Contributor Author

Any more feedback, or can this get merged?

@rrousselGit rrousselGit merged commit 0ce1e0c into rrousselGit:master Jul 15, 2022
@rrousselGit
Copy link
Owner

Nothing to say, thanks for your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support JsonSerializable(genericArgumentFactories: true)
2 participants