Throwing JsonParseException when the user expected class doesn't match the JSON input during deserialization.
This commit is contained in:
parent
c9cb05fc72
commit
4f727df749
@ -42,6 +42,9 @@ final class JsonArrayDeserializationVisitor<T> 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<T> 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<T> 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);
|
||||
}
|
||||
}
|
||||
|
@ -47,11 +47,14 @@ final class JsonObjectDeserializationVisitor<T> 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<T> 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<T> 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;
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user