-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Adapter Layering Feature (Java 6 Compatible) #1473
base: main
Are you sure you want to change the base?
Changes from 15 commits
c573f03
61d8b67
8e09b88
6557382
ca51290
947e927
9f67f91
cc917ac
e0dfca0
4d1f716
27e4760
17a6b01
4f7ac53
a52ff66
96fdf48
327ef1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,22 @@ | |
|
||
package com.google.gson; | ||
|
||
import static com.google.gson.Gson.DEFAULT_COMPLEX_MAP_KEYS; | ||
import static com.google.gson.Gson.DEFAULT_ESCAPE_HTML; | ||
import static com.google.gson.Gson.DEFAULT_JSON_NON_EXECUTABLE; | ||
import static com.google.gson.Gson.DEFAULT_LENIENT; | ||
import static com.google.gson.Gson.DEFAULT_PRETTY_PRINT; | ||
import static com.google.gson.Gson.DEFAULT_SERIALIZE_NULLS; | ||
import static com.google.gson.Gson.DEFAULT_SPECIALIZE_FLOAT_VALUES; | ||
|
||
import com.google.gson.internal.$Gson$Preconditions; | ||
import com.google.gson.internal.ConstructorConstructor; | ||
import com.google.gson.internal.Excluder; | ||
import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory; | ||
import com.google.gson.internal.bind.TreeTypeAdapter; | ||
import com.google.gson.internal.bind.TypeAdapters; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import java.lang.reflect.Type; | ||
import java.sql.Timestamp; | ||
import java.text.DateFormat; | ||
|
@@ -26,21 +42,6 @@ | |
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.bind.TreeTypeAdapter; | ||
import com.google.gson.internal.bind.TypeAdapters; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
|
||
import static com.google.gson.Gson.DEFAULT_COMPLEX_MAP_KEYS; | ||
import static com.google.gson.Gson.DEFAULT_ESCAPE_HTML; | ||
import static com.google.gson.Gson.DEFAULT_JSON_NON_EXECUTABLE; | ||
import static com.google.gson.Gson.DEFAULT_LENIENT; | ||
import static com.google.gson.Gson.DEFAULT_PRETTY_PRINT; | ||
import static com.google.gson.Gson.DEFAULT_SERIALIZE_NULLS; | ||
import static com.google.gson.Gson.DEFAULT_SPECIALIZE_FLOAT_VALUES; | ||
|
||
/** | ||
* <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 | ||
|
@@ -511,6 +512,36 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { | |
return this; | ||
} | ||
|
||
/** | ||
* Configures Gson for custom serialization and deserialization with Fill-in. Takes the type | ||
* adapter, and wraps the result from the {@link TypeAdapter} into a {@link ReflectiveTypeAdapterFactory.Adapter}. Like | ||
* {@link #registerTypeAdapter(Type, Object)}, it only applies to the type specified by | ||
* the {@code type} parameter. | ||
* | ||
* @param baseType the type definition for the type adapter being registered | ||
* @param objectAdapter This object must implement the {@link TypeAdapter} class | ||
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern | ||
*/ | ||
public GsonBuilder registerTypeAdapterWithFillIn(final Type baseType, Object objectAdapter) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of this method, why not create a TypeAdapter implementation that users can extend to get fill-in features. |
||
$Gson$Preconditions.checkArgument(objectAdapter instanceof TypeAdapter<?>); | ||
final TypeAdapter<?> typeAdapter = (TypeAdapter<?>) objectAdapter; | ||
|
||
factories.add(new TypeAdapterFactory() { | ||
@Override | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
ConstructorConstructor constructorConstructor = new ConstructorConstructor(type, typeAdapter); | ||
List<Type> typeList = new ArrayList<Type>(); | ||
typeList.add(baseType); | ||
ReflectiveTypeAdapterFactory reflectiveTypeAdapterFactory = | ||
new ReflectiveTypeAdapterFactory(constructorConstructor, gson.fieldNamingStrategy, | ||
gson.excluder, gson.jsonAdapterFactory, typeList); | ||
return reflectiveTypeAdapterFactory.create(gson, type); | ||
} | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Register a factory for type adapters. Registering a factory is useful when the type | ||
* adapter needs to be configured based on the type of the field being processed. Gson | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.google.gson; | ||
|
||
import com.google.gson.stream.JsonReader; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Acts as a wrapper of the {@link InstanceCreator} interface for fill-in. Fulfills roughly the same | ||
* functionality as the {@link com.google.gson.internal.ObjectConstructorWrapper} class. It doesn't | ||
* break existing functionality by implementing {@code InstanceCreator} but when casted to this | ||
* class, it can expose a method that accepts a {@code JsonReader} as a parameter, allowing fill-in | ||
* functionality. | ||
*/ | ||
public class InstanceCreatorWrapper<T> implements InstanceCreator<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this class part of the public API? |
||
|
||
@Override | ||
public T createInstance(Type type) { | ||
return null; | ||
} | ||
|
||
/** | ||
* If not defined, defaults to returning the result of {@link #createInstance}. This method is | ||
* designed to help with creating Adapters with Fill-In. See {@link | ||
* GsonBuilder#registerTypeAdapterFactory}. | ||
* | ||
* @param type the parameterized T represented as a {@link Type}. | ||
* @param in the JsonReader from which to create the instance. | ||
* @return a default object instance of type T. | ||
*/ | ||
public T createInstance(Type type, JsonReader in) { | ||
return createInstance(type); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid frivolous changes like adding final to method parameters, imports reordering, or formatting changes.