
Jackson - Serialization Annotations
- Jackson - @JsonAnyGetter
- Jackson - @JsonGetter
- Jackson - @JsonPropertyOrder
- Jackson - @JsonRawValue
- Jackson - @JsonValue
- Jackson - @JsonRootName
- Jackson - @JsonSerialize
Jackson - Deserialization Annotations
- Jackson - @JsonCreator
- Jackson - @JacksonInject
- Jackson - @JsonAnySetter
- Jackson - @JsonSetter
- Jackson - @JsonDeserialize
- Jackson - @JsonEnumDefaultValue
Jackson - Property Inclusion Annotations
- Jackson - @JsonIgnoreProperties
- Jackson - @JsonIgnore
- Jackson - @JsonIgnoreType
- Jackson - @JsonInclude
- Jackson - @JsonAutoDetect
Jackson - Type Handling Annotations
Jackson - General Annotations
- Jackson - @JsonProperty
- Jackson - @JsonFormat
- Jackson - @JsonUnwrapped
- Jackson - @JsonView
- Jackson - @JsonManagedReference
- Jackson - @JsonBackReference
- Jackson - @JsonIdentityInfo
- Jackson - @JsonFilter
Jackson - Miscellaneous
Jackson - Resources
Jackson Annotations - @JsonFilter
Overview
@JsonIdentityInfo is used to apply filter during serialization/de-serialization like which properties are to be used or not.
Usage of @JsonFilter
Annotate Class with @JsonFilter
@JsonFilter("nameFilter") class Student { public int id; ... }
Create a Filter
FilterProvider filters = new SimpleFilterProvider() .addFilter( "nameFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"));
Apply the Filter
String jsonString = mapper.writer(filters) .withDefaultPrettyPrinter() .writeValueAsString(student);
Example - Serialization with @JsonFilter
JacksonTester.java
package com.tutorialspoint; import java.io.IOException; import java.text.ParseException; import com.fasterxml.jackson.annotation.JsonFilter; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ser.FilterProvider; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; public class JacksonTester { public static void main(String args[]) throws IOException, ParseException { ObjectMapper mapper = new ObjectMapper(); Student student = new Student(1,13, "Mark"); FilterProvider filters = new SimpleFilterProvider() .addFilter( "nameFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name")); String jsonString = mapper.writer(filters) .withDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } } @JsonFilter("nameFilter") class Student { public int id; public int rollNo; public String name; Student(int id, int rollNo, String name) { this.id = id; this.rollNo = rollNo; this.name = name; } }
Output
Run the JacksonTester and verify the output −
{ "name" : "Mark" }
Here we can see, using JsonFilter we're able to filter object properties.
Advertisements