使用没有注释的@JsonIdentityInfo

时间:2014-12-05 13:00:42

标签: java json serialization jackson

我使用Jackson 2.2.3将POJO序列化为JSON。然后我遇到了问题,我无法序列化递归结构......我通过使用@JsonIdentityInfo =>解决了这个问题。效果很好。

但是,我不希望这个注释位于我的POJO之上。

所以我的问题是:是否还有其他可能设置ObjectMapper的默认行为以便为每个POJO使用该功能?

所以我想转换这个注释代码

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")

类似

ObjectMapper om = new ObjectMapper();
om.setDefaultIdentityInfo(ObjectIdGenerators.IntSequenceGenerator.class, "@id");

有什么想法吗?

2 个答案:

答案 0 :(得分:5)

您可以使用the Jackson mix-in annotationsthe Jackson annotation introspector来实现这一目标。

以下示例显示了这两种方法:

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));
    }    
}

输出:

{"@id":1,"field":"value"}
{"@id2":1,"field2":"value2"}
{"@id3":1,"field3":"value3"}

答案 1 :(得分:0)

经过几个月的研究和研究,我已经实施了自己的解决方案,以保证我的域名不受杰克逊依赖。

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;}
}

首先,您必须声明双向关系的每个实体:

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{};

}

之后,可以自动配置对象映射器:

ObjectMapper om = new ObjectMapper();
Class<?>[] definitions = BidirectionalDefinition.class.getDeclaredClasses();
for (Class<?> definition : definitions) {
    om.addMixInAnnotations(definition.getAnnotation(JsonIdentityInfo.class).scope(), definition);
}