gpt4 book ai didi

java - Gson : StackOverflowError

转载 作者:行者123 更新时间:2023-11-30 06:20:59 25 4
gpt4 key购买 nike

以下代码导致 StackOverflowError。代码的目的是从 java 类创建一个 json 字符串。

for (ControlTransaction crt : ctrList) {
crt= new ControlTransaction();// for test, Still issue
final Gson gson = new GsonBuilder().registerTypeAdapter(
ControlTransaction.class,
new ControlTransactionSerializer()).create();
String jsonControlTransactionString = gson.toJson(crt);
strList.add(jsonControlTransactionString);

}

我的类(class)看起来像

public class ControlTransaction implements IsSerializable, Serializable ,IsBean{
private long id;
private String value; // H
private String lastValue; // H
private FormTransaction formTransaction;
private List<FormTransaction> gridRows;
private ControlTransaction referenceGridTransaction;
private RowTransaction fkRowTransaction;
private ReportTransaction reportTransaction;
//getters ... setters
}

堆栈跟踪是这样的,让我知道

Caused by: java.lang.StackOverflowError
at com.google.gson.stream.JsonWriter.<init>(JsonWriter.java:190)
at com.google.gson.internal.bind.JsonTreeWriter.<init>(JsonTreeWriter.java:58)
at com.google.gson.Gson.toJsonTree(Gson.java:478)
at com.google.gson.Gson$3.serialize(Gson.java:140)
at com.nextenders.common.ControlTransactionSerializer.serialize(ControlTransactionJsonUtil.java:129)
at com.nextenders.common.ControlTransactionSerializer.serialize(ControlTransactionJsonUtil.java:1)
at com.google.gson.TreeTypeAdapter.write(TreeTypeAdapter.java:70)
at com.google.gson.Gson.toJson(Gson.java:586)
at com.google.gson.Gson.toJsonTree(Gson.java:479)
at com.google.gson.Gson$3.serialize(Gson.java:140)
at com.nextenders.common.ControlTransactionSerializer.serialize(ControlTransactionJsonUtil.java:129)
at com.nextenders.common.ControlTransactionSerializer.serialize(ControlTransactionJsonUtil.java:1)
at com.google.gson.TreeTypeAdapter.write(TreeTypeAdapter.java:70)
at com.google.gson.Gson.toJson(Gson.java:586)
at com.google.gson.Gson.toJsonTree(Gson.java:479)
at com.google.gson.Gson$3.serialize(Gson.java:140)
at com.nextenders.common.ControlTransactionSerializer.serialize(ControlTransactionJsonUtil.java:129)
at com.nextenders.common.ControlTransactionSerializer.serialize(ControlTransactionJsonUtil.java:1)
at com.google.gson.TreeTypeAdapter.write(TreeTypeAdapter.java:70)
at com.google.gson.Gson.toJson(Gson.java:586)
at com.google.gson.Gson.toJsonTree(Gson.java:479)
at com.google.gson.Gson$3.serialize(Gson.java:140)

这是我的序列化器反序列化器

class ControlTransactionDeserializer implements
JsonDeserializer<ControlTransaction> {
@Override
public ControlTransaction deserialize(JsonElement json, Type type,
JsonDeserializationContext context) throws JsonParseException {
return context.deserialize(json, type);
}
}

class ControlTransactionSerializer implements
JsonSerializer<ControlTransaction> {

@Override
public JsonElement serialize(ControlTransaction ctr, Type type,
JsonSerializationContext context) {
return context.serialize(ctr, type);
}
}

后端发生了什么。我只是传递了一个空对象,这意味着新的 ControlTransaction(),仍然无法解析。

最佳答案

JsonSerializer 的要点是序列化对象的字段,而不是对象本身。但是,您正在传递您已经告诉 Gson 进行序列化的对象。以下

@Override
public JsonElement serialize(ControlTransaction ctr, Type type,
JsonSerializationContext context) {
return context.serialize(ctr, type);
}

相当于

@Override
public JsonElement serialize(ControlTransaction ctr, Type type,
JsonSerializationContext context) {
return new GsonBuilder().registerTypeAdapter(
ControlTransaction.class,
new ControlTransactionSerializer()).create().toJsonTree(ctr);
}

我希望你能看到它正在进入递归循环。

JsonSerializationContext 基本上是 Gson 对象用来序列化您的对象的底层结构。当它看到您的类型 ControlTransaction 时,它将委托(delegate)给您的自定义 JsonSerializer。但是您的 JsonSerializer 会将其发送回 JsonSerializationContext 并且循环继续。

关于java - Gson : StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20987556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com