From ddde79c861cd2a3f62c33650698942cac1bb961c Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 25 Nov 2011 05:40:17 +0000 Subject: [PATCH] Added a deepCopy() method in GsonBuilder. This allows a web-service to create a pre-configured GsonBuilder and then for each request, create a Gson instance by adding type adapters to a copy. In TypeAdapter.Factory.create() method, using the term gson to refer to the Gson instance instead of a little ambiguous context. --- .../java/com/google/gson/GsonBuilder.java | 37 ++++++++++++++++--- .../java/com/google/gson/TypeAdapter.java | 2 +- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index e6533e4d..db9e407f 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -16,11 +16,6 @@ package com.google.gson; -import com.google.gson.internal.$Gson$Preconditions; -import com.google.gson.internal.Excluder; -import com.google.gson.internal.Primitives; -import com.google.gson.internal.bind.TypeAdapters; -import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.sql.Timestamp; import java.text.DateFormat; @@ -30,6 +25,12 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.google.gson.internal.$Gson$Preconditions; +import com.google.gson.internal.Excluder; +import com.google.gson.internal.Primitives; +import com.google.gson.internal.bind.TypeAdapters; +import com.google.gson.reflect.TypeToken; + /** *

Use this builder to construct a {@link Gson} instance when you need to set configuration * options other than the default. For {@link Gson} with default configuration, it is simpler to @@ -92,6 +93,32 @@ public final class GsonBuilder { public GsonBuilder() { } + /** + * Creates a deep copy of this instance: The collections for factories are deep copied, but the + * factories and type adapters themselves are not. + * + * @since 2.1 + */ + public GsonBuilder deepCopy() { + GsonBuilder copy = new GsonBuilder(); + copy.excluder = excluder; + copy.longSerializationPolicy = longSerializationPolicy; + copy.fieldNamingPolicy = fieldNamingPolicy; + copy.instanceCreators.putAll(instanceCreators); + copy.factories.addAll(factories); + copy.hierarchyFactories.addAll(hierarchyFactories); + copy.serializeNulls = serializeNulls; + copy.datePattern = datePattern; + copy.dateStyle = dateStyle; + copy.timeStyle = timeStyle; + copy.complexMapKeySerialization = complexMapKeySerialization; + copy.serializeSpecialFloatingPointValues = serializeSpecialFloatingPointValues; + copy.escapeHtmlChars = escapeHtmlChars; + copy.prettyPrinting = prettyPrinting; + copy.generateNonExecutableJson = generateNonExecutableJson; + return copy; + } + /** * Configures Gson to enable versioning support. * diff --git a/gson/src/main/java/com/google/gson/TypeAdapter.java b/gson/src/main/java/com/google/gson/TypeAdapter.java index 6e4bf7b5..66771399 100644 --- a/gson/src/main/java/com/google/gson/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TypeAdapter.java @@ -76,6 +76,6 @@ public abstract class TypeAdapter { } public interface Factory { - TypeAdapter create(Gson context, TypeToken type); + TypeAdapter create(Gson gson, TypeToken type); } }