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
|
// TODO: nice documentation
|
||||||
public GsonBuilder factory(TypeAdapter.Factory factory) {
|
public GsonBuilder registerTypeAdapterFactory(TypeAdapter.Factory factory) {
|
||||||
typeAdapterFactories.add(factory);
|
typeAdapterFactories.add(factory);
|
||||||
return this;
|
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.
|
* Configures Gson to enable versioning support.
|
||||||
*
|
*
|
||||||
|
@ -476,11 +457,12 @@ public final class GsonBuilder {
|
||||||
* {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
|
* {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
|
||||||
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
|
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
|
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
|
||||||
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
|
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
|
||||||
|| typeAdapter instanceof JsonDeserializer<?>
|
|| typeAdapter instanceof JsonDeserializer<?>
|
||||||
|| typeAdapter instanceof InstanceCreator<?>
|
|| typeAdapter instanceof InstanceCreator<?>
|
||||||
|| typeAdapter instanceof TypeAdapter.Factory);
|
|| typeAdapter instanceof TypeAdapter<?>);
|
||||||
if (Primitives.isPrimitive(type) || Primitives.isWrapperType(type)) {
|
if (Primitives.isPrimitive(type) || Primitives.isWrapperType(type)) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Cannot register type adapters for " + type);
|
"Cannot register type adapters for " + type);
|
||||||
|
@ -492,12 +474,18 @@ public final class GsonBuilder {
|
||||||
TypeToken<?> typeToken = TypeToken.get(type);
|
TypeToken<?> typeToken = TypeToken.get(type);
|
||||||
typeAdapterFactories.add(new TreeTypeAdapter.SingleTypeFactory(typeToken, typeAdapter));
|
typeAdapterFactories.add(new TreeTypeAdapter.SingleTypeFactory(typeToken, typeAdapter));
|
||||||
}
|
}
|
||||||
if (typeAdapter instanceof TypeAdapter.Factory) {
|
if (typeAdapter instanceof TypeAdapter<?>) {
|
||||||
typeAdapterFactories.add((TypeAdapter.Factory) typeAdapter);
|
typeAdapter(TypeToken.get(type), (TypeAdapter)typeAdapter);
|
||||||
}
|
}
|
||||||
return this;
|
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
|
* 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
|
* 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
|
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
|
||||||
* @since 1.7
|
* @since 1.7
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
|
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
|
||||||
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
|
$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<?>) {
|
if (typeAdapter instanceof InstanceCreator<?>) {
|
||||||
registerInstanceCreatorForTypeHierarchy(baseType, (InstanceCreator<?>) typeAdapter);
|
registerInstanceCreatorForTypeHierarchy(baseType, (InstanceCreator<?>) typeAdapter);
|
||||||
}
|
}
|
||||||
|
@ -544,6 +534,15 @@ public final class GsonBuilder {
|
||||||
if (typeAdapter instanceof JsonDeserializer<?>) {
|
if (typeAdapter instanceof JsonDeserializer<?>) {
|
||||||
registerDeserializerForTypeHierarchy(baseType, (JsonDeserializer<?>) typeAdapter);
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -102,7 +102,7 @@ public final class StreamingTypeAdaptersTest extends TestCase {
|
||||||
writer.value(value.name);
|
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);
|
truckAdapter = miniGson.getAdapter(Truck.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,14 +58,14 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
||||||
public void testSerializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() {
|
public void testSerializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() {
|
||||||
Gson gson = new GsonBuilder()
|
Gson gson = new GsonBuilder()
|
||||||
.registerTypeAdapter(Foo.class, FOO_SERIALIZER)
|
.registerTypeAdapter(Foo.class, FOO_SERIALIZER)
|
||||||
.typeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
.registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||||
.create();
|
.create();
|
||||||
assertEquals("\"foo (via FOO_SERIALIZER)\"", gson.toJson(new Foo("foo")));
|
assertEquals("\"foo (via FOO_SERIALIZER)\"", gson.toJson(new Foo("foo")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSerializeStreamingTypeAdapterFollowedByNonstreamingTypeAdapter() {
|
public void testSerializeStreamingTypeAdapterFollowedByNonstreamingTypeAdapter() {
|
||||||
Gson gson = new GsonBuilder()
|
Gson gson = new GsonBuilder()
|
||||||
.typeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
.registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||||
.registerTypeAdapter(Foo.class, FOO_SERIALIZER)
|
.registerTypeAdapter(Foo.class, FOO_SERIALIZER)
|
||||||
.create();
|
.create();
|
||||||
assertEquals("\"foo (via FOO_TYPE_ADAPTER)\"", gson.toJson(new Foo("foo")));
|
assertEquals("\"foo (via FOO_TYPE_ADAPTER)\"", gson.toJson(new Foo("foo")));
|
||||||
|
@ -74,14 +74,14 @@ public final class TypeAdapterPrecedenceTest extends TestCase {
|
||||||
public void testDeserializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() {
|
public void testDeserializeNonstreamingTypeAdapterFollowedByStreamingTypeAdapter() {
|
||||||
Gson gson = new GsonBuilder()
|
Gson gson = new GsonBuilder()
|
||||||
.registerTypeAdapter(Foo.class, FOO_DESERIALIZER)
|
.registerTypeAdapter(Foo.class, FOO_DESERIALIZER)
|
||||||
.typeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
.registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||||
.create();
|
.create();
|
||||||
assertEquals("foo (via FOO_DESERIALIZER)", gson.fromJson("foo", Foo.class).name);
|
assertEquals("foo (via FOO_DESERIALIZER)", gson.fromJson("foo", Foo.class).name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDeserializeStreamingTypeAdapterFollowedByNonstreamingTypeAdapter() {
|
public void testDeserializeStreamingTypeAdapterFollowedByNonstreamingTypeAdapter() {
|
||||||
Gson gson = new GsonBuilder()
|
Gson gson = new GsonBuilder()
|
||||||
.typeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
.registerTypeAdapter(Foo.class, FOO_TYPE_ADAPTER)
|
||||||
.registerTypeAdapter(Foo.class, FOO_DESERIALIZER)
|
.registerTypeAdapter(Foo.class, FOO_DESERIALIZER)
|
||||||
.create();
|
.create();
|
||||||
assertEquals("foo (via FOO_TYPE_ADAPTER)", gson.fromJson("foo", Foo.class).name);
|
assertEquals("foo (via FOO_TYPE_ADAPTER)", gson.fromJson("foo", Foo.class).name);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user