Be strict in TypeAdapter's toJson/fromJson methods
This commit is contained in:
parent
641590b5b6
commit
323dfa0af5
@ -227,7 +227,6 @@ public abstract class TypeAdapter<T> {
|
|||||||
public final JsonElement toJsonTree(T value) {
|
public final JsonElement toJsonTree(T value) {
|
||||||
try {
|
try {
|
||||||
JsonTreeWriter jsonWriter = new JsonTreeWriter();
|
JsonTreeWriter jsonWriter = new JsonTreeWriter();
|
||||||
jsonWriter.setLenient(true);
|
|
||||||
write(jsonWriter, value);
|
write(jsonWriter, value);
|
||||||
return jsonWriter.get();
|
return jsonWriter.get();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -254,7 +253,6 @@ public abstract class TypeAdapter<T> {
|
|||||||
*/
|
*/
|
||||||
public final T fromJson(Reader in) throws IOException {
|
public final T fromJson(Reader in) throws IOException {
|
||||||
JsonReader reader = new JsonReader(in);
|
JsonReader reader = new JsonReader(in);
|
||||||
reader.setLenient(true); // TODO: non-lenient?
|
|
||||||
return read(reader);
|
return read(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,7 +278,6 @@ public abstract class TypeAdapter<T> {
|
|||||||
public final T fromJsonTree(JsonElement jsonTree) {
|
public final T fromJsonTree(JsonElement jsonTree) {
|
||||||
try {
|
try {
|
||||||
JsonReader jsonReader = new JsonTreeReader(jsonTree);
|
JsonReader jsonReader = new JsonTreeReader(jsonTree);
|
||||||
jsonReader.setLenient(true);
|
|
||||||
return read(jsonReader);
|
return read(jsonReader);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new JsonIOException(e);
|
throw new JsonIOException(e);
|
||||||
|
@ -26,7 +26,7 @@ public final class ObjectTypeAdapterTest extends TestCase {
|
|||||||
private final TypeAdapter<Object> adapter = gson.getAdapter(Object.class);
|
private final TypeAdapter<Object> adapter = gson.getAdapter(Object.class);
|
||||||
|
|
||||||
public void testDeserialize() throws Exception {
|
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(5.0, map.get("a"));
|
||||||
assertEquals(Arrays.asList(1.0, 2.0, null), map.get("b"));
|
assertEquals(Arrays.asList(1.0, 2.0, null), map.get("b"));
|
||||||
assertEquals(Collections.singletonMap("x", "y"), map.get("c"));
|
assertEquals(Collections.singletonMap("x", "y"), map.get("c"));
|
||||||
|
Loading…
Reference in New Issue
Block a user