-
Notifications
You must be signed in to change notification settings - Fork 401
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
[Feature Request] Add option to choose which fields should be added to toJson and fromJson #1244
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a more structured approach to #1178 I already started with the implementation and have written the necessary tests, but there are still some open questions, which I listed below. Once those are answered, I should be able to finish the implementation of this feature.
Problem
The current implementation does not give a lot of flexibility when it comes to defining which properties should be included in
fromJson
andtoJson
.A property will be included in
fromJson
if all of the following are true:JsonKey.ignore == false
JsonSerializable.createFactory == true
A property will be included in
toJson
if one of the cases below is true:JsonKey.ignore == false
JsonSerializable.createToJson == true
JsonSerializable.createFactory == true
JsonKey.ignore == false
JsonSerializable.createToJson == true
JsonSerializable.createFactory == false
Proposal
Add
JsonKey.includeWith
, which is of typeenum IncludeWith{none, fromJson, toJson, both}
. IfJsonKey.includeWith
is null, we resort to the previously described behavior. Otherwise, the decisions will be made as described below. This way, we ensure backwards compatibility.A property will be included in
fromJson
if all of the following are true:JsonKey.includeWith
is one of{fromJson, both}
JsonSerializable.createFactory == true
A property will be include in
toJson
if all of the following are true:JsonKey.includeWith
is one of{toJson, both}
JsonSerializable.createToJson == true
Open Questions
fromJson
andtoJson
. I am not sure what this field map is used for or whether it would be a breaking change, if we just included all properties that are either part offromJson
ortoJson
.ignore
andincludeWith
are non null?JsonKey
annotation?@JsonKey(includeWith: IncludeWith.toJson)
is used on a setter, that has no corresponding getter and vice versa?@JsonKey(includeWith: IncludeWith.both)
is used on a field that only has a setter or getter?The text was updated successfully, but these errors were encountered: