newmanne
newmanne

Reputation: 2089

Jackson use JsonSerialize.Inclusion.NON_NULL except for one class

I normally use objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL) because I never want the null values of my classes serialized. Except now I have a specific field should be written out, even if it is null. Is there a quick annotation I can put on this one field that overrides the Inclusion.NON_NULL property for that one field? What's a good way to achieve this?

Upvotes: 26

Views: 52387

Answers (3)

Pascal Gélinas
Pascal Gélinas

Reputation: 2824

With Jackson 1.x you can use @JsonSerialize(include = Inclusion.ALWAYS) and with Jackson 2.x you can use @JsonInclude(Include.ALWAYS). These annotations will override the default config from your ObjectMapper.

Upvotes: 37

Marcus
Marcus

Reputation: 2238

@user1433372, JsonInclude is an annotation only for Jackson 2.x.

in Jackson 1.9

@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)

is the same in Jackson 2.x as

@JsonInclude(JsonInclude.Include.NON_EMPTY)

Upvotes: 20

userM1433372
userM1433372

Reputation: 5497

With Jackson 1.9 is used @JsonSerialize(include= JsonSerialize.Inclusion.ALWAYS)

@JsonSerialize(include=Include.ALWAYS) did not compile.

Upvotes: 3

Related Questions