【问题标题】:How to deserialize JSON Array contained an abstract class without modifying a parent class?如何在不修改父类的情况下反序列化包含抽象类的 JSON 数组?
【发布时间】:2014-05-09 21:27:03
【问题描述】:

我正在尝试使用 Jackson 将持久化到我的 MongoDB 中的 JSON 数组反序列化为 Java 对象。我发现很多教程提到了通过添加来处理这种多态性:

@JsonTypeInfo(use=Id.CLASS,property="_class")

Super-class。但是,就我而言,我无法修改Super-class。那么,是否有一些解决方案可以在不修改Super-class 的情况下解决它?这是我的代码:

public class User {

    @JsonProperty("_id")
    private String id;
    private List<Identity> identities; // <-- My List contains objects of an abstract class; Identity
    public User(){
        identities = new ArrayList<Identity>();
    }
    public static Iterable<User> findAllUsers(){
        return users().find().as(User.class); // Always give me the errors
    }
    /*More code*/
}

它总是给我错误 - Can not construct instance of securesocial.core.Identity, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information

【问题讨论】:

    标签: java json mongodb jackson jongo


    【解决方案1】:

    您可以使用@JsonDeserilize annotation 将具体实现类绑定到抽象类。如果你不能修改你的抽象类,你可以使用the Jackson Mix-in annotations告诉Jackson如何找到实现类。

    这是一个例子:

    public class JacksonAbstract {
        public static class User {
            private final String id;
            private final List<Identity> identities;
    
            @JsonCreator
            public User(@JsonProperty("_id") String id, @JsonProperty("identities") List<Identity> identities) {
                this.id = id;
                this.identities = identities;
            }
    
            @JsonProperty("_id")
            public String getId() {
                return id;
            }
    
            public List<Identity> getIdentities() {
                return identities;
            }
        }
    
        public static abstract class Identity {
            public abstract String getField();
        }
    
        @JsonDeserialize(as = IdentityImpl.class)
        public static abstract class IdentityMixIn {
        }
    
        public static class IdentityImpl extends Identity {
            private final String field;
    
            public IdentityImpl(@JsonProperty("field") String field) {
                this.field = field;
            }
    
            @Override
            public String getField() {
                return field;
            }
        }
    
        public static void main(String[] args) throws IOException {
            User u = new User("myId", Collections.<Identity>singletonList(new IdentityImpl("myField")));
            ObjectMapper mapper = new ObjectMapper();
            mapper.addMixInAnnotations(Identity.class, IdentityMixIn.class);
            String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(u);
            System.out.println(json);
            System.out.println(mapper.readValue(json, User.class));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      相关资源
      最近更新 更多