Using @JsonIdentityInfo without annotations

I am using Jackson 2.2.3 to serialize POJO for JSON. Then I had a problem that I could not serialize recursive structures ... I solved this problem using @JsonIdentityInfo => works fine.

But I do not want this annotation at the top of my POJO.

So my question is: is there any other way to set the default behavior of my ObjectMapper to use this function for each POJO?

So I want to convert 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?

+3
java json jackson serialization
Dec 05 '14 at 13:00
source share
2 answers

You can achieve this using Jackson mix annotations or 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"} 
+4
Dec 05 '14 at 20:38
source share

After a few months and a lot of research, I implemented my own solution to keep my domain without Jackson dependencies.

 public class Parent { private Child child; public Child getChild(){return child;} public void setChild(Child child){this.child=child;} } public class Child { private Parent parent; public Child getParent(){return parent;} public void setParent(Parent parent){this.parent=parent;} } 

First, you must declare each of your entities a bi-directional relationship:

 public interface BidirectionalDefinition { @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=Parent.class) public interface ParentDef{}; @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=Child.class) public interface ChildDef{}; } 

After that, you can configure the display configuration of the object:

 ObjectMapper om = new ObjectMapper(); Class<?>[] definitions = BidirectionalDefinition.class.getDeclaredClasses(); for (Class<?> definition : definitions) { om.addMixInAnnotations(definition.getAnnotation(JsonIdentityInfo.class).scope(), definition); } 
0
Oct 30 '15 at 4:17
source share



All Articles