From 34d7521d9581b025c23bd5e4880a1b3a687b3da0 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Mon, 28 Mar 2016 14:46:02 -0700 Subject: [PATCH] moved the JsonSerializationContext/JsonDeserializationContext fields to where they are used. Also moved TreeTypeAdapter to internal.bind package for potential use in JsonAdapterAnnotationFactory. --- gson/src/main/java/com/google/gson/Gson.java | 16 --------- .../java/com/google/gson/GsonBuilder.java | 1 + .../{ => internal/bind}/TreeTypeAdapter.java | 36 +++++++++++++++---- 3 files changed, 31 insertions(+), 22 deletions(-) rename gson/src/main/java/com/google/gson/{ => internal/bind}/TreeTypeAdapter.java (79%) diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index a335d870..8910cbc4 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -133,22 +133,6 @@ public final class Gson { private final boolean prettyPrinting; private final boolean lenient; - final JsonDeserializationContext deserializationContext = new JsonDeserializationContext() { - @SuppressWarnings("unchecked") - @Override public T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { - return (T) fromJson(json, typeOfT); - } - }; - - final JsonSerializationContext serializationContext = new JsonSerializationContext() { - @Override public JsonElement serialize(Object src) { - return toJsonTree(src); - } - @Override public JsonElement serialize(Object src, Type typeOfSrc) { - return toJsonTree(src, typeOfSrc); - } - }; - /** * Constructs a Gson object with default configuration. The default configuration has the * following settings: diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index b258e22f..0c917d46 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -29,6 +29,7 @@ import java.util.Map; import com.google.gson.internal.$Gson$Preconditions; import com.google.gson.internal.Excluder; +import com.google.gson.internal.bind.TreeTypeAdapter; import com.google.gson.internal.bind.TypeAdapters; import com.google.gson.reflect.TypeToken; diff --git a/gson/src/main/java/com/google/gson/TreeTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java similarity index 79% rename from gson/src/main/java/com/google/gson/TreeTypeAdapter.java rename to gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java index 5b7305ed..e13e3489 100644 --- a/gson/src/main/java/com/google/gson/TreeTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java @@ -14,31 +14,42 @@ * limitations under the License. */ -package com.google.gson; +package com.google.gson.internal.bind; +import com.google.gson.Gson; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.internal.$Gson$Preconditions; import com.google.gson.internal.Streams; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import java.io.IOException; +import java.lang.reflect.Type; /** * Adapts a Gson 1.x tree-style adapter as a streaming TypeAdapter. Since the * tree adapter may be serialization-only or deserialization-only, this class * has a facility to lookup a delegate type adapter on demand. */ -final class TreeTypeAdapter extends TypeAdapter { +public final class TreeTypeAdapter extends TypeAdapter { private final JsonSerializer serializer; private final JsonDeserializer deserializer; private final Gson gson; private final TypeToken typeToken; private final TypeAdapterFactory skipPast; + private final GsonContextImpl context = new GsonContextImpl(); /** The delegate is lazily created because it may not be needed, and creating it may fail. */ private TypeAdapter delegate; - TreeTypeAdapter(JsonSerializer serializer, JsonDeserializer deserializer, + public TreeTypeAdapter(JsonSerializer serializer, JsonDeserializer deserializer, Gson gson, TypeToken typeToken, TypeAdapterFactory skipPast) { this.serializer = serializer; this.deserializer = deserializer; @@ -55,7 +66,7 @@ final class TreeTypeAdapter extends TypeAdapter { if (value.isJsonNull()) { return null; } - return deserializer.deserialize(value, typeToken.getType(), gson.deserializationContext); + return deserializer.deserialize(value, typeToken.getType(), context); } @Override public void write(JsonWriter out, T value) throws IOException { @@ -67,7 +78,7 @@ final class TreeTypeAdapter extends TypeAdapter { out.nullValue(); return; } - JsonElement tree = serializer.serialize(value, typeToken.getType(), gson.serializationContext); + JsonElement tree = serializer.serialize(value, typeToken.getType(), context); Streams.write(tree, out); } @@ -105,7 +116,7 @@ final class TreeTypeAdapter extends TypeAdapter { return new SingleTypeFactory(typeAdapter, null, false, hierarchyType); } - private static class SingleTypeFactory implements TypeAdapterFactory { + private static final class SingleTypeFactory implements TypeAdapterFactory { private final TypeToken exactType; private final boolean matchRawType; private final Class hierarchyType; @@ -138,4 +149,17 @@ final class TreeTypeAdapter extends TypeAdapter { : null; } } + + private final class GsonContextImpl implements JsonSerializationContext, JsonDeserializationContext { + @Override public JsonElement serialize(Object src) { + return gson.toJsonTree(src); + } + @Override public JsonElement serialize(Object src, Type typeOfSrc) { + return gson.toJsonTree(src, typeOfSrc); + } + @SuppressWarnings("unchecked") + @Override public R deserialize(JsonElement json, Type typeOfT) throws JsonParseException { + return (R) gson.fromJson(json, typeOfT); + } + }; }