Gson:StackOverflowError

时间:2014-01-08 05:16:22

标签: java json gson stack-overflow

以下代码导致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)

这是我的serializerdeserializer

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(),仍然无法解析。

1 个答案:

答案 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并继续循环播放。