-
Notifications
You must be signed in to change notification settings - Fork 411
Serialize private field #537
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I also had this problem and took some time to work out what was going one. If you have a setter and a getter the json will be generated. The documentation should note the fact that private fields are not generated. I would argue that private fields should be supported as I may need to deserialise json into a class but not want to expose some internal fields outside of the class. |
@bsutton Agree on your point regarding private fields, and with all that my library |
@k-paxian thanks for the link. I've passed it onto the team member looking after a json code bits. |
I would love to be able to serialize private fields. My use-case: I would like to serialize and deserialize the state of the application with The workaround I found is using the The solution works, but it is confusing code. The annotation communicates that the field was made visible for testing, instead of communicating that the field was made public for serialization. // The JsonSerializable and JsonKey are just examples, their args could be anything else
@JsonSerializable(fieldRename: FieldRename.snake)
class SomeState {
@JsonKey()
@visibleForTesting // <--- THE IMPORTANT PART
final DateTime dateTime;
} The |
In addition to the general and reasonable utility of saving private fields, the situation that I'm currently running into is that I had to destructure |
I deprecated some fields in my model but as I couldn't make them private I have to use them internally, so I get warnings from the linter. Having this would probably solve my problem. |
My code is full of @VisibleForTesting workaround occurrences (mentioned above). This feature would be really useful 🙏 |
For some reason, the @VisibleForTesting did not work for me. I had a private field _uuid in my class, and noting the comment by @bsutton above, I added a getter (which I needed anyway) and an empty setter:
That worked for me. |
Duplicate of #569 Trying to align all of these requests into one place! |
I have a (draft) PR out on this. I'd LOVE (early) feedback about if this works for what folks need here: #1256 I still need to do some documentation and testing... |
I am very surprised I can't serialize a basic private field, I guess it is by design because as soon as I make it public, it works well.
I have a quite basic use case.
I have a private field storing a global id count, used to generate local id of child objects.
So other object should have access to nextCatId or nextDogId but not _nextId.
And _nextId must be saved and loaded locally in my document-based dababase.
Any thoughts ?
The text was updated successfully, but these errors were encountered: