Skip to content

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

Closed
Nico04 opened this issue Sep 10, 2019 · 10 comments · Fixed by #1256
Closed

Serialize private field #537

Nico04 opened this issue Sep 10, 2019 · 10 comments · Fixed by #1256

Comments

@Nico04
Copy link

Nico04 commented Sep 10, 2019

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 ?

@bsutton
Copy link

bsutton commented Oct 24, 2019

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.

@k-paxian
Copy link

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

@bsutton
Copy link

bsutton commented Dec 22, 2019

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

@vincevargadev
Copy link

vincevargadev commented Jun 25, 2020

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.

@matthew-carroll
Copy link

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.

@mateusfccp
Copy link

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.

@ncuillery
Copy link

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

@ambstuc
Copy link

ambstuc commented Jul 23, 2022

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.

@kevmoo
Copy link
Collaborator

kevmoo commented Nov 17, 2022

Duplicate of #569

Trying to align all of these requests into one place!

@kevmoo kevmoo closed this as completed Nov 17, 2022
@kevmoo
Copy link
Collaborator

kevmoo commented Nov 22, 2022

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

kevmoo added a commit that referenced this issue Nov 22, 2022
Deprecate JsonKey.ignore

Fixes #24
Fixes #274
Fixes #537
Fixes #569
Fixes #797
Fixes #1100
Fixes #1244
kevmoo added a commit that referenced this issue Nov 22, 2022
Deprecate JsonKey.ignore

Fixes #24
Fixes #274
Fixes #537
Fixes #569
Fixes #797
Fixes #1100
Fixes #1244
kevmoo added a commit that referenced this issue Nov 30, 2022
… serialization (#1256)

Deprecate `JsonKey.ignore`

Fixes #24
Fixes #274
Fixes #537
Fixes #569
Fixes #797
Fixes #1100
Fixes #1244
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
9 participants