Advicer
Advicer

Reputation: 1330

Jackson serialize NULL property value if Include.NON_NULL is set at class level

I have a model class like this:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Defect

and I need JsonInclude.Include.NON_NULL to ignore the null values. But I have a property that needs to be null sometimes.

@JsonProperty("blocked")
private String blocked;

Is there a way I can dynamically (at run-time) set this value to be included or not?

Upvotes: 4

Views: 5371

Answers (1)

heenenee
heenenee

Reputation: 20125

You should be able to override the class-level @JsonInclude with a field-level @JsonInclude, as follows:

@JsonInclude(JsonInclude.Include.ALWAYS)
@JsonProperty("blocked")
private String blocked;

Upvotes: 12

Related Questions