Do not leak out a ClassCastException to our clients.
This commit is contained in:
parent
727dbcdd88
commit
c9cb05fc72
@ -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> T fromJson(JsonElement json, Class<T> classOfT) throws JsonParseException {
|
||||
T target = classOfT.cast(fromJson(json, (Type) classOfT));
|
||||
return target;
|
||||
|
@ -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() {
|
||||
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() {
|
||||
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() {
|
||||
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() {
|
||||
if (isJsonNull()) {
|
||||
return (JsonNull) this;
|
||||
} else {
|
||||
throw new IllegalStateException("This is not a JSON Null.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
@ -49,6 +53,49 @@ public class JsonParserTest extends TestCase {
|
||||
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 =
|
||||
new StringReader("{'stringValue':'no message','intValue':10,'longValue':20}");
|
||||
|
@ -473,8 +473,10 @@ public class ParameterizedTypesTest extends TestCase {
|
||||
private interface Immutable {
|
||||
}
|
||||
|
||||
public static final class Amount<Q extends Quantity> implements
|
||||
Measurable<Q>, Field<Amount<?>>, Serializable, Immutable {
|
||||
public static final class Amount<Q extends Quantity>
|
||||
implements Measurable<Q>, Field<Amount<?>>, Serializable, Immutable {
|
||||
private static final long serialVersionUID = -7560491093120970437L;
|
||||
|
||||
int value = 30;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user