abdulrehmanz / google-gson

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

Attempting to parse an empty string returns null instead of throwing JsonSyntaxException #457

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The following test case illustrates what I think the parser should be doing in 
the event of an immediate EOF:

    @Test
    public void testEmptyStreamBehaviour()
    {
        Gson gson = new Gson();
        try
        {
            gson.fromJson(new StringReader(""), String.class);
            fail("Expected JsonSyntaxException");
        }
        catch (JsonSyntaxException e)
        {
            // Expected.
        }
    }

What really happens is that it returns null. This resulted in an incredibly 
difficult to track down bug which turned out to just be someone sending no data 
instead of sending JSON.

Original issue reported on code.google.com by trejkaz on 2 Jul 2012 at 1:49

GoogleCodeExporter commented 9 years ago
This is a sad consequence of our decision to be backwards-compatible with Gson 
1.5 which had the same behavior.

Work around this by using a TypeAdapter to do your serialization:
  gson.getAdapter(String.class).fromJson(string)

Original comment by limpbizkit on 2 Sep 2012 at 9:38