When EOF is encountered prematurely, Streams.parse() (and JsonParser) now throw JsonSyntaxException.

This commit is contained in:
Inderjeet Singh 2012-07-02 18:36:54 +00:00
parent 582b0a0c9c
commit 6575cdebca
2 changed files with 8 additions and 8 deletions

View File

@ -50,10 +50,8 @@ public final class Streams {
if (isEmpty) { if (isEmpty) {
return JsonNull.INSTANCE; return JsonNull.INSTANCE;
} }
// We could possibly throw JsonSyntaxException since the stream prematurely ended. // The stream ended prematurely so it is likely a syntax error.
// However, it seems safe to throw JsonIOException since the source is an IOException. throw new JsonSyntaxException(e);
// Another reason is to maintain backward compatibility.
throw new JsonIOException(e);
} catch (MalformedJsonException e) { } catch (MalformedJsonException e) {
throw new JsonSyntaxException(e); throw new JsonSyntaxException(e);
} catch (IOException e) { } catch (IOException e) {

View File

@ -16,14 +16,16 @@
package com.google.gson; package com.google.gson;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;
import java.io.CharArrayReader; import java.io.CharArrayReader;
import java.io.CharArrayWriter; import java.io.CharArrayWriter;
import java.io.StringReader; import java.io.StringReader;
import junit.framework.TestCase; import junit.framework.TestCase;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader;
/** /**
* Unit test for {@link JsonParser} * Unit test for {@link JsonParser}
* *
@ -42,7 +44,7 @@ public class JsonParserTest extends TestCase {
try { try {
parser.parse("[[]"); parser.parse("[[]");
fail(); fail();
} catch (JsonParseException expected) { } } catch (JsonSyntaxException expected) { }
} }
public void testParseUnquotedStringArrayFails() { public void testParseUnquotedStringArrayFails() {