diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 640c4797..1c0342d3 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -402,7 +402,6 @@ public final class Gson { * @throws JsonParseException if json is not a valid representation for an object of type typeOfT * @since 1.3 */ - @SuppressWarnings("unchecked") public T fromJson(JsonElement json, Class classOfT) throws JsonParseException { T target = classOfT.cast(fromJson(json, (Type) classOfT)); return target; diff --git a/gson/src/main/java/com/google/gson/JsonElement.java b/gson/src/main/java/com/google/gson/JsonElement.java index 3c8e50cd..5b367844 100644 --- a/gson/src/main/java/com/google/gson/JsonElement.java +++ b/gson/src/main/java/com/google/gson/JsonElement.java @@ -73,10 +73,14 @@ public abstract class JsonElement { * first. * * @return get this element as a {@link JsonObject}. - * @throws ClassCastException if the element is of another type. + * @throws IllegalStateException if the element is of another type. */ public JsonObject getAsJsonObject() { - return (JsonObject) this; + if (isJsonObject()) { + return (JsonObject) this; + } else { + throw new IllegalStateException("This is not a JSON Object."); + } } /** @@ -86,10 +90,14 @@ public abstract class JsonElement { * first. * * @return get this element as a {@link JsonArray}. - * @throws ClassCastException if the element is of another type. + * @throws IllegalStateException if the element is of another type. */ public JsonArray getAsJsonArray() { - return (JsonArray) this; + if (isJsonArray()) { + return (JsonArray) this; + } else { + throw new IllegalStateException("This is not a JSON Array."); + } } /** @@ -99,10 +107,14 @@ public abstract class JsonElement { * first. * * @return get this element as a {@link JsonPrimitive}. - * @throws ClassCastException if the element is of another type. + * @throws IllegalStateException if the element is of another type. */ public JsonPrimitive getAsJsonPrimitive() { - return (JsonPrimitive) this; + if (isJsonPrimitive()) { + return (JsonPrimitive) this; + } else { + throw new IllegalStateException("This is not a JSON Primitive."); + } } /** @@ -112,11 +124,15 @@ public abstract class JsonElement { * first. * * @return get this element as a {@link JsonNull}. - * @throws ClassCastException if the element is of another type. + * @throws IllegalStateException if the element is of another type. * @since 1.2 */ public JsonNull getAsJsonNull() { - return (JsonNull) this; + if (isJsonNull()) { + return (JsonNull) this; + } else { + throw new IllegalStateException("This is not a JSON Null."); + } } /** 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 2ded2c41..c51f2d76 100644 --- a/gson/src/test/java/com/google/gson/functional/JsonParserTest.java +++ b/gson/src/test/java/com/google/gson/functional/JsonParserTest.java @@ -16,14 +16,18 @@ package com.google.gson.functional; -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.common.TestTypes.BagOfPrimitives; +import java.io.StringReader; import junit.framework.TestCase; -import java.io.StringReader; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; +import com.google.gson.common.TestTypes.BagOfPrimitives; +import com.google.gson.common.TestTypes.Nested; /** * Functional tests for that use JsonParser and related Gson methods @@ -48,6 +52,49 @@ public class JsonParserTest extends TestCase { assertEquals(11, target.intValue); assertEquals("foo", target.stringValue); } + + public void testBadTypeForDeserializingCustomTree() { + JsonObject obj = new JsonObject(); + obj.addProperty("stringValue", "foo"); + obj.addProperty("intValue", 11); + JsonArray array = new JsonArray(); + array.add(obj); + try { + gson.fromJson(array, BagOfPrimitives.class); + fail("BagOfPrimitives is not an array"); + } catch (UnsupportedOperationException expected) { } + } + + public void testBadFieldTypeForCustomDeserializerCustomTree() { + JsonArray array = new JsonArray(); + array.add(new JsonPrimitive("blah")); + JsonObject obj = new JsonObject(); + obj.addProperty("stringValue", "foo"); + obj.addProperty("intValue", 11); + obj.add("longValue", array); + + try { + gson.fromJson(obj, BagOfPrimitives.class); + fail("BagOfPrimitives is not an array"); + } catch (JsonParseException expected) { } + } + + public void testBadFieldTypeForDeserializingCustomTree() { + JsonArray array = new JsonArray(); + array.add(new JsonPrimitive("blah")); + JsonObject primitive1 = new JsonObject(); + primitive1.addProperty("string", "foo"); + primitive1.addProperty("intValue", 11); + + JsonObject obj = new JsonObject(); + obj.add("primitive1", primitive1); + obj.add("primitive2", array); + + try { + gson.fromJson(obj, Nested.class); + fail("Nested has field BagOfPrimitives which is not an array"); + } catch (IllegalStateException expected) { } + } public void testChangingCustomTreeAndDeserializing() { StringReader json = diff --git a/gson/src/test/java/com/google/gson/functional/ParameterizedTypesTest.java b/gson/src/test/java/com/google/gson/functional/ParameterizedTypesTest.java index aec2a80e..f59f3136 100644 --- a/gson/src/test/java/com/google/gson/functional/ParameterizedTypesTest.java +++ b/gson/src/test/java/com/google/gson/functional/ParameterizedTypesTest.java @@ -473,8 +473,10 @@ public class ParameterizedTypesTest extends TestCase { private interface Immutable { } - public static final class Amount implements - Measurable, Field>, Serializable, Immutable { + public static final class Amount + implements Measurable, Field>, Serializable, Immutable { + private static final long serialVersionUID = -7560491093120970437L; + int value = 30; }