以下代码导致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);
}
我的班级看起来像
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)
这是我的serializer
和deserializer
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);
}
}
后端发生了什么事。我刚刚传递了一个空的Object,这意味着新的ControlTransaction()
,仍然无法解析。
答案 0 :(得分:5)
JsonSerializer
的要点是序列化Object的字段,而不是对象本身。但是,您传递的对象已经告诉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
时,它会委托给您的自定义JsonSerializer
。但是您的JsonSerializer
会将其发送回JsonSerializationContext
并继续循环播放。