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

Add option to omit null fields in fromJson constructor or define default values #74

Closed
AlexanderJohr opened this issue Nov 22, 2017 · 13 comments

Comments

@AlexanderJohr
Copy link
Contributor

Great Library. Finally something to replace Dartson with. However: When deserializing fields that aren't in the json, they will be written to the object as null. In Angular I then get errors like "boolean expression must not be null". I would like to keep the default values assigned to the fields instead of having those overwritten by null:

// Please keep my assigned value, don't overwrite with null
bool myBool = true

Or maybe setting the default value with an annotation would be useful.

@JsonKey(default: true)
bool myBool
@kevmoo
Copy link
Collaborator

kevmoo commented May 24, 2018

@AlexanderJohr – FYI, going to look into this soon...

Something we want for other things. @jakemac53 FYI

@jakemac53
Copy link
Collaborator

+1 for an annotation, and possibly a warning if it sees a default value for a constructor parameter that it knows its going to overwrite with (possibly) null?

kevmoo added a commit that referenced this issue May 25, 2018
kevmoo added a commit that referenced this issue May 25, 2018
@kevmoo kevmoo closed this as completed in 60b29d1 May 29, 2018
@AlexanderJohr
Copy link
Contributor Author

Awesome!

@kevmoo
Copy link
Collaborator

kevmoo commented May 31, 2018

Published, @AlexanderJohr – let us know how it goes...

@AlexanderJohr
Copy link
Contributor Author

@kevmoo Works just great! Now I can remove all those factory constructors which ensured my default values before.

@kevmoo
Copy link
Collaborator

kevmoo commented Jun 13, 2018

👍

@Nico04
Copy link

Nico04 commented Jun 19, 2019

In my projects, several of my classes inherits this class :

abstract class Editable {
  DateTime updatedAt;

  Editable() : updatedAt = DateTime.now();
} 

The idea is to store the date when the objet was created.
It works great when a create objets from my code.
But if the objetc is created from a json (via fromJson), and because the json doesn't include updatedAt, updateAt is overriten with null, which is unwanted.

Setting the default value with @jsonkey(defaultValue: ...) is not enought because if the object is created not from a json, it doens't get his default value...

Any idea ?

@kevmoo
Copy link
Collaborator

kevmoo commented Jun 19, 2019

@Nico04 – this is tough.

You could create get/set pairs with a private field and ignore null values on set to updatedAt.

@Nico04
Copy link

Nico04 commented Jun 19, 2019

But what if I need this behavior globally for all my serializable classes?
Do not override a field with null in fromJson

@kevmoo
Copy link
Collaborator

kevmoo commented Jun 19, 2019

@Nico04 – we're not going to run with this. It'd add a LOT of complexity to the generator to track this.

We're trying to keep things reasonably simple, especially if there is a work-around.

@Nico04
Copy link

Nico04 commented Jun 20, 2019

I'm a bit disapointed but I totally understand your argument.
Thanks for answering so quickly though 👍

@Nico04
Copy link

Nico04 commented May 27, 2020

It seems that now the package has a defaultValue key.
This is great, and it could solve this issue, but you have to set this for each fields and REPEAT default value which is not clean nor practicle.
Any plans on making it a global key please ?

@vihangel
Copy link

I think this can solve your problem

//when the variable has the value that match with the ignore json will not be constructed!

@jsonkey(ignore: null)
final String verificationCode;

https://docs.flutter.dev/development/data-and-backend/json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants