From 4f727df749b9ad97dfd0dd108a67539b7f163ee2 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 27 Mar 2009 23:17:46 +0000 Subject: [PATCH] Throwing JsonParseException when the user expected class doesn't match the JSON input during deserialization. --- .../gson/JsonArrayDeserializationVisitor.java | 17 +++++++++++++---- .../gson/JsonObjectDeserializationVisitor.java | 11 ++++++++++- .../google/gson/functional/JsonParserTest.java | 4 ++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/gson/src/main/java/com/google/gson/JsonArrayDeserializationVisitor.java b/gson/src/main/java/com/google/gson/JsonArrayDeserializationVisitor.java index 835a64bc..8f645d29 100644 --- a/gson/src/main/java/com/google/gson/JsonArrayDeserializationVisitor.java +++ b/gson/src/main/java/com/google/gson/JsonArrayDeserializationVisitor.java @@ -42,6 +42,9 @@ final class JsonArrayDeserializationVisitor extends JsonDeserializationVisito TypeInfo typeInfo = new TypeInfo(targetType); + if (!json.isJsonArray()) { + throw new JsonParseException("Expecting array found: " + json); + } JsonArray jsonArray = json.getAsJsonArray(); if (typeInfo.isArray()) { TypeInfoArray arrayTypeInfo = TypeInfoFactory.getTypeInfoForArray(targetType); @@ -55,6 +58,9 @@ final class JsonArrayDeserializationVisitor extends JsonDeserializationVisito } public void visitArray(Object array, Type arrayType) { + if (!json.isJsonArray()) { + throw new JsonParseException("Expecting array found: " + json); + } JsonArray jsonArray = json.getAsJsonArray(); TypeInfoArray arrayTypeInfo = TypeInfoFactory.getTypeInfoForArray(arrayType); for (int i = 0; i < jsonArray.size(); i++) { @@ -82,18 +88,21 @@ final class JsonArrayDeserializationVisitor extends JsonDeserializationVisito // instead. 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) { - throw new UnsupportedOperationException(); + throw new JsonParseException("Expecting array but found array field " + f.getName() + ": " + + 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) { - throw new UnsupportedOperationException(); + throw new JsonParseException("Expecting array but found field " + f.getName() + ": " + + parent); } } diff --git a/gson/src/main/java/com/google/gson/JsonObjectDeserializationVisitor.java b/gson/src/main/java/com/google/gson/JsonObjectDeserializationVisitor.java index 77e681d2..a6278032 100644 --- a/gson/src/main/java/com/google/gson/JsonObjectDeserializationVisitor.java +++ b/gson/src/main/java/com/google/gson/JsonObjectDeserializationVisitor.java @@ -47,11 +47,14 @@ final class JsonObjectDeserializationVisitor extends JsonDeserializationVisit public void visitArray(Object array, Type componentType) { // 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) { try { + if (!json.isJsonObject()) { + throw new JsonParseException("Expecting object found: " + json); + } JsonObject jsonObject = json.getAsJsonObject(); String fName = getFieldName(f); JsonElement jsonChild = jsonObject.get(fName); @@ -68,6 +71,9 @@ final class JsonObjectDeserializationVisitor extends JsonDeserializationVisit public void visitArrayField(Field f, Type typeOfF, Object obj) { try { + if (!json.isJsonObject()) { + throw new JsonParseException("Expecting object found: " + json); + } JsonObject jsonObject = json.getAsJsonObject(); String fName = getFieldName(f); JsonArray jsonChild = (JsonArray) jsonObject.get(fName); @@ -90,6 +96,9 @@ final class JsonObjectDeserializationVisitor extends JsonDeserializationVisit public boolean visitFieldUsingCustomHandler(Field f, Type actualTypeOfField, Object parent) { try { String fName = getFieldName(f); + if (!json.isJsonObject()) { + throw new JsonParseException("Expecting object found: " + json); + } JsonElement child = json.getAsJsonObject().get(fName); if (child == null) { return true; diff --git a/gson/src/test/java/com/google/gson/functional/JsonParserTest.java b/gson/src/test/java/com/google/gson/functional/JsonParserTest.java index c51f2d76..aaba4110 100644 --- a/gson/src/test/java/com/google/gson/functional/JsonParserTest.java +++ b/gson/src/test/java/com/google/gson/functional/JsonParserTest.java @@ -62,7 +62,7 @@ public class JsonParserTest extends TestCase { try { gson.fromJson(array, BagOfPrimitives.class); fail("BagOfPrimitives is not an array"); - } catch (UnsupportedOperationException expected) { } + } catch (JsonParseException expected) { } } public void testBadFieldTypeForCustomDeserializerCustomTree() { @@ -93,7 +93,7 @@ public class JsonParserTest extends TestCase { try { gson.fromJson(obj, Nested.class); fail("Nested has field BagOfPrimitives which is not an array"); - } catch (IllegalStateException expected) { } + } catch (JsonParseException expected) { } } public void testChangingCustomTreeAndDeserializing() {