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.
int _nextId = 0;
String get nextCatId => "c${nextId++}";
String get nextDogId => "h${nextId++}";
Any thoughts ?
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
https://github.com/k-paxian/dart-json-mapper supports getters only serialization if you are looking for alternatives of course.
@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 hydrated_bloc, but some of the values I'm serializing can be private to the state, so I'd like to keep them private.
The workaround I found is using the @visibleForTesting annotation to make sure that I don't use the field elsewhere in my codebase.
// The JsonSerializable and JsonKey are just examples, their args could be anything else
@JsonSerializable(
fieldRename: FieldRename.snake,
ignoreUnannotated: true,
includeIfNull: false,
)
class TheState {
@JsonKey()
@visibleForTesting // <--- important part
final DateTime theField;
}
The solution works, but it could cause some confusion. The annotation communicates that the field had to be made visible for testing, instead of communicating that the field was made public for serialization. Maybe you actually want to test through other methods or getters, in which case making the field visible via @visibleForTesting can cause issues once someone has to write new or update old tests.
The dart-json-mapper library from k-paxian looks great, but to be honest, I don't feel like updating my whole codebase to use that library only for this feature.
* actually the bloc itself, but I'm just delegating the serialization to the state
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 Alignment and Rect because I couldn't figure out how to implement a TypeHelper. As a result, I have a bunch of private fields for things like left, top, right, bottom, x, y, and none of these should be public, but I need them in my serialized JSON.
Most helpful comment
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.