Fixed issue 443 by relying on Streams.parse() to return a JsonNull on empty documents and throw a JsonParseException otherwise.
This commit is contained in:
parent
1c7aee40f3
commit
582b0a0c9c
@ -15,14 +15,14 @@
|
|||||||
*/
|
*/
|
||||||
package com.google.gson;
|
package com.google.gson;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
|
||||||
import com.google.gson.internal.Streams;
|
import com.google.gson.internal.Streams;
|
||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
import com.google.gson.stream.JsonToken;
|
import com.google.gson.stream.JsonToken;
|
||||||
import com.google.gson.stream.MalformedJsonException;
|
import com.google.gson.stream.MalformedJsonException;
|
||||||
import java.io.EOFException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A parser to parse Json into a parse tree of {@link JsonElement}s
|
* A parser to parse Json into a parse tree of {@link JsonElement}s
|
||||||
@ -86,11 +86,6 @@ public final class JsonParser {
|
|||||||
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);
|
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);
|
||||||
} catch (OutOfMemoryError e) {
|
} catch (OutOfMemoryError e) {
|
||||||
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);
|
throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);
|
||||||
} catch (JsonParseException e) {
|
|
||||||
if (e.getCause() instanceof EOFException) {
|
|
||||||
return JsonNull.INSTANCE;
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
} finally {
|
} finally {
|
||||||
json.setLenient(lenient);
|
json.setLenient(lenient);
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,9 @@ public final class Streams {
|
|||||||
if (isEmpty) {
|
if (isEmpty) {
|
||||||
return JsonNull.INSTANCE;
|
return JsonNull.INSTANCE;
|
||||||
}
|
}
|
||||||
|
// We could possibly throw JsonSyntaxException since the stream prematurely ended.
|
||||||
|
// However, it seems safe to throw JsonIOException since the source is an IOException.
|
||||||
|
// Another reason is to maintain backward compatibility.
|
||||||
throw new JsonIOException(e);
|
throw new JsonIOException(e);
|
||||||
} catch (MalformedJsonException e) {
|
} catch (MalformedJsonException e) {
|
||||||
throw new JsonSyntaxException(e);
|
throw new JsonSyntaxException(e);
|
||||||
|
@ -38,6 +38,13 @@ public class JsonParserTest extends TestCase {
|
|||||||
parser = new JsonParser();
|
parser = new JsonParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testParseInvalidJson() {
|
||||||
|
try {
|
||||||
|
parser.parse("[[]");
|
||||||
|
fail();
|
||||||
|
} catch (JsonParseException expected) { }
|
||||||
|
}
|
||||||
|
|
||||||
public void testParseUnquotedStringArrayFails() {
|
public void testParseUnquotedStringArrayFails() {
|
||||||
JsonElement element = parser.parse("[a,b,c]");
|
JsonElement element = parser.parse("[a,b,c]");
|
||||||
assertEquals("a", element.getAsJsonArray().get(0).getAsString());
|
assertEquals("a", element.getAsJsonArray().get(0).getAsString());
|
||||||
|
@ -50,6 +50,13 @@ public class JsonParserTest extends TestCase {
|
|||||||
gson = new Gson();
|
gson = new Gson();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testParseInvalidJson() {
|
||||||
|
try {
|
||||||
|
gson.fromJson("[[]", Object[].class);
|
||||||
|
fail();
|
||||||
|
} catch (JsonSyntaxException expected) { }
|
||||||
|
}
|
||||||
|
|
||||||
public void testDeserializingCustomTree() {
|
public void testDeserializingCustomTree() {
|
||||||
JsonObject obj = new JsonObject();
|
JsonObject obj = new JsonObject();
|
||||||
obj.addProperty("stringValue", "foo");
|
obj.addProperty("stringValue", "foo");
|
||||||
|
Loading…
Reference in New Issue
Block a user