How to exclude properties from the JSON output with Jackson?
About
This article lists the techniques that may be used with the Jackson libary to filter out some properties of an object when transforming it as a JSON.
Filter in Jackson means that the data are processed not deleted
Techniques
There are 5 techniques that you can apply to filter out properties:
- JsonFilter: apply a transformation that will remove properties
- JsonView: select properties that have been annotated as part of a view
- Mixin: modify the annotation properties on the fly.
- Ignore Annotations (JsonIgnore, …): to ignore fields
- Visibility (private/public or the JsonAutoDetect annotation): to set the visibility of fields
JsonFilter
JsonFilter is an annotation property that permits applying a filter when processing.
By default, the standard given filters permit to exclude properties but you can also just apply a transformation if you create your own filter.
Example:
- You need to apply the filter on the pojo property with the JsonFilter annotation. Otherwise, the filter will have no effect.
Example on a minimal user:
public class User {
private String id;
private String name;
@JsonFilter("private")
@JsonProperty("id")
public String getId() {
return id;
}
@JsonProperty("name")
public String getName() {
return name;
}
}
- Then you can create it and apply the processing
String jsonString = JsonMapper.builder()
.build()
.setFilterProvider(
// Add a filter named private that process all properties (filter) except the property id
new SimpleFilterProvider()
.addFilter("private", SimpleBeanPropertyFilter.filterOutAllExcept("id"))
)
.writeValueAsString(userPojo);
- The output is a JSON user without id
JsonView
JsonView 1) is a property/field annotation that adds a property to a view. When processing, you can then select the view that you want to render.
Example:
- Creation of the public view
public class Views {
public static class Public {
}
}
- Annotation of the Pojo
public class User {
private String id;
private String name;
@JsonProperty("id")
public String getId() {
return id;
}
@JsonView(Views.Public.class)
@JsonProperty("name")
public String getName() {
return name;
}
}
- Processing with the DEFAULT_VIEW_INCLUSION mapper feature disabled 2)
String jsonString = JsonMapper.builder()
// By default, all properties not explicitly annotated with a JsonView
// is part of the view
// The below code disables this behavior
.disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
.build()
.writerWithView(Views.Public.class)
.writeValueAsString(userPojo);
Ignore
This section lists annotations for ignoring properties 3).
JsonIgnore
With the @JsonIgnore annotation 4), the property will be completely ignored
Example: ignore the id field completely.
public class User {
private String id;
private String name;
@JsonIgnore
public String getId() {
return id;
}
@JsonProperty("name")
public String getName() {
return name;
}
}
JsonIgnoreProperties
@JsonIgnoreProperties is a per-class annotation to list properties to ignore, or to indicate that any unknown properties are to be ignored.
- On serialization to ignore a list of properties
@JsonIgnoreProperties({"prop1", "prop2"})
- On deserialization to ignore properties that don't have getter/setters
@JsonIgnoreProperties(ignoreUnknown=true)
JsonIgnoreType
@JsonIgnoreType, a per-class annotation to indicate that all properties of annotated type are to be ignored
Visibility
Visibility (ie Public / Private) lets Jackson see or not the property.
Private/Public
You can manage the visibility with accessors:
- A Getter Makes a Non-Public Field Serializable and Deserializable
- A Setter Makes a Non-Public Field Deserializable Only
JsonAutoDetect
You can configure them with the Visibility enum 5)
- on the mapper level
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
- on the field/class level with @JsonAutoDetect annotation
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
Mixin
A Mixin 6) permits overwriting the JSON annotations on the fly.
Mix-ins are useful for applying Jackson configuration to classes without modifying the source code of the target class.
They can be thought of as a configuration layer that sits above a class that Jackson will look at for instructions during de/serialization.
- Create a Mixin to ignore the user id
abstract class UserMixin {
@JsonIgnore
abstract Long getId();
}
- Apply it
String jsonString = JsonMapper.builder()
.build()
.addMixIn(User.class, UserMixin.class)
.writeValueAsString(userPojo);
