DonaldDu / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

EOFException in JsonReader on an empty document #330

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

  public void test() throws IOException {
    JsonReader reader = new JsonReader(new StringReader(""));
    assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  }

What is the expected output? What do you see instead?

Expected test to pass, but instead it throws an exception:

java.io.EOFException: End of input
    at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:887)
    at com.google.gson.stream.JsonReader.nextValue(JsonReader.java:787)
    at com.google.gson.stream.JsonReader.quickPeek(JsonReader.java:365)
    at com.google.gson.stream.JsonReader.peek(JsonReader.java:340)
...

What version of the product are you using? On what operating system?

GSON 1.6

Please provide any additional information below.

Interestingly, we catch EOFException in quickPeek() for the NONEMPTY_DOCUMENT 
scope, but not for the EMPTY_DOCUMENT scope.

Original issue reported on code.google.com by yan...@google.com on 2 Jun 2011 at 4:10

GoogleCodeExporter commented 9 years ago
The empty string is not a valid JSON document. So strictly speaking, this is 
working as it should. But I'm tempted to support an empty document if the 
parser is lenient.

Original comment by limpbizkit on 2 Jun 2011 at 8:55

GoogleCodeExporter commented 9 years ago
I agree that it makes sense to only support an empty string if the parser is 
lenient.  That said, for the case of a non-lenient parser, I think it would be 
a better developer experience to use syntaxError() and throw an IOException 
rather than throw an EOFException.

Original comment by yan...@google.com on 2 Jun 2011 at 9:07

GoogleCodeExporter commented 9 years ago
EOFException subclasses IOException; I think you'll catch it either way.

Original comment by limpbizkit on 29 Dec 2011 at 5:58