Exclude empty Arrays from Jackson ObjectMapper
You should use:
config.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);
for Jackson 1 or
config.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
for Jackson 2
If you can modify the object to be serialized, you can also place an annotation directly on the field, for example (Jackson 2.11.2):
@JsonProperty
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Set<String> mySet = new HashSet<>();
In this way, no further configuration of the ObjectMapper
is required.