Jackson对嵌套对象进行反序列化,其中一个引用另一个。未解决的ForwardReference

滴水:

我正在尝试反序列化以下JSON结构:

{
  "node" : {
    "number" : 5,
    "x" : 2000.0,
    "y" : 1500.0
  },
  "force" : {
    "number" : 1,
    "value" : -20.0,
    "angle" : 90.0,
    "node" : 5
  }
}

放入以下对象:

public class NodeWithForce {

    private Node node;
    private Force force;

    public NodeWithForce(){}

    public NodeWithForce(Node node, Force force) {
        this.node = node;
        this.force = force;
    }

    //getters and setters for Node and Force, Equals and Hashcode
}

节点类:

public class Node {

    private long number;
    private double x;
    private double y;

    public Node() {}

    public Node(long number, double x, double y) {
        this.number = number;
        this.x = x;
        this.y = y;
    }

    //getters and setters for number, x and y, equals and hashcode
    }
}

力类:

public class Force {

    private long number;
    private double value;
    private double angle;

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "number")
    @JsonIdentityReference(alwaysAsId = true)
    private Node node;

    public Force() {}

    public Force(long number, double value, double angle, Node node) {
        this.number = number;
        this.value = value;
        this.angle = angle;
        this.node = node;
    }

    //getters and setters for number, value, angle and node. Equals and Hashcode.
}

因此,Node是包装对象NodeWithForce的一部分,也是包装子之一:Force。实际的数据结构比这要复杂得多,但是我尝试在SO上降低复杂性并仅保留对这个问题重要的部分。

然后当我运行这个:

public NodeWithForce getNodeWithForce() throws Exception {
    Node node = new Node(5, 2000, 1500);
    Force force = new Force(1, -20, 90, node);
    NodeWithForce nwf = new NodeWithForce(node,force);
    NodeWithForce nwf2 = null;
    ObjectMapper om = context.getBean(ObjectMapper.class);
    om.enable(SerializationFeature.INDENT_OUTPUT);
    String jsonRepresentation = null;
    jsonRepresentation = om.writeValueAsString(nwf);
    System.out.println(jsonRepresentation);
    nwf2 = om.readValue(jsonRepresentation, NodeWithForce.class);
    System.out.println("equal: " + nwf.equals(nwf2));
    return nwf2;
}

它给了我以下例外:

com.fasterxml.jackson.databind.deser.UnresolvedForwardReference: Unresolved forward references for: 
 at [Source: (String)"{
  "node" : {
    "number" : 5,
    "x" : 2000.0,
    "y" : 1500.0
  },
  "force" : {
    "number" : 1,
    "value" : -20.0,
    "angle" : 90.0,
    "node" : 5
  }
}"; line: 13, column: 1]Object id [5] (for `com.company.calculationmanager.pojos.Node`) at [Source: (String)"{
  "node" : {
    "number" : 5,
    "x" : 2000.0,
    "y" : 1500.0
  },
  "force" : {
    "number" : 1,
    "value" : -20.0,
    "angle" : 90.0,
    "node" : 5
  }
}"; line: 11, column: 15].
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.checkUnresolvedObjectId(DefaultDeserializationContext.java:165) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4015) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004) ~[jackson-databind-2.9.6.jar:2.9.6]

序列化之所以有效,是因为它将对象作为json打印到控制台,但是即使我添加了JsonIdentityInfo批注,反序列化也不行。

我在这里缺少一些注释吗?我认为编写一个单独的反序列化器对于此简单文章来说有点过多。

teppic:

@JsonIdentityInfo注释放在Node类上,而不是在字段上Force

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "number")
public static class Node { //...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Gson反序列化另一个对象内的对象数组

反序列化包含另一个对象的json对象

在 Jackson 中反序列化 JSON,其中 key 是一个值

如何反序列化嵌套在另一个标签的文本部分中的标签?

Gson-xml 在另一个列表<对象> 中反序列化列表<对象>

根据另一个序列化查询的结果进行序列化查询

不反序列化一个对象的实例

Hazelcast在另一个类别中反序列化

基于另一个字段的Jackson字段序列化

如何在不反序列化第一个protobuf的情况下将序列化protobuf与另一个protobuf合并

如何 XML 序列化来自另一个类的列表?(解决了)

Java将序列化的对象转换为另一个对象

用json序列化另一个对象中的Python对象?

反序列化Custom对象的序列化ArrayList,添加一个对象,然后重新序列化

将两个对象(其中一个保留对另一个对象的引用)传递到线程中

当类继承另一个类的列表时进行序列化

Catel:反序列化适用于 1 个单元测试,另一个失败

C#Xml序列化:无法(反)序列化来自另一个程序集的对象

反序列化XML,其中根对象是一个数组,仅返回单个元素

在GSON中反序列化对象的JSON集合(可能与另一个集合一起)

如果将对象移动到另一个程序包或重命名了,该如何反序列化该对象?

在F#中使用区分的联合反序列化数据的另一个失败

如何使用GSON或Java中的另一个JSON库反序列化列表?

就是在这样一个构建反序列化嵌套的对象类型提示属性?

向量化嵌套循环,其中一个循环变量依赖于另一个

如何构建其中一个字段引用另一个字段的结构

反序列化一个简单的 JSON 数组

Gson反序列化跳过一个引号

当其中一个从另一个定义返回时如何使用## 进行连接