Throwing JsonParseException when the user expected class doesn't match the JSON input during deserialization.

This commit is contained in:
Inderjeet Singh 2009-03-27 23:17:46 +00:00
parent c9cb05fc72
commit 4f727df749
3 changed files with 25 additions and 7 deletions

View File

@ -42,6 +42,9 @@ final class JsonArrayDeserializationVisitor<T> extends JsonDeserializationVisito
TypeInfo typeInfo = new TypeInfo(targetType); TypeInfo typeInfo = new TypeInfo(targetType);
if (!json.isJsonArray()) {
throw new JsonParseException("Expecting array found: " + json);
}
JsonArray jsonArray = json.getAsJsonArray(); JsonArray jsonArray = json.getAsJsonArray();
if (typeInfo.isArray()) { if (typeInfo.isArray()) {
TypeInfoArray arrayTypeInfo = TypeInfoFactory.getTypeInfoForArray(targetType); TypeInfoArray arrayTypeInfo = TypeInfoFactory.getTypeInfoForArray(targetType);
@ -55,6 +58,9 @@ final class JsonArrayDeserializationVisitor<T> extends JsonDeserializationVisito
} }
public void visitArray(Object array, Type arrayType) { public void visitArray(Object array, Type arrayType) {
if (!json.isJsonArray()) {
throw new JsonParseException("Expecting array found: " + json);
}
JsonArray jsonArray = json.getAsJsonArray(); JsonArray jsonArray = json.getAsJsonArray();
TypeInfoArray arrayTypeInfo = TypeInfoFactory.getTypeInfoForArray(arrayType); TypeInfoArray arrayTypeInfo = TypeInfoFactory.getTypeInfoForArray(arrayType);
for (int i = 0; i < jsonArray.size(); i++) { for (int i = 0; i < jsonArray.size(); i++) {
@ -82,18 +88,21 @@ final class JsonArrayDeserializationVisitor<T> extends JsonDeserializationVisito
// instead. // instead.
public void startVisitingObject(Object node) { public void startVisitingObject(Object node) {
throw new UnsupportedOperationException(); throw new JsonParseException("Expecting array but found object: " + node);
} }
public void visitArrayField(Field f, Type typeOfF, Object obj) { public void visitArrayField(Field f, Type typeOfF, Object obj) {
throw new UnsupportedOperationException(); throw new JsonParseException("Expecting array but found array field " + f.getName() + ": "
+ obj);
} }
public void visitObjectField(Field f, Type typeOfF, Object obj) { public void visitObjectField(Field f, Type typeOfF, Object obj) {
throw new UnsupportedOperationException(); throw new JsonParseException("Expecting array but found object field " + f.getName() + ": "
+ obj);
} }
public boolean visitFieldUsingCustomHandler(Field f, Type actualTypeOfField, Object parent) { public boolean visitFieldUsingCustomHandler(Field f, Type actualTypeOfField, Object parent) {
throw new UnsupportedOperationException(); throw new JsonParseException("Expecting array but found field " + f.getName() + ": "
+ parent);
} }
} }

View File

@ -47,11 +47,14 @@ final class JsonObjectDeserializationVisitor<T> extends JsonDeserializationVisit
public void visitArray(Object array, Type componentType) { public void visitArray(Object array, Type componentType) {
// should not be called since this case should invoke JsonArrayDeserializationVisitor // should not be called since this case should invoke JsonArrayDeserializationVisitor
throw new IllegalStateException(); throw new JsonParseException("Expecting object but found array: " + array);
} }
public void visitObjectField(Field f, Type typeOfF, Object obj) { public void visitObjectField(Field f, Type typeOfF, Object obj) {
try { try {
if (!json.isJsonObject()) {
throw new JsonParseException("Expecting object found: " + json);
}
JsonObject jsonObject = json.getAsJsonObject(); JsonObject jsonObject = json.getAsJsonObject();
String fName = getFieldName(f); String fName = getFieldName(f);
JsonElement jsonChild = jsonObject.get(fName); JsonElement jsonChild = jsonObject.get(fName);
@ -68,6 +71,9 @@ final class JsonObjectDeserializationVisitor<T> extends JsonDeserializationVisit
public void visitArrayField(Field f, Type typeOfF, Object obj) { public void visitArrayField(Field f, Type typeOfF, Object obj) {
try { try {
if (!json.isJsonObject()) {
throw new JsonParseException("Expecting object found: " + json);
}
JsonObject jsonObject = json.getAsJsonObject(); JsonObject jsonObject = json.getAsJsonObject();
String fName = getFieldName(f); String fName = getFieldName(f);
JsonArray jsonChild = (JsonArray) jsonObject.get(fName); JsonArray jsonChild = (JsonArray) jsonObject.get(fName);
@ -90,6 +96,9 @@ final class JsonObjectDeserializationVisitor<T> extends JsonDeserializationVisit
public boolean visitFieldUsingCustomHandler(Field f, Type actualTypeOfField, Object parent) { public boolean visitFieldUsingCustomHandler(Field f, Type actualTypeOfField, Object parent) {
try { try {
String fName = getFieldName(f); String fName = getFieldName(f);
if (!json.isJsonObject()) {
throw new JsonParseException("Expecting object found: " + json);
}
JsonElement child = json.getAsJsonObject().get(fName); JsonElement child = json.getAsJsonObject().get(fName);
if (child == null) { if (child == null) {
return true; return true;

View File

@ -62,7 +62,7 @@ public class JsonParserTest extends TestCase {
try { try {
gson.fromJson(array, BagOfPrimitives.class); gson.fromJson(array, BagOfPrimitives.class);
fail("BagOfPrimitives is not an array"); fail("BagOfPrimitives is not an array");
} catch (UnsupportedOperationException expected) { } } catch (JsonParseException expected) { }
} }
public void testBadFieldTypeForCustomDeserializerCustomTree() { public void testBadFieldTypeForCustomDeserializerCustomTree() {
@ -93,7 +93,7 @@ public class JsonParserTest extends TestCase {
try { try {
gson.fromJson(obj, Nested.class); gson.fromJson(obj, Nested.class);
fail("Nested has field BagOfPrimitives which is not an array"); fail("Nested has field BagOfPrimitives which is not an array");
} catch (IllegalStateException expected) { } } catch (JsonParseException expected) { }
} }
public void testChangingCustomTreeAndDeserializing() { public void testChangingCustomTreeAndDeserializing() {