discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

ClassCastException thrown instead of ParseError #105

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. class TestObjectA { TestObjectB b }
2. String two = {"b" : [ ]}
3. new Gson().fromJson(two, TestObjectA.class)

What is the expected output? What do you see instead?

Expected: parse error

Actual: ClassCastException: com.google.gson.JsonArray cannot be cast to
com.google.gson.JsonObject

What version of the product are you using? On what operating system?

v1.2.2 on Linux

Please provide any additional information below.

Original issue reported on code.google.com by ch...@gmail.com on 4 Mar 2009 at 7:12

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
Fixed in r404

Original comment by inder123 on 28 Mar 2009 at 5:55