From 1c5f5132d8cc9b3578070b8c570ccf8d8d2e8f8c Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Wed, 24 Nov 2010 23:22:13 +0000 Subject: [PATCH] Revert most of r677: Required strings to be quoted even in lenient mode. As far as Inderjeet and I can tell, this is consistent with Gson 1.5. --- gson/src/main/java/com/google/gson/Gson.java | 20 +++++++++++++++++-- .../main/java/com/google/gson/JsonParser.java | 20 ++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 186425c6..7724bf94 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -17,8 +17,9 @@ package com.google.gson; import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; - +import com.google.gson.stream.MalformedJsonException; import java.io.IOException; import java.io.Reader; import java.io.StringReader; @@ -455,6 +456,7 @@ public final class Gson { public T fromJson(Reader json, Class classOfT) throws JsonSyntaxException, JsonIOException { JsonReader jsonReader = new JsonReader(json); Object object = fromJson(jsonReader, classOfT); + assertFullConsumption(object, jsonReader); return Primitives.wrap(classOfT).cast(object); } @@ -479,7 +481,21 @@ public final class Gson { */ public T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException { JsonReader jsonReader = new JsonReader(json); - return this.fromJson(jsonReader, typeOfT); + T object = this.fromJson(jsonReader, typeOfT); + assertFullConsumption(object, jsonReader); + return object; + } + + private static void assertFullConsumption(Object obj, JsonReader reader) { + try { + if (obj != null && reader.peek() != JsonToken.END_DOCUMENT) { + throw new JsonIOException("JSON document was not fully consumed."); + } + } catch (MalformedJsonException e) { + throw new JsonSyntaxException(e); + } catch (IOException e) { + throw new JsonIOException(e); + } } /** diff --git a/gson/src/main/java/com/google/gson/JsonParser.java b/gson/src/main/java/com/google/gson/JsonParser.java index 584ef7f0..a2eef8ad 100755 --- a/gson/src/main/java/com/google/gson/JsonParser.java +++ b/gson/src/main/java/com/google/gson/JsonParser.java @@ -16,8 +16,10 @@ package com.google.gson; 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; @@ -51,8 +53,20 @@ public final class JsonParser { * @since 1.3 */ public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException { - JsonReader jsonReader = new JsonReader(json); - return parse(jsonReader); + try { + JsonReader jsonReader = new JsonReader(json); + JsonElement element = parse(jsonReader); + if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) { + throw new JsonSyntaxException("Did not consume the entire document."); + } + return element; + } catch (MalformedJsonException e) { + throw new JsonSyntaxException(e); + } catch (IOException e) { + throw new JsonIOException(e); + } catch (NumberFormatException e) { + throw new JsonSyntaxException(e); + } } /**