Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attributes which aren't in the constructor aren't serialized #274

Closed
joostlek opened this issue Aug 5, 2018 · 6 comments · Fixed by #1256
Closed

Attributes which aren't in the constructor aren't serialized #274

joostlek opened this issue Aug 5, 2018 · 6 comments · Fixed by #1256

Comments

@joostlek
Copy link

joostlek commented Aug 5, 2018

Hey,

I found an issue whilst working on my flutter app.

(Bit of context first)
I have an Activity, it's a base model, containing a DateTime, Firestore DocumentID, user and a type. That type is an enum, and currently I have ~11 activities, all inheriting the Activity class. And adding more attributes. This all works.

To deserialize the activities, I check on the Type attribute, and get the right class to construct the Activity with.

But because the Type of each different class is the same, I didn't include it as an argument in the constructor (When I am constructing a CreateUser, I don't want to send the type again because if I create a CreateUser, I want a CreateUser, not a CreateUser with a RemoveUser type)

So all the extended models have a constructor with a super constructor, and that type is not passed as argument but is just in the super call (CreateUser(.....) : Super(Activities.CreateUser).

This would create an object with type: CreateUser.

But when I am Serializing, it doesnt Serialize the type attribute. So when it's stored in Firestore it doesn't show what Type it is, thus I can't deserialize it, because I don't know what constructor I need to use.

I fixed this by just putting a , [Activities type] to the constructor.

Relevant code is in GitHub.com/joostlek/eros
(The lib/models/activity/Activity is the main class)

Thanks
Keep up the great work you re doing

@kevmoo
Copy link
Collaborator

kevmoo commented Aug 6, 2018

That's a tricky one. I could imagine adding a special-case annotation for a field – something like "serialize me, even though I'm not in a ctor".

An interesting feature request. Sounds like you have a work-around.

@joostlek
Copy link
Author

joostlek commented Aug 6, 2018

Yes, the workaround works, but having it working like intended is also a nice to have 😂

@kevmoo kevmoo added the P3 low label Jan 9, 2019
@dominickj-tdi
Copy link

dominickj-tdi commented Apr 29, 2019

I have a very similar situation. I want to have a 'type' field that is to be saved in the output JSON when I serialize an object, so I can read that field when deserializing so I know what type class to deserialize it as. My current workaround is adding an extra line to the toJson() method to manually add that missing attribute..

I previously mentioned this in a related issue, #24 It is essentially the same use case and probably the same solution would fix both of these issues.

@esDotDev
Copy link

esDotDev commented Jun 9, 2021

Mentioned this here, but we have the exact same use case, we're sharing data classes on the backend, and we need to embed a type. #891

The work around of adding an optional field is not great, as it's mis-leading code. You're allowing the type can be overridden, which is a bug vector and could confuse some future developer.

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants