Closed GoogleCodeExporter closed 9 years ago
Full stacktrace below:
ClassCastException: com.google.gson.JsonArray cannot be cast to
com.google.gson.JsonObject
java.lang.ClassCastException
at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:78)
at
com.google.gson.JsonObjectDeserializationVisitor.visitPrimitiveField(JsonObjectD
eserializationVisitor.java:129)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:177)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:151)
at
com.google.gson.JsonDeserializationVisitor.visitChild(JsonDeserializationVisitor
.java:131)
at
com.google.gson.JsonDeserializationVisitor.visitChildAsObject(JsonDeserializatio
nVisitor.java:93)
at
com.google.gson.JsonObjectDeserializationVisitor.visitObjectField(JsonObjectDese
rializationVisitor.java:75)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:179)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:151)
at
com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializa
tionContextDefault.java:75)
at
com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializatio
nContextDefault.java:47)
at com.google.gson.Gson.fromJson(Gson.java:379)
at com.google.gson.Gson.fromJson(Gson.java:327)
at com.google.gson.Gson.fromJson(Gson.java:303)
Original comment by ch...@gmail.com
on 4 Mar 2009 at 7:13
You have an array and you are trying to map it to an object. Objects have
name-value
pairs whereas Arrays only have values. This mapping can not be made to work in
Gson.
Original comment by inder123
on 11 Mar 2009 at 8:53
Yep, I understand, but the expected output should be a JsonParseError, not a
ClassCastException -- is this correct?
Am I supposed to try/catch ClassCastException when parsing untrusted input?
Original comment by ch...@gmail.com
on 20 Mar 2009 at 2:41
I don't think catching a ClassCastException is a good idea. I don't know if
throwing
a "JsonParseException" is the right thing to do either because it is valid JSON.
It's more like bad user input which sounds like an "IllegalArgumentException"
should
be thrown in this case.
Thoughts?
Original comment by joel.leitch@gmail.com
on 20 Mar 2009 at 3:13
That's fine with me. I consider both malformed JSON and
valid-JSON-that-doesn't-match-what-I'm-parsing to be parse errors, but I'm find
catching a second exception if that's the desired behavior.
Original comment by ch...@gmail.com
on 26 Mar 2009 at 10:43
On second thoughts, we decided to consider all such errors as parsing errors,
so we
will now be throwing JsonParseException if the input didn't match what was
expected
(even if it was valid JSON).
Original comment by inder123
on 28 Mar 2009 at 5:54
Fixed in r404
Original comment by inder123
on 28 Mar 2009 at 5:55
Original issue reported on code.google.com by
ch...@gmail.com
on 4 Mar 2009 at 7:12