GSON.toJson(new RuntimeException())抛出StackOverflowError

时间:2018-01-31 09:59:10

标签: java exception gson

我的应用程序抛出了StackOverflowError。发现我们的应用程序以JSON的形式记录了Exception,并且代码如下。

GSON.toJson(object) //here object can be my application exception or any runtime exception

令我惊讶的是GSON.toJson抛出了StackOverflowError。

尝试以下设置验证:

public static void main(String[] args) {
    Gson GSON = new Gson();
    GSON.toJson(new NullPointerException());
  }

堆栈跟踪:

Exception in thread "main" java.lang.StackOverflowError
    at com.google.gson.stream.JsonWriter.string(JsonWriter.java:519)
    at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:387)
    at com.google.gson.stream.JsonWriter.beginObject(JsonWriter.java:300)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:190)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)

有人能说出为什么我不能获得任何例外的JSON吗?

注意:这仅在我们的应用程序中发生

1 个答案:

答案 0 :(得分:0)

升级到版本Gson 2.6.1解决了问题