Be strict in TypeAdapter's toJson/fromJson methods

This commit is contained in:
Jesse Wilson 2012-01-01 12:36:42 +00:00
parent 641590b5b6
commit 323dfa0af5
2 changed files with 1 additions and 4 deletions

View File

@ -227,7 +227,6 @@ public abstract class TypeAdapter<T> {
public final JsonElement toJsonTree(T value) {
try {
JsonTreeWriter jsonWriter = new JsonTreeWriter();
jsonWriter.setLenient(true);
write(jsonWriter, value);
return jsonWriter.get();
} catch (IOException e) {
@ -254,7 +253,6 @@ public abstract class TypeAdapter<T> {
*/
public final T fromJson(Reader in) throws IOException {
JsonReader reader = new JsonReader(in);
reader.setLenient(true); // TODO: non-lenient?
return read(reader);
}
@ -280,7 +278,6 @@ public abstract class TypeAdapter<T> {
public final T fromJsonTree(JsonElement jsonTree) {
try {
JsonReader jsonReader = new JsonTreeReader(jsonTree);
jsonReader.setLenient(true);
return read(jsonReader);
} catch (IOException e) {
throw new JsonIOException(e);

View File

@ -26,7 +26,7 @@ public final class ObjectTypeAdapterTest extends TestCase {
private final TypeAdapter<Object> adapter = gson.getAdapter(Object.class);
public void testDeserialize() throws Exception {
Map<?, ?> map = (Map<?, ?>) adapter.fromJson("{a: 5, b: [1, 2, null], c: {x: y}}");
Map<?, ?> map = (Map<?, ?>) adapter.fromJson("{\"a\":5,\"b\":[1,2,null],\"c\":{\"x\":\"y\"}}");
assertEquals(5.0, map.get("a"));
assertEquals(Arrays.asList(1.0, 2.0, null), map.get("b"));
assertEquals(Collections.singletonMap("x", "y"), map.get("c"));