Don't return null on an unexpected EOF unless the document is empty. This brings us back to compatibility with GSON 1.5.
This commit is contained in:
parent
4d0cd67cde
commit
103edb9c36
@ -17,9 +17,8 @@
|
|||||||
package com.google.gson;
|
package com.google.gson;
|
||||||
|
|
||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
import com.google.gson.stream.MalformedJsonException;
|
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
import com.google.gson.stream.MalformedJsonException;
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
@ -34,7 +33,31 @@ final class Streams {
|
|||||||
* Takes a reader in any state and returns the next value as a JsonElement.
|
* Takes a reader in any state and returns the next value as a JsonElement.
|
||||||
*/
|
*/
|
||||||
static JsonElement parse(JsonReader reader) throws JsonParseException {
|
static JsonElement parse(JsonReader reader) throws JsonParseException {
|
||||||
|
boolean isEmpty = true;
|
||||||
try {
|
try {
|
||||||
|
reader.peek();
|
||||||
|
isEmpty = false;
|
||||||
|
return parseRecursive(reader);
|
||||||
|
} catch (EOFException e) {
|
||||||
|
/*
|
||||||
|
* For compatibility with JSON 1.5 and earlier, we return a JsonNull for
|
||||||
|
* empty documents instead of throwing.
|
||||||
|
*/
|
||||||
|
if (isEmpty) {
|
||||||
|
return JsonNull.createJsonNull();
|
||||||
|
} else {
|
||||||
|
throw new JsonIOException(e);
|
||||||
|
}
|
||||||
|
} catch (MalformedJsonException e) {
|
||||||
|
throw new JsonSyntaxException(e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new JsonIOException(e);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new JsonSyntaxException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static JsonElement parseRecursive(JsonReader reader) throws IOException {
|
||||||
switch (reader.peek()) {
|
switch (reader.peek()) {
|
||||||
case STRING:
|
case STRING:
|
||||||
return new JsonPrimitive(reader.nextString());
|
return new JsonPrimitive(reader.nextString());
|
||||||
@ -50,7 +73,7 @@ final class Streams {
|
|||||||
JsonArray array = new JsonArray();
|
JsonArray array = new JsonArray();
|
||||||
reader.beginArray();
|
reader.beginArray();
|
||||||
while (reader.hasNext()) {
|
while (reader.hasNext()) {
|
||||||
array.add(parse(reader));
|
array.add(parseRecursive(reader));
|
||||||
}
|
}
|
||||||
reader.endArray();
|
reader.endArray();
|
||||||
return array;
|
return array;
|
||||||
@ -58,7 +81,7 @@ final class Streams {
|
|||||||
JsonObject object = new JsonObject();
|
JsonObject object = new JsonObject();
|
||||||
reader.beginObject();
|
reader.beginObject();
|
||||||
while (reader.hasNext()) {
|
while (reader.hasNext()) {
|
||||||
object.add(reader.nextName(), parse(reader));
|
object.add(reader.nextName(), parseRecursive(reader));
|
||||||
}
|
}
|
||||||
reader.endObject();
|
reader.endObject();
|
||||||
return object;
|
return object;
|
||||||
@ -69,15 +92,6 @@ final class Streams {
|
|||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
} catch (EOFException e) {
|
|
||||||
return JsonNull.createJsonNull();
|
|
||||||
} catch (MalformedJsonException e) {
|
|
||||||
throw new JsonSyntaxException(e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new JsonIOException(e);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
throw new JsonSyntaxException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,6 +20,7 @@ import com.google.gson.Gson;
|
|||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.InstanceCreator;
|
import com.google.gson.InstanceCreator;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
import com.google.gson.common.TestTypes.ArrayOfObjects;
|
import com.google.gson.common.TestTypes.ArrayOfObjects;
|
||||||
import com.google.gson.common.TestTypes.BagOfPrimitiveWrappers;
|
import com.google.gson.common.TestTypes.BagOfPrimitiveWrappers;
|
||||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||||
@ -30,6 +31,7 @@ import com.google.gson.common.TestTypes.ClassWithTransientFields;
|
|||||||
import com.google.gson.common.TestTypes.Nested;
|
import com.google.gson.common.TestTypes.Nested;
|
||||||
import com.google.gson.common.TestTypes.PrimitiveArray;
|
import com.google.gson.common.TestTypes.PrimitiveArray;
|
||||||
|
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
@ -145,6 +147,14 @@ public class ObjectTest extends TestCase {
|
|||||||
assertNull(object);
|
assertNull(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testTruncatedDeserialization() {
|
||||||
|
try {
|
||||||
|
gson.fromJson("[\"a\", \"b\",", new TypeToken<List<String>>() {}.getType());
|
||||||
|
fail();
|
||||||
|
} catch (JsonParseException expected) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void testNullDeserialization() throws Exception {
|
public void testNullDeserialization() throws Exception {
|
||||||
String myNullObject = null;
|
String myNullObject = null;
|
||||||
Object object = gson.fromJson(myNullObject, Object.class);
|
Object object = gson.fromJson(myNullObject, Object.class);
|
||||||
|
Loading…
Reference in New Issue
Block a user