I use Jackson 2.2.3 to serialize POJOs to JSON. Then I had the problem, that I couldn't serialize recursive structures...I solved this problem by using @JsonIdentityInfo => works great.
But, I don't want this annotation on the top of my POJO.
So my question is: Is there any other possibility to set the default behavior of my ObjectMapper to use the feature for every POJO? 
So I want to transform this annotation code
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
to something like
ObjectMapper om = new ObjectMapper();
om.setDefaultIdentityInfo(ObjectIdGenerators.IntSequenceGenerator.class, "@id");
Any ideas?
The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate the object identity during the serialization and deserialization process.
@JsonIdentityInfo is used to handle circular reference of an object by serializing the back-reference's identifier rather than serializing the complete reference. @JsonIdentityInfo allows to serialize a POJO by id when it is encountered second time during serialization.
@JsonPOJOBuilder The @JsonPOJOBuilder annotation is used to configure a builder class to customize deserialization of a JSON document to recover POJOs when the naming convention is different from the default. The names of the bean's properties are different from those of the fields in JSON string.
@JsonIgnore is used at field level to mark a property or list of properties to be ignored.
You can achieve that using the Jackson mix-in annotations or the Jackson annotation introspector.
Here is an example showing both methods:
public class JacksonJsonIdentityInfo {
    @JsonIdentityInfo(
            generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
    static class Bean {
        public final String field;
        public Bean(final String field) {this.field = field;}
    }
    static class Bean2 {
        public final String field2;
        public Bean2(final String field2) {this.field2 = field2;}
    }
    @JsonIdentityInfo(
            generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id2")
    static interface Bean2MixIn {
    }
    static class Bean3 {
        public final String field3;
        public Bean3(final String field3) {this.field3 = field3;}
    }
    static class MyJacksonAnnotationIntrospector extends JacksonAnnotationIntrospector {
        @Override
        public ObjectIdInfo findObjectIdInfo(final Annotated ann) {
            if (ann.getRawType() == Bean3.class) {
                return new ObjectIdInfo(
                        PropertyName.construct("@id3", null),
                        null,
                        ObjectIdGenerators.IntSequenceGenerator.class,
                        null);
            }
            return super.findObjectIdInfo(ann);
        }
    }
    public static void main(String[] args) throws JsonProcessingException {
        final Bean bean = new Bean("value");
        final Bean2 bean2 = new Bean2("value2");
        final Bean3 bean3 = new Bean3("value3");
        final ObjectMapper mapper = new ObjectMapper();
        mapper.addMixInAnnotations(Bean2.class, Bean2MixIn.class);
        mapper.setAnnotationIntrospector(new MyJacksonAnnotationIntrospector());
        System.out.println(mapper.writeValueAsString(bean));
        System.out.println(mapper.writeValueAsString(bean2));
        System.out.println(mapper.writeValueAsString(bean3));
    }    
}
Output:
{"@id":1,"field":"value"}
{"@id2":1,"field2":"value2"}
{"@id3":1,"field3":"value3"}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With