From 99801915aa324c4d07b852e23debfbcefb1a3007 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 9 Sep 2011 05:40:34 +0000 Subject: [PATCH] More code through the same fromJson path --- gson/src/main/java/com/google/gson/Gson.java | 17 ++++++----------- .../google/gson/internal/bind/TypeAdapter.java | 6 +----- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index b02db3fb..fa8c905b 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -35,6 +35,7 @@ import com.google.gson.stream.MalformedJsonException; import java.io.IOException; import java.io.Reader; +import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.lang.reflect.Field; @@ -557,14 +558,12 @@ public final class Gson { return null; } try { - TypeAdapter typeAdapter = (TypeAdapter)miniGson.getAdapter(TypeToken.get(typeOfT)); - return typeAdapter.fromJson(json); + StringReader reader = new StringReader(json); + T target = (T) fromJson(reader, typeOfT); + return target; } catch (IllegalStateException e) { // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException throw new JsonSyntaxException(e); - } catch (IOException e) { - // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException - throw new JsonSyntaxException(e); } } @@ -641,14 +640,10 @@ public final class Gson { */ @SuppressWarnings("unchecked") public T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { - if (reader == null) { - // TODO(inder): remove this null check since we didnt have it in a previously released version - return null; - } boolean oldLenient = reader.isLenient(); reader.setLenient(true); try { - TypeAdapter typeAdapter = (TypeAdapter)miniGson.getAdapter(TypeToken.get(typeOfT)); + TypeAdapter typeAdapter = (TypeAdapter) miniGson.getAdapter(TypeToken.get(typeOfT)); return typeAdapter.read(reader); } catch (IOException e) { // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException @@ -702,7 +697,7 @@ public final class Gson { if (json == null) { return null; } - return (T) miniGson.getAdapter(TypeToken.get(typeOfT)).fromJsonElement(json); + return fromJson(new StringReader(json.toString()), typeOfT); } @Override diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapter.java index 3f04234a..c246222b 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapter.java @@ -70,11 +70,7 @@ public abstract class TypeAdapter { public T fromJsonElement(JsonElement json) { try { - StringWriter stringWriter = new StringWriter(); - JsonWriter jsonWriter = new JsonWriter(stringWriter); - jsonWriter.setLenient(true); - Streams.write(json, false, jsonWriter); - JsonReader jsonReader = new JsonReader(new StringReader(stringWriter.toString())); + JsonReader jsonReader = new JsonReader(new StringReader(json.toString())); jsonReader.setLenient(true); return read(jsonReader); } catch (IOException e) {