Reputation: 161
i define this class
public class PostServerActionsRequest {
private ChangePassword changePassword;
private Reboot reboot;
private Rebuild rebuild;
private Resize resize;
private String confirmResize;
private String revertResize;
private CreateImage createImage;
//constructor + getter & setter
}
that i used to parse multiple json requests using jackson json processor. The situation is the following: i receive a json with this structure:
//Json A
{
"reboot": {
"type": "SOFT"
}
}
where i define class Reboot with a private String attribute whose name is type. Obviously the attributes that haven't the relative equivalent in json are set to null by jackson during deserialization. So serializing i obtain this json:
//Json B
{
"changePassword": null,
"reboot": {
"type": "SOFT"
},
"rebuild": null,
"resize": null,
"confirmResize": null,
"revertResize": null,
"createImage": null
}
Now i know how tell jackson to ignore null or empty values during serialization, for example with the annotation @JsonSerialize(include=JsonSerialize.Inclusion.NON_DEFAULT) above the class PostServerActionsRequest, but my question is, it's possible to tell jackson to don't set to null values that are not in json request (json A) during deserialization? This because i want to obtain a class has only the values present in the json request. I hope i was clear to explain my issue and thanks you in advance for your help.
Upvotes: 0
Views: 117
Reputation: 719446
it's possible to tell jackson to don't set to null values that are not in json request (json A) during deserialization?
No, it is not possible.
Indeed, what you are asking doesn't make any sense. When you create a PostServerActionsRequest
, by deserializing from JSON, or by any other means, each of the fields in the PostServerActionsRequest
instance has to have a value.
It is simply meaningless to talk about a field of a Java object as having "no value" ... in the sense you are talking about. For fields that are reference typed, the value has to be either a reference to some object ... or null
. There are simply no alternatives.
Upvotes: 2