Java Jackson @JsonAppend Example

1. Introduction

In certain cases, it becomes necessary to add extra fields to a serialized JSON without modifying the original POJO structure. The Jackson @JsonAppend annotation provides a solution for this, allowing the dynamic addition of properties to the output JSON. This guide showcases the usage of the @JsonAppend annotation to enrich the JSON serialization process.

2. Example Steps

1. Add the necessary Jackson library dependencies to your project.

2. Define a Java class (POJO) for which you want to append additional properties during serialization.

3. Use the @JsonAppend annotation and define the additional properties.

4. Utilize Jackson's ObjectMapper to serialize the Java object, including the additional properties.

5. Output the serialized JSON.

3. Java Jackson @JsonAppend Example

import com.fasterxml.jackson.annotation.JsonAppend;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonAppend.Prop;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

public class JsonAppendExample {

    public static void main(String[] args) {
        // Create an instance of the Person class
        Person person = new Person("John", 25);

        // Define a filter to include the appended property
        SimpleFilterProvider filters = new SimpleFilterProvider().addFilter("addHeight", SimpleBeanPropertyFilter.serializeAll());

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            // Serialize the object to JSON
            String jsonString = objectMapper.writer(filters).writeValueAsString(person);
            System.out.println(jsonString);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    @JsonAppend(props = {
        @Prop(name = "height", value = "default-height", include = Include.NON_NULL)
    })
    @com.fasterxml.jackson.annotation.JsonFilter("addHeight")
    static class Person {
        private String name;
        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        // Add getters and setters...

        public String getDefaultHeight() {
            return "5.9ft";
        }
    }
}

Output:

{"name":"John","age":25,"height":"5.9ft"}

4. Step By Step Explanation

1. We created a simple Person class with two fields - name and age.

2. We used the @JsonAppend annotation to introduce an additional property, height, to the serialized JSON. The property will fetch its value from the getDefaultHeight() method.

3. Alongside the @JsonAppend annotation, a @JsonFilter is added. This filter, named addHeight, will ensure that the appended property is included during serialization.

4. In the main method, an instance of the Person class is created, and an ObjectMapper is used to serialize this instance into a JSON string.

5. A SimpleFilterProvider defines the filter to include the appended property during serialization.

6. The serialized JSON string contains the appended height property alongside the original properties.


Comments