Java JsonAppend-class And Method Code Example


import com.fasterxml.jackson.annotation.JsonAppend;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonAppend(
    attrs = {
        @JsonAppend.Attr(value = "age", type = int.class),
        @JsonAppend.Attr(value = "gender", type = Gender.class)
    }
)
public class Person {
    @JsonProperty("name")
    private String name;
    @JsonProperty("address")
    private String address;

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

public enum Gender {
    MALE,
    FEMALE
}

This is an example of how to use the com.fasterxml.jackson.annotation.JsonAppend annotation in Jackson. The @JsonAppend annotation is used to specify additional properties that should be included in the JSON representation of the class. The attrs parameter is used to specify the properties to be included, in this case age and gender with their respective data types.

In this example, the Person class has two properties, name and address, and the @JsonAppend annotation is used to include the properties age and gender as additional properties in the JSON representation of the class, when serialized.

It's important to note that, by default, the properties that are included with @JsonAppend are appended at the end of the JSON representation, if you want to include them in a specific position, you have to use @JsonAppend(props) where props is an array of JsonAppend.Prop.