k-paxian / dart-json-mapper

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

ignoreNullMembers Annotation is ignored #196

Closed Lars-Sommer closed 1 year ago

Lars-Sommer commented 1 year ago

I have a DTLocation model with a DTGeoPosition object property which is null. I do not want this property to be serialized, I want to omit it. (Actually I want this to be the overall default setting for JsonMapper) This is my current state of the classes:

@JsonSerializable()
@Json(ignoreNullMembers: true)
class DTLocationRequest {
  String? Test;
  DTGeoPosition? GeoPosition;
}

@JsonSerializable()
@Json(ignoreNullMembers: true)
class DTGeoPosition {
  double lng = 0;
  double lat = 0;
  double accuracy = 0;
}

When I serialize a DTLocation class where DTGeoPosition is null: var json1 = JsonMapper.serialize(request); the null property is being serialized and is null: {"Test":"hello","GeoPosition":"null"}

If I serialize with options like this: var json2 = JsonMapper.serialize(request, SerializationOptions(ignoreNullMembers: true)); it works: {"Test":"hello"}

So it seems like the annotation is ignored, or am I missing something? As far as I can see, you cannot set default annotations like this, for all classes, is that correct?

k-paxian commented 1 year ago

As far as I can see, you cannot set default annotations like this, for all classes, is that correct?

yep, for global context you have SerializationOptions, for local context individual annotations.

I'll check this case and let you know what's wrong with it. Seems legit case for me

k-paxian commented 1 year ago

Well, here is a unit test which is covering this functionality. https://github.com/k-paxian/dart-json-mapper/blob/6d8da09ea7f034483fb00996d5a88fdeb7ee52a6/perf-test/test/unit/test.constructors.dart#L394 and it's green 💚 Highly probably you've not regenerated the code after altering @Json(ignoreNullMembers: true) annotation.

To force full regeneration cycle, you can:

Usually to save on coding performance, code is not fully regenerated while you are typing. The only thing sensible is the presence/absence of @JsonSerializable() / @jsonSerializable annotation

Lars-Sommer commented 1 year ago

I´ve tried what you suggested, and it works. So the problem is that when you add annotation options, the builder doesn´t care about them until the @JsonSerializable() is removed from the class and added again? Anyways, thanks alot!

By the way, it would be nice if you could call initializeJsonMapper() with some default options, like ignoreNullMembers - but that is a detail. Thanks alot for an awesome project, it´s a great help!

k-paxian commented 1 year ago

Thanks for the suggestion, makes sense to me. I'll take it as improvement 😉

Lars-Sommer commented 1 year ago

Cool 🍻😊

k-paxian commented 1 year ago

@fasterlars Since 2.2.6+1 initializeJsonMapper() accepts global serialization/deserialization options. That's actually saved me a lot of redundant code in unit tests well done 👍 through the years I've not noticed such a straight forward move 😄

Lars-Sommer commented 1 year ago

Haha, awesome! 👍