renamed GsonBuilder.factory() to GsonBuilder.registerTypeAdapterFactory()
revised GsonBuilder.registerTypeAdapter/registerTypeHierarchyAdapter to take streaming type adapters as well. Removed the typeAdapter() and typeHierarchyAdapter() methods from the public API.
This commit is contained in:
parent
d1cee8443f
commit
d1de4cf676
@ -105,30 +105,11 @@ public final class GsonBuilder {
|
||||
}
|
||||
|
||||
// TODO: nice documentation
|
||||
public GsonBuilder factory(TypeAdapter.Factory factory) {
|
||||
public GsonBuilder registerTypeAdapterFactory(TypeAdapter.Factory factory) {
|
||||
typeAdapterFactories.add(factory);
|
||||
return this;
|
||||
}
|
||||
|
||||
// TODO: nice documentation
|
||||
public <T> GsonBuilder typeAdapter(final Class<T> type, final TypeAdapter<T> typeAdapter) {
|
||||
typeAdapterFactories.add(TypeAdapters.newFactory(type, typeAdapter));
|
||||
return this;
|
||||
}
|
||||
|
||||
// TODO: nice documentation
|
||||
// TODO: accept a Type instead of a TypeToken? It's less typesafe but more Gson-like
|
||||
public <T> GsonBuilder typeAdapter(TypeToken<T> type, TypeAdapter<T> typeAdapter) {
|
||||
typeAdapterFactories.add(TypeAdapters.newFactory(type, typeAdapter));
|
||||
return this;
|
||||
}
|
||||
|
||||
// TODO: nice documentation
|
||||
public <T> GsonBuilder typeHierarchyAdapter(Class<T> type, TypeAdapter<T> typeAdapter) {
|
||||
typeAdapterFactories.add(TypeAdapters.newTypeHierarchyFactory(type, typeAdapter));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures Gson to enable versioning support.
|
||||
*
|
||||
@ -476,11 +457,12 @@ public final class GsonBuilder {
|
||||
* {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
|
||||
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
|
||||
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
|
||||
|| typeAdapter instanceof JsonDeserializer<?>
|
||||
|| typeAdapter instanceof InstanceCreator<?>
|
||||
|| typeAdapter instanceof TypeAdapter.Factory);
|
||||
|| typeAdapter instanceof TypeAdapter<?>);
|
||||
if (Primitives.isPrimitive(type) || Primitives.isWrapperType(type)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Cannot register type adapters for " + type);
|
||||
@ -492,12 +474,18 @@ public final class GsonBuilder {
|
||||
TypeToken<?> typeToken = TypeToken.get(type);
|
||||
typeAdapterFactories.add(new TreeTypeAdapter.SingleTypeFactory(typeToken, typeAdapter));
|
||||
}
|
||||
if (typeAdapter instanceof TypeAdapter.Factory) {
|
||||
typeAdapterFactories.add((TypeAdapter.Factory) typeAdapter);
|
||||
if (typeAdapter instanceof TypeAdapter<?>) {
|
||||
typeAdapter(TypeToken.get(type), (TypeAdapter)typeAdapter);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
// TODO: inline this method?
|
||||
private <T> GsonBuilder typeAdapter(TypeToken<T> type, TypeAdapter<T> typeAdapter) {
|
||||
typeAdapterFactories.add(TypeAdapters.newFactory(type, typeAdapter));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures Gson to use a custom {@link InstanceCreator} for the specified type. If an instance
|
||||
* creator was previously registered for the specified class, it is overwritten. Since this method
|
||||
@ -532,9 +520,11 @@ public final class GsonBuilder {
|
||||
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
|
||||
* @since 1.7
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
|
||||
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
|
||||
|| typeAdapter instanceof JsonDeserializer<?> || typeAdapter instanceof InstanceCreator<?>);
|
||||
|| typeAdapter instanceof JsonDeserializer<?> || typeAdapter instanceof InstanceCreator<?>
|
||||
|| typeAdapter instanceof TypeAdapter<?>);
|
||||
if (typeAdapter instanceof InstanceCreator<?>) {
|
||||
registerInstanceCreatorForTypeHierarchy(baseType, (InstanceCreator<?>) typeAdapter);
|
||||
}
|
||||
@ -544,6 +534,15 @@ public final class GsonBuilder {
|
||||
if (typeAdapter instanceof JsonDeserializer<?>) {
|
||||
registerDeserializerForTypeHierarchy(baseType, (JsonDeserializer<?>) typeAdapter);
|
||||
}
|
||||
if (typeAdapter instanceof TypeAdapter<?>) {
|
||||
typeHierarchyAdapter(baseType, (TypeAdapter)typeAdapter);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
// TODO: inline this method?
|
||||
private <T> GsonBuilder typeHierarchyAdapter(Class<T> type, TypeAdapter<T> typeAdapter) {
|
||||
typeAdapterFactories.add(TypeAdapters.newTypeHierarchyFactory(type, typeAdapter));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -102,7 +102,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
||||
writer.value(value.name);
|
||||
}
|
||||
};
|
||||
miniGson = new GsonBuilder().typeAdapter(Person.class, personNameAdapter).create();
|
||||
miniGson = new GsonBuilder().registerTypeAdapter(Person.class, personNameAdapter).create();
|
||||
truckAdapter = miniGson.getAdapter(Truck.class);
|
||||
}
|
||||
|
||||
|
@ -58,14 +58,14 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
||||
public void testSerializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Foo.class, FOO_SERIALIZER)
|
||||
.typeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||
.registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||
.create();
|
||||
assertEquals("\"foo (via FOO_SERIALIZER)\"", gson.toJson(new Foo("foo")));
|
||||
}
|
||||
|
||||
public void testSerializeStreamingTypeAdapterFollowedByNonstreamingTypeAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.typeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||
.registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||
.registerTypeAdapter(Foo.class, FOO_SERIALIZER)
|
||||
.create();
|
||||
assertEquals("\"foo (via FOO_TYPE_ADAPTER)\"", gson.toJson(new Foo("foo")));
|
||||
@ -74,14 +74,14 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
||||
public void testDeserializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Foo.class, FOO_DESERIALIZER)
|
||||
.typeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||
.registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||
.create();
|
||||
assertEquals("foo (via FOO_DESERIALIZER)", gson.fromJson("foo", Foo.class).name);
|
||||
}
|
||||
|
||||
public void testDeserializeStreamingTypeAdapterFollowedByNonstreamingTypeAdapter() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.typeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||
.registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||
.registerTypeAdapter(Foo.class, FOO_DESERIALIZER)
|
||||
.create();
|
||||
assertEquals("foo (via FOO_TYPE_ADAPTER)", gson.fromJson("foo", Foo.class).name);
|
||||
|
Loading…
Reference in New Issue
Block a user