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.
This commit is contained in:
Inderjeet Singh 2011-11-25 05:40:17 +00:00
parent 7e47f46ac1
commit ddde79c861
2 changed files with 33 additions and 6 deletions

View File

@ -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;
/**
* <p>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.
*

View File

@ -76,6 +76,6 @@ public abstract class TypeAdapter<T> {
}
public interface Factory {
<T> TypeAdapter<T> create(Gson context, TypeToken<T> type);
<T> TypeAdapter<T> create(Gson gson, TypeToken<T> type);
}
}