json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Serialize private field

Open Nico04 opened this issue 5 years ago • 8 comments

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 ?

Nico04 avatar Sep 10 '19 17:09 Nico04

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 avatar Oct 24 '19 00:10 bsutton

@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 avatar Dec 19 '19 14:12 k-paxian

@k-paxian thanks for the link. I've passed it onto the team member looking after a json code bits.

bsutton avatar Dec 22 '19 22:12 bsutton

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.

vincevargadev avatar Jun 25 '20 13:06 vincevargadev

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.

matthew-carroll avatar Sep 14 '20 07:09 matthew-carroll

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.

mateusfccp avatar Sep 16 '21 13:09 mateusfccp

My code is full of @visibleForTesting workaround occurrences (mentioned above). This feature would be really useful 🙏

ncuillery avatar Apr 13 '22 13:04 ncuillery

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.

ambstuc avatar Jul 23 '22 16:07 ambstuc

Duplicate of #569

Trying to align all of these requests into one place!

kevmoo avatar Nov 17 '22 23:11 kevmoo

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...

kevmoo avatar Nov 22 '22 01:11 kevmoo