I'm trying to parse a big chunk of JSON with gson (assisted by GsonFire). Somewhere along the way it's throwing a JsonSyntaxException
.
I know what the exception means, but I'm parsing a huge file and it would really help if I could figure out which token is causing the exception. Is there a way to get more verbose output from gson?
Exception is below:
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
With Gson 2.3, you get a much nicer exception. It tells you the JSONPath to the failure, for example:
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()
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With