Jackson feature: prevent serialization of nulls, default values
As of Jackson 1.1, you can suppress serialization of properties that have either:
- Null value, or
- Default value for the bean (which may be null, or any other value after being constructed using the default constructor)
These is achieved by using one of 2 mechanisms:
Call ObjectMapper.getSerializationConfig().setSerializationInclusion() to define global setting (value enumeration JsonSerialize.Inclusion, with values ALWAYS, NON_NULL, NON_DEFAULT).
Annotate properties (i.e. serializable field or getter method that implies a property) with @JsonSerialize(include=VALUE), where value is as above.
So either:
ObjectMapper mapper = new ObjectMapper(); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); // no more null-valued properties
or
@JsonSerialize(include=JsonSerialize.Inclusion.NON_DEFAULT)
public class MyBean {
// ... only serialize properties with values other than what they default to
}
Back to JacksonFeatures11
