Closed Lars-Sommer closed 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
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:
@JsonSerializable()
annotation, switch your keyboard focus on the console running build runner@JsonSerializable()
annotation back and wait till build runner will succeedsUsually 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
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!
Thanks for the suggestion, makes sense to me. I'll take it as improvement 😉
Cool 🍻😊
@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 😄
Haha, awesome! 👍
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:
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?