Throw JsonParseException in event of binding failures like type mismatches.

This commit is contained in:
Jesse Wilson 2011-10-24 01:32:46 +00:00
parent c226bd4f3f
commit 016261d9cf
2 changed files with 21 additions and 1 deletions

View File

@ -637,7 +637,6 @@ public final class Gson {
T target = (T) fromJson(reader, typeOfT);
return target;
} catch (IllegalStateException e) {
// TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException
throw new JsonSyntaxException(e);
}
}
@ -732,6 +731,8 @@ public final class Gson {
return null;
}
throw new JsonSyntaxException(e);
} catch (IllegalStateException e) {
throw new JsonSyntaxException(e);
} catch (IOException e) {
// TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException
throw new JsonSyntaxException(e);

View File

@ -18,8 +18,11 @@ package com.google.gson.functional;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonStreamParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.reflect.TypeToken;
import java.util.Map;
import junit.framework.TestCase;
import java.io.CharArrayReader;
@ -113,4 +116,20 @@ public class ReadersWritersTest extends TestCase {
assertEquals("two", actualTwo.stringValue);
assertFalse(parser.hasNext());
}
public void testTypeMismatchThrowsJsonSyntaxExceptionForStrings() {
try {
gson.fromJson("true", new TypeToken<Map<String, String>>() {}.getType());
fail();
} catch (JsonSyntaxException expected) {
}
}
public void testTypeMismatchThrowsJsonSyntaxExceptionForReaders() {
try {
gson.fromJson(new StringReader("true"), new TypeToken<Map<String, String>>() {}.getType());
fail();
} catch (JsonSyntaxException expected) {
}
}
}