JSON-B的循环引用问题 [英] Circular reference issue with JSON-B
问题描述
我有两个互相引用的类.当我尝试将类序列化为Json时,出现循环引用错误.谁能告诉我如何解决这个问题?
I have two classes that reference each other. When I try to serialize the classes to Json I get a circular reference error. Can any one tell me how to correct this?
我有一个包含密码列表的Folder类:
I have a Folder class with a list of passwords:
public class Folder {
private List<Password> items = new ArrayList<>();
private String name;
public Folder(String name) {
this.name = name;
}
public void add(Password password) {
items.add(password);
}
public List<Password> getItems() {
return items;
}
public void setItems(List<Password> items) {
this.items = items;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后我有一个密码类,它引用了Folder类:
Then I have a Password class that references back to the Folder class:
public class Password {
private String name;
private Folder mFolder;
public Password(String name, Folder folder) {
this.name = name;
folder.add(this);
mFolder = folder;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Folder getmFolder() {
return mFolder;
}
public void setmFolder(Folder mFolder) {
this.mFolder = mFolder;
}
}
最后是Main类:
public class Main {
public static void main(String[] args) throws IOException {
Password password = new Password("Password", new Folder("Folder"));
Jsonb jsonb = JsonbBuilder.create();
try (Writer writer = new StringWriter()) {
jsonb.toJson(password, writer);
System.out.println(writer.toString());
}
}
}
得到以下异常:
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:60)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:91)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:76)
at org.eclipse.yasson.internal.serializer.CollectionSerializer.serializeInternal(CollectionSerializer.java:35)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:60)
at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:91)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:92)
at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:59)
推荐答案
在Password
或Folder
中,您需要删除它们的引用(在序列化中).您可以对不想在JSON中序列化的字段使用@JsonbTransient
.
In either Password
or Folder
, you need to remove their reference (in serialisation) to each other. You can use @JsonbTransient
to the field you don't want to be serialised in JSON.
(文档: http://json-b.net/docs/user-guide.html#ignoring-properties )
示例:Password
类方法
@JsonbTransient
private Folder mFolder;
示例:Folder
类方法
@JsonbTransient
private List<Password> items = new ArrayList<>();
现在由您决定哪个班级不会显示对另一个班级的引用.另外,请参见此
It's up to you now which class will not show the reference to another class. Also, see this
这篇关于JSON-B的循环引用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!