k-paxian / dart-json-mapper

Serialize / Deserialize Dart Objects to / from JSON
https://pub.dev/packages/dart_json_mapper
Other
400 stars 33 forks source link

ignoreNullMembers seems to break the deserialization #202

Closed Lars-Sommer closed 1 year ago

Lars-Sommer commented 1 year ago

I have a problem with the ignoreNullMembers annotation. As far as I understand, you can use this annotation if you would like to ignore null members or not existing json members of the json that is about to be deserialized.

I have two models, with the only difference being this annotation:

@JsonSerializable()
@Json(caseStyle: CaseStyle.pascal)
class TestModel1 {
  String id = '';
  String? address = '';
}
@JsonSerializable()
@Json(ignoreNullMembers: true)
@Json(caseStyle: CaseStyle.pascal)
class TestModel2 {
  String id = '';
  String? address = '';
}

Now if I parse the exact same data where none if the properties are null, to each model, I would expect the same result. But this is not the case:

image

Every property in TestModel2 is empty strings (their default value). image

image

I would like to use the ignoreNullMembers annotation, but of course also deserialization. So am I doing anything wrong?

k-paxian commented 1 year ago

Try to join the two @Json annotations to just one, having both attributes side by side. Intention is to have just one annotation for the class level settings

@Json(ignoreNullMembers: true, caseStyle: CaseStyle.pascal)
Lars-Sommer commented 1 year ago

Aaah, rookie mistake, I did not know that you could join the annotations.

Thanks 👍