json_serializable.dart
json_serializable.dart copied to clipboard
Serialize private field
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 are private to the state, and 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 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 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.
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.
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:
String _uuid = 'My unique ID';
String get uuid => _uuid;
// workaround for json_serializable
set uuid(_) {}
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: https://github.com/google/json_serializable.dart/pull/1256
I still need to do some documentation and testing...