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)进行应对序列化时,创建审查酶数据时的jsontypeinfo提示失败了,因为“善良”属性与其母体关联而不是可见。我尝试了工厂方法和杰克逊注释的各种变体,但是由于杰克逊创建了extplenode对象并将其传递给setData()本身,所以我没有看到控制哪些对象类别的地方。

有帮助吗?

解决方案 2

其他提示

从Google到达这里,找到了解决方案。实际上,如今,由于包括= jsontypeinfo.as.as.external_property,示例:

 public class Parent {

    @JsonProperty("type")
    public String type;

    @JsonProperty("data")
    @JsonInclude(Include.NON_NULL)
    public ChildBase ChildBase;

    public Parent() {
        medias = new HashMap<>();
    }

    @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.EXTERNAL_PROPERTY, property="type")
    @JsonTypeIdResolver(ChildBaseByParentTypeResolver.class)
    public void setChildBase(ChildBase ChildBase){
        this.ChildBase = ChildBase;
    }
}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ChildBase {
      public String someStr;

}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class AggressiveChild extends ChildBase{
     public String someStr1;

}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ChilledChild extends ChildBase{
     public String someStr1;

}


public class ChildBaseByParentTypeResolver extends TypeIdResolverBase {

    private JavaType superType;

    @Override
    public void init(JavaType baseType) {
        superType = baseType;
    }   

    @Override
    public Id getMechanism() {
        return Id.NAME;
    }

    @Override
    public JavaType typeFromId(DatabindContext context, String id) {
        Class<?> subType = ChildBase.class;     
        switch (id) {
        case "agressiveParent":        
            subType = AggressiveChild.class;
            break;
        case "chilledParent":        
            subType = ChilledChild.class;
            break;        
        }
        return context.constructSpecializedType(superType, subType);        
    }

    @Override
    public JavaType typeFromId(String directiveType) {
         throw new NotImplementedException();
    }

}

有趣的文章:

与杰克逊朋友的多态性

相关堆栈溢出问题

正确 - 使用“外部类型标识符”(不包含在对象内的估算中的标识符,但作为兄弟姐妹)尚未支持。

可以实现这一点(如在其中,没有什么基本的阻止支持的添加) - 除了提到的JIRA问题外,可以提供新的功能请求,以直接要求使用这种类型标识符的能力。实际上,多个用户提到了这一点。可能是因为有使用这种打字结构的数据格式(Geojson?)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top