Gson : StackOverflowError

大城市里の小女人 提交于 2019-12-24 03:09:41

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/20987556/gson-stackoverflowerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!