Close

Jackson JSON - Applying Custom property inclusion rules using @JsonInclude.Include.CUSTOM

[Updated: Jul 14, 2018, Created: Jul 13, 2018]

Following example shows how to apply customized inclusion rules with @JsonInclude annotation.

If we use @JsonInclude#value=JsonInclude.Include.CUSTOM and specify a filter class via @JsonInclude#valueFilter then the property value will only be serialized if it is not filtered by the filter class. The equals() method of the filter class is used for filtering the values; if it returns 'true' then value is not serialized.

Similarly, if we use @JsonInclude#content=JsonInclude.Include.CUSTOM and specify a filter class via @JsonInclude#contentFilter then the content value of the target property will not be serialized if equals method of the filter class returns true. Check out @JsonInclude#content tutorial to understand what does 'content' mean here.

Example

package com.logicbig.example;

import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Date;
import java.util.Map;

public class Employee {
  private String name;
  @JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = DateOfBirthFilter.class)
  private Date dateOfBirth;
  @JsonInclude(content = JsonInclude.Include.CUSTOM, contentFilter = PhoneFilter.class)
  private Map<String, String> phones;
    .............
}

Filter Classes

public class DateOfBirthFilter {

  @Override
  public boolean equals(Object obj) {
      if (obj == null || !(obj instanceof Date)) {
          return false;
      }
      //date should be in the past
      Date date = (Date) obj;
      return !date.before(new Date());
  }
}
public class PhoneFilter {
  private static Pattern phonePattern = Pattern.compile("\\d{3}-\\d{3}-\\d{4}");

  @Override
  public boolean equals(Object obj) {
      if (obj == null || !(obj instanceof String)) {
          return false;
      }
      //phone must match the regex pattern
      return !phonePattern.matcher(obj.toString()).matches();
  }
}

Main class

public class ExampleMain {
  public static void main(String[] args) throws IOException {
      Employee employee = new Employee();
      employee.setName("Trish");
      employee.setDateOfBirth(Date.from(ZonedDateTime.now().plusDays(1).toInstant()));
      employee.setPhones(Map.of("Cell","111-111-1111", "Work", "(222) 222 2222"));

      ObjectMapper om = new ObjectMapper();
      String jsonString = om.writeValueAsString(employee);
      System.out.println(jsonString);
  }
}
{"name":"Trish","phones":{"Cell":"111-111-1111"}}

Without Custom filters

If we don't use any @JsonInclude annotations:

public class Employee {
  private String name;
  private Date dateOfBirth;
  private Map<String, String> phones;
    .............
}

The output will be:

{"name":"Trish","dateOfBirth":1531544400000,"phones":{"Cell":"111-111-1111","Work":"(222) 222 2222"}}

Example Project

Dependencies and Technologies Used:

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

@JsonInclude.include.CUSTOM Example Select All Download
  • jackson-json-include-custom
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Employee.java

    See Also