From 582b0a0c9c1894f04b476ea11bc181405e61303c Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Sat, 30 Jun 2012 18:48:11 +0000 Subject: [PATCH] Fixed issue 443 by relying on Streams.parse() to return a JsonNull on empty documents and throw a JsonParseException otherwise. --- gson/src/main/java/com/google/gson/JsonParser.java | 13 ++++--------- .../main/java/com/google/gson/internal/Streams.java | 3 +++ .../test/java/com/google/gson/JsonParserTest.java | 7 +++++++ .../com/google/gson/functional/JsonParserTest.java | 7 +++++++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/gson/src/main/java/com/google/gson/JsonParser.java b/gson/src/main/java/com/google/gson/JsonParser.java index 53b58072..a8ae337b 100755 --- a/gson/src/main/java/com/google/gson/JsonParser.java +++ b/gson/src/main/java/com/google/gson/JsonParser.java @@ -15,14 +15,14 @@ */ 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.stream.JsonReader; import com.google.gson.stream.JsonToken; 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 @@ -86,11 +86,6 @@ public final class JsonParser { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError 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 { json.setLenient(lenient); } diff --git a/gson/src/main/java/com/google/gson/internal/Streams.java b/gson/src/main/java/com/google/gson/internal/Streams.java index 3ac25907..f49329d2 100644 --- a/gson/src/main/java/com/google/gson/internal/Streams.java +++ b/gson/src/main/java/com/google/gson/internal/Streams.java @@ -50,6 +50,9 @@ public final class Streams { if (isEmpty) { 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); } catch (MalformedJsonException e) { throw new JsonSyntaxException(e); diff --git a/gson/src/test/java/com/google/gson/JsonParserTest.java b/gson/src/test/java/com/google/gson/JsonParserTest.java index 30ccdf27..f3012ca3 100644 --- a/gson/src/test/java/com/google/gson/JsonParserTest.java +++ b/gson/src/test/java/com/google/gson/JsonParserTest.java @@ -38,6 +38,13 @@ public class JsonParserTest extends TestCase { parser = new JsonParser(); } + public void testParseInvalidJson() { + try { + parser.parse("[[]"); + fail(); + } catch (JsonParseException expected) { } + } + public void testParseUnquotedStringArrayFails() { JsonElement element = parser.parse("[a,b,c]"); assertEquals("a", element.getAsJsonArray().get(0).getAsString()); diff --git a/gson/src/test/java/com/google/gson/functional/JsonParserTest.java b/gson/src/test/java/com/google/gson/functional/JsonParserTest.java index dc70f07b..44f4477c 100644 --- a/gson/src/test/java/com/google/gson/functional/JsonParserTest.java +++ b/gson/src/test/java/com/google/gson/functional/JsonParserTest.java @@ -50,6 +50,13 @@ public class JsonParserTest extends TestCase { gson = new Gson(); } + public void testParseInvalidJson() { + try { + gson.fromJson("[[]", Object[].class); + fail(); + } catch (JsonSyntaxException expected) { } + } + public void testDeserializingCustomTree() { JsonObject obj = new JsonObject(); obj.addProperty("stringValue", "foo");