Skip to content

More control over which fields should be added to fromJson and toJson #1178

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

Closed
wants to merge 7 commits into from

Conversation

joeldomke
Copy link

@joeldomke joeldomke commented Jul 15, 2022

Adds the option to JsonKey to include fields in toJson that are not used in the factory (fromJson).

Related issues: #24, #891

@google-cla
Copy link

google-cla bot commented Jul 15, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@joeldomke
Copy link
Author

I guess this approach you suggested could also be used. Let me know if you want me to take a look at that.

@kevmoo
Copy link
Collaborator

kevmoo commented Jul 18, 2022

I think I like the enum route. Just allows us to unify more things!

@joeldomke
Copy link
Author

joeldomke commented Jul 20, 2022

Okay, I should be able to implement this. Two questions:

  1. Currently there exists the unavailableReason 'Setter-only properties are not supported'. I guess this should no longer be the case, since we offer IncludeWith.fromJson? But this would also be a breaking change for classes that have fields that only have a setter, which would then have to be annotated with @JsonKey(includeWith: IncludeWith.ignore).
  2. Same for getters. None of the 4 IncludeWith options match the old behaviour where fields without setters would be ignored in toJson if they are unused during the factory generation. This would mean that all those getters would have to be annotated with @JsonKey(includeWith: IncludeWith.ignore).

Should there be a fifth option, that matches the old behaviour, to prevent those breaking changes?

Comment on lines +84 to +87
@JsonKey(includeWith: IncludeWith.fromJson)
String get setter => someString;

set setter(String value) => someString = value;
Copy link
Author

Choose a reason for hiding this comment

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

Since the JsonKey annotation can't be added to setters, we still need a getter.

Copy link
Author

Choose a reason for hiding this comment

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

I guess it would be possible to also support fields that only have setters? @kevmoo would you consider this PR, if I implemented this, or are there other issues with this PR or just limited time/resources from your side?

@@ -136,3 +136,5 @@ class JsonKey {
}

enum _NullAsDefault { value }

enum IncludeWith { both, toJson, fromJson, ignore, legacy }
Copy link
Author

@joeldomke joeldomke Jul 26, 2022

Choose a reason for hiding this comment

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

I added a fifth option, currently called legacy. Fields that use this option should behave as fields that previously used ignore: false. Currently this is the default option.

@joeldomke joeldomke changed the title Serialize fields that are not used in the factory More control over which fields should be added to fromJson and toJson Sep 21, 2022
@kevmoo
Copy link
Collaborator

kevmoo commented Oct 1, 2022

So. I think I like this BUT! it's a breaking change.

We could bring it in as a non-breaking change, but we'd need to keep the existing ignore field.

Are you on Twitter ? Can you DM me so we can schedule time to chat about this?

Latest changes from json_serializable master
@joeldomke
Copy link
Author

I just saw that you enabled the CI. This PR was a bit of a mess. Two different approaches, only code and no explanation. I am currently preparing a new PR that addresses the ignore related breaking change. It will have a more detailed description and a list of open questions. So you don't have to waste any time on this. I should be able to get it out next week.

@kevmoo
Copy link
Collaborator

kevmoo commented Nov 17, 2022

Can't run with this. Need to have a model that support the old and the new living together.

@kevmoo kevmoo closed this Nov 17, 2022
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.

2 participants