Java Jackson @JsonFilter Example

1. Introduction

One of Jackson's strengths is its flexibility in serializing and deserializing JSON data. Occasionally, you might want to conditionally filter out certain fields during the serialization process based on various criteria. The @JsonFilter annotation in Jackson provides a mechanism to dynamically decide which properties of an object should be serialized.

2. Example Steps

1. Define a class Product with several attributes.

2. Annotate the Product class with @JsonFilter and provide a filter name.

3. Set up custom filter criteria using Jackson's SimpleBeanPropertyFilter and FilterProvider.

4. Serialize the Product instance, applying the custom filter.

5. Print the serialized JSON string.

3. Java Jackson @JsonFilter Example

import com.fasterxml.jackson.core.JsonProcessingException;
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;
import com.fasterxml.jackson.annotation.JsonFilter;

public class JsonFilterExample {

    @JsonFilter("productFilter")
    public static class Product {
        public int id;
        public String name;
        public double price;
        public String manufacturer;

        public Product(int id, String name, double price, String manufacturer) {
            this.id = id;
            this.name = name;
            this.price = price;
            this.manufacturer = manufacturer;
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        Product product = new Product(101, "Laptop", 1500.00, "TechCorp");

        // Define filter criteria - Exclude 'price' property
        SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.serializeAllExcept("price");
        FilterProvider filters = new SimpleFilterProvider().addFilter("productFilter", filter);

        ObjectMapper mapper = new ObjectMapper();
        String result = mapper.writer(filters).writeValueAsString(product);

        System.out.println(result);
    }
}

Output:

{"id":101,"name":"Laptop","manufacturer":"TechCorp"}

4. Step By Step Explanation

The @JsonFilter annotation allows you to associate a filter name with a class, in this case, productFilter for the Product class. This name is then used to define the actual filtering criteria.

In our example, we've used SimpleBeanPropertyFilter.serializeAllExcept to indicate that all properties should be serialized, except for the "price". This is accomplished by defining a filter that serializes everything but omits the price.

The SimpleFilterProvider is then used to associate the defined filter criteria with the filter name (productFilter).

When we serialize our Product object using an ObjectMapper, we pass the filter criteria. This results in the exclusion of the price field in the output.

This way, Jackson provides a powerful mechanism to conditionally serialize object properties based on dynamic filtering criteria.


Comments