来自gson的详细异常消息?

时间:2015-03-10 22:41:04

标签: java json debugging gson

我正在尝试用gson解析一大块JSON(由GsonFire协助)。在某个地方,它正在抛出一个JsonSyntaxException

我知道异常意味着什么,但是我正在解析一个巨大的文件,如果我能找出哪个令牌导致异常,那将会非常有用。有没有办法从gson获得更详细的输出?

例外情况如下:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
    at com.google.gson.TypeAdapter.fromJsonTree(TypeAdapter.java:281)
    at io.gsonfire.gson.FireTypeAdapter.deserialize(FireTypeAdapter.java:93)
    at io.gsonfire.gson.FireTypeAdapter.read(FireTypeAdapter.java:52)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
    at com.google.gson.TypeAdapter.fromJsonTree(TypeAdapter.java:281)
    at io.gsonfire.gson.FireTypeAdapter.deserialize(FireTypeAdapter.java:93)
    at io.gsonfire.gson.FireTypeAdapter.read(FireTypeAdapter.java:52)
    at com.google.gson.TypeAdapter.fromJsonTree(TypeAdapter.java:281)
    at io.gsonfire.gson.FireTypeAdapter.deserialize(FireTypeAdapter.java:93)
    at io.gsonfire.gson.FireTypeAdapter.read(FireTypeAdapter.java:52)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
    at com.google.gson.TypeAdapter.fromJsonTree(TypeAdapter.java:281)
    at io.gsonfire.gson.FireTypeAdapter.deserialize(FireTypeAdapter.java:93)
    at io.gsonfire.gson.FireTypeAdapter.read(FireTypeAdapter.java:52)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
    at com.google.gson.TypeAdapter.fromJsonTree(TypeAdapter.java:281)
    at io.gsonfire.gson.FireTypeAdapter.deserialize(FireTypeAdapter.java:93)
    at io.gsonfire.gson.FireTypeAdapter.read(FireTypeAdapter.java:52)
    at com.google.gson.Gson.fromJson(Gson.java:810)
    at com.google.gson.Gson.fromJson(Gson.java:775)
    at com.google.gson.Gson.fromJson(Gson.java:724)
    at com.google.gson.Gson.fromJson(Gson.java:696)
    at com.inin.util.BinarySerializable.fromJsonFile(BinarySerializable.java:90)
    at com.inin.util.BinarySerializable.fromJsonFile(BinarySerializable.java:83)
    at com.inin.simulation.App.main(App.java:35)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING
    at com.google.gson.internal.bind.JsonTreeReader.expect(JsonTreeReader.java:139)
    at com.google.gson.internal.bind.JsonTreeReader.beginObject(JsonTreeReader.java:70)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:189)
    ... 29 more

1 个答案:

答案 0 :(得分:2)

使用Gson 2.3,您可以获得更好的例外。它会告诉您失败的JSONPath,例如:

com.google.gson.JsonSyntaxException: Expected a boolean but was NUMBER at line 1 column 4728 path $.history[0].change_status.deletions  
  at com.google.gson.stream.JsonReader.nextBoolean()
  at com.google.gson.TypeAdapters$3.read()
  at com.google.gson.TypeAdapters$3.read()
  at com.google.gson.ReflectiveTypeAdapter$1.read()
  at com.google.gson.ReflectiveTypeAdapter$Adapter.read()
  at com.google.gson.ReflectiveTypeAdapter$Adapter.read()
  at com.google.gson.ReflectiveTypeAdapter$1.read()
  at com.google.gson.ReflectiveTypeAdapter$Adapter.read()
  ...