反序列化时使用父对象的属性确定子类吗? [英] Use property of parent object to determine subclass when deserializing?

查看:84
本文介绍了反序列化时使用父对象的属性确定子类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

children: [
      {
          o kind: "t3"
            data: {                 // ExampleNodeT3 class should be used for kind == t3
                + t3var1: "val1"
                + t3var2: true
            }
      }
      {
          o kind: "t4"                
            data: {                 // ExampleNodeT4 class should be used for kind == t4
                + t4var1: false
                + t4var2: 2346
            }
      }
] ... etc.


@JsonTypeInfo(use=Id.NAME, property="kind")
@JsonSubTypes({
@Type(value=ExampleNodeT3.class, name="t3"),
@Type(value=ExampleNodeT4.class, name="t4")})
public abstract class ExampleNode {
...
public void setData(ExampleNode data) {
    this.data = data;
}

当尝试用Jackson进行反序列化时,创建ExampleNode数据时JsonTypeInfo提示失败,因为"kind"属性与其父级关联并且不可见.我尝试了各种工厂方法和Jackson注释的变体,但是由于Jackson创建了ExampleNode对象并将其传递给setData()本身,所以我看不到有什么地方可以控制创建什么类型的对象.

When attempting to deserialize this with Jackson, the JsonTypeInfo hints fail when ExampleNode data is created because the "kind" property is associated with its parent and not visible. I have tried various variations of factory methods, and Jackson annotations, but because Jackson creates the ExampleNode object and passes it to setData() itself, I see no place to control what class of object is created.

推荐答案

尚不支持此功能.

功能增强请求: http://jira.codehaus.org/browse/JACKSON-275

有关该问题的更多详细信息: http://jackson-users.ning.com/forum/topics/how-to-use-a-property-of?commentId=5286555%3AComment%3A2679

More details on the problem: http://jackson-users.ning.com/forum/topics/how-to-use-a-property-of?commentId=5286555%3AComment%3A2679

这篇关于反序列化时使用父对象的属性确定子类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆