Gson:StackOverflowError [英] Gson : StackOverflowError
问题描述
以下代码导致 StackOverflowError
。
for(ControlTransaction crt:ctrList){
crt = new ControlTransaction(); // for test,仍然发出
final Gson gson = new GsonBuilder()。registerTypeAdapter(
ControlTransaction.class,$ b $ new ControlTransactionSerializer())。create() ;
String jsonControlTransactionString = gson.toJson(crt);
strList.add(jsonControlTransactionString);
我的班级看起来像是
public class ControlTransaction实现了IsSerializable,Serializable,IsBean {
private long id;
私有字符串值; // H
private String lastValue; // H
private FormTransaction formTransaction;
私人列表< FormTransaction> gridRows;
私人ControlTransaction referenceGridTransaction;
private RowTransaction fkRowTransaction;
私人ReportTransaction reportTransaction;
// getters ... setters
}
,让我知道
原因:java.lang.StackOverflowError
,com.google.gson.stream.JsonWriter 。< init>(JsonWriter.java:190)
at com.google.gson.internal.bind.JsonTreeWriter。< init>(JsonTreeWriter.java:58)
com.google.gson .Gson.toJsonTree(Gson.java:478)
在com.google.gson.Gson $ 3.serialize(Gson.java:140)
在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)
在com.google.gson.Gson.toJson(Gson.java:586)
在com.google.gson.Gson.toJsonTree(Gson.java:479)
在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)
在com.google.gson.Gson.toJson(Gson.java:586)
在com.google.gson.Gson.toJsonTree(Gson.java:479)
在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)
在com.google.gson.TreeTypeAdapter.write(TreeTypeAdapter.java:70)
在com.google.gson.Gson.toJson(Gson.java:586)
在com .google.gson.Gson.toJsonTree(Gson.java:479)
com.google.gson.Gson $ 3.serialize(Gson.java:140)
这是我的序列化程序
和反序列化程序
类ControlTransactionDeserializer实现
JsonDeserializer< Con trolTransaction> {
@Override
public ControlTransaction deserialize(JsonElement json,Type type,
JsonDeserializationContext context)throws JsonParseException {
return context.deserialize(json,type);
}
}
class ControlTransactionSerializer实现
JsonSerializer< ControlTransaction> {
@Override
public JsonElement serialize(ControlTransaction ctr,Type type,
JsonSerializationContext context){
return context.serialize(ctr,type);
}
}
在后端发生了什么。我只是传递了一个空的Object,这意味着新的 ControlTransaction()
,仍然无法解析。
解决方案 JsonSerializer
的要点是序列化Object的字段,而不是对象本身。然而,你传递的是你已经告诉 Gson
的对象来序列化。以下 @Override
public JsonElement serialize(ControlTransaction ctr,Type type,
JsonSerializationContext context){
返回context.serialize(ctr,type);
$ / code>
相当于
@Override
public JsonElement serialize(ControlTransaction ctr,Type type,
JsonSerializationContext context){
返回新的GsonBuilder()。registerTypeAdapter(
ControlTransaction.class,
ControlTransactionSerializer())。create()。toJsonTree(ctr);
}
我希望你能看到它正在进入递归循环。
$ b JsonSerializationContext
基本上是 Gson
对象用于的基础结构序列化你的对象。当它看到您的类型 ControlTransaction
时,它将委托给您的自定义 JsonSerializer
。但是,您的 JsonSerializer
会将它发送回 JsonSerializationContext
,循环继续。
The below codes causing StackOverflowError
. purpose of the code is to create a json string from java class.
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);
}
My class looks like
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
}
Stack trace followed like this, Let me know
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)
Here is my serializer
and 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);
}
}
Whats going on in backend. I just passed a empty Object, that means new ControlTransaction()
, Still unable to parse.
解决方案 The point of a JsonSerializer
is to serialize the fields of an Object, not the object itself. However, you are passing the object you've already told Gson
to serialize. The following
@Override
public JsonElement serialize(ControlTransaction ctr, Type type,
JsonSerializationContext context) {
return context.serialize(ctr, type);
}
is equivalent to
@Override
public JsonElement serialize(ControlTransaction ctr, Type type,
JsonSerializationContext context) {
return new GsonBuilder().registerTypeAdapter(
ControlTransaction.class,
new ControlTransactionSerializer()).create().toJsonTree(ctr);
}
which I hope you can see is going into recursive loop.
The JsonSerializationContext
is basically the underlying structure that the Gson
object uses to serialize your object. When it sees your type ControlTransaction
, it will delegate to your custom JsonSerializer
. But your JsonSerializer
will send it back to the JsonSerializationContext
and the loop goes on.
这篇关于Gson:StackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文