Close

Jackson JSON - Filtering out specified properties using @JsonFilter Annotation

[Updated: Oct 23, 2018, Created: Oct 23, 2018]

@JsonFilter annotation can be used to filter certain properties of a POJO during serialization.

This annotation specifies only the filter name. For example

@JsonFilter("myFilterId")
public class Person{
 private String name;
 private int age;
 private String address;
 .....
}

Then we can specify the properties name of the object at the time of serialization using FilterProvider (implementation SimpleFilterProvider) and SimpleBeanPropertyFilter:

 SimpleFilterProvider filterProvider = new SimpleFilterProvider();
 //only 'name' and 'age' will be serialized
 filterProvider.addFilter("empFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name", "age"));
 ObjectMapper om = new ObjectMapper();
 om.setFilterProvider(filterProvider);
...
 //now serialize some Person instances

SimpleBeanPropertyFilter useful methods:

package com.fasterxml.jackson.databind.ser.impl;
 ....
public class SimpleBeanPropertyFilter implements BeanPropertyFilter, PropertyFilter{
   
    //serializes all properties
    public static SimpleBeanPropertyFilter serializeAll() {
         ....
    }

    //filters out all properties except the ones specified in the set
    public static SimpleBeanPropertyFilter filterOutAllExcept(Set<String> properties) {
         ....
    }

    public static SimpleBeanPropertyFilter filterOutAllExcept(String... propertyArray) {
          ....
    }

    public static SimpleBeanPropertyFilter serializeAllExcept(Set<String> properties) {
          ....
    }

    public static SimpleBeanPropertyFilter serializeAllExcept(String... propertyArray) {
          ....
    }

  ..........  
}

@JsonFilter can also be used on property level. Let's see some examples.

Examples

Using @JsonFilter on class

package com.logicbig.example;

import com.fasterxml.jackson.annotation.JsonFilter;

@JsonFilter("empFilter")
public class Employee {
  private String name;
  private String dept;
  private int salary;
  private String phone;
    .............
}

Serializing

public class ExampleMain {
  public static void main(String[] args) throws IOException {

      //specifying fields to be filtered
      SimpleFilterProvider filterProvider = new SimpleFilterProvider();
      filterProvider.addFilter("empFilter",
              SimpleBeanPropertyFilter.filterOutAllExcept("name", "phone"));

      ObjectMapper om = new ObjectMapper();
      om.setFilterProvider(filterProvider);

      Employee e = Employee.of("Jake", "Admin",3000, "111-111-111");
      System.out.println(e);
      System.out.println("-- serializing --");
      String s = om.writeValueAsString(e);
      System.out.println(s);
  }
}
Employee{name='Jake', dept='Admin', salary=3000, phone='111-111-111'}
-- serializing --
{"name":"Jake","phone":"111-111-111"}

Using @JsonFilter on properties

package com.logicbig.example;

import com.fasterxml.jackson.annotation.JsonFilter;

public class Person {
  private String name;
  @JsonFilter("PersonAddressFilter")
  private Address address;
    .............
}
package com.logicbig.example;

public class Address {
  private String street;
  private String city;
  private int zipCode;
    .............
}

Serializing

public class ExampleMain2 {
  public static void main(String[] args) throws IOException {
      //specifying fields to be filtered
      SimpleFilterProvider filterProvider = new SimpleFilterProvider();
      filterProvider.addFilter("PersonAddressFilter",
              SimpleBeanPropertyFilter.filterOutAllExcept("zipCode"));

      ObjectMapper om = new ObjectMapper();
      om.setFilterProvider(filterProvider);

      Person p = Person.of("Jack",
              Address.of("101 Green Pile Dr", "Smoke Valley", 12323));
      System.out.println(p);
      System.out.println("-- serializing --");
      String s = om.writeValueAsString(p);
      System.out.println(s);
  }
}
Person{name='Jack', address=Address{street='101 Green Pile Dr', city='Smoke Valley', zipCode=12323}}
-- serializing --
{"name":"Jack","address":{"zipCode":12323}}

Example Project

Dependencies and Technologies Used:

  • jackson-databind 2.9.7: General data-binding functionality for Jackson: works on core streaming API.
  • JDK 11
  • Maven 3.5.4

Jackson -@JsonFilter Example Select All Download
  • jackson-json-filter-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Employee.java

    See Also