gpt4 book ai didi

java - 嵌套对象的 jackson 反序列化,其中一个引用另一个。 Unresolved 前向引用

转载 作者:行者123 更新时间:2023-11-30 10:12:12 24 4
gpt4 key购买 nike

我正在尝试反序列化以下 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/

24 4 0
文章推荐: java - Map.computeIfPresent() 在 Java 中初始化一个列表
文章推荐: Java或Spring代码从nexus下载包
文章推荐: java - 如何在Android中添加向firebase添加数据的进度对话框?
文章推荐: javascript - 选择所有包含 <input> 和
标签的 标签