- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试反序列化以下 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 注释也是如此。
我是不是遗漏了一些注释?我认为编写一个单独的反序列化器对于这个简单的部分来说有点太多了。
最佳答案
将您的 @JsonIdentityInfo
注释放在 Node
类上,而不是 Force
中的字段:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "number")
public static class Node { //...
关于java - 嵌套对象的 jackson 反序列化,其中一个引用另一个。 Unresolved 前向引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51995766/
我是一名优秀的程序员,十分优秀!