make it possible to extend gson type adapters

This commit is contained in:
Adrian Cole 2012-09-16 23:38:35 -07:00
parent 54ec689019
commit 0be04f7952
7 changed files with 16 additions and 15 deletions

View File

@ -102,10 +102,10 @@ public class GsonModule extends AbstractModule {
builder.registerTypeAdapter(byte[].class, byteArrayAdapter.nullSafe()); builder.registerTypeAdapter(byte[].class, byteArrayAdapter.nullSafe());
builder.registerTypeAdapter(JsonBall.class, jsonAdapter.nullSafe()); builder.registerTypeAdapter(JsonBall.class, jsonAdapter.nullSafe());
builder.registerTypeAdapterFactory(optional); builder.registerTypeAdapterFactory(optional);
builder.registerTypeAdapterFactory(iterable);
builder.registerTypeAdapterFactory(set); builder.registerTypeAdapterFactory(set);
builder.registerTypeAdapterFactory(map); builder.registerTypeAdapterFactory(map);
builder.registerTypeAdapterFactory(multimap); builder.registerTypeAdapterFactory(multimap);
builder.registerTypeAdapterFactory(iterable);
builder.registerTypeAdapterFactory(fluentIterable); builder.registerTypeAdapterFactory(fluentIterable);
AnnotationConstructorNamingStrategy deserializationPolicy = AnnotationConstructorNamingStrategy deserializationPolicy =

View File

@ -53,7 +53,7 @@ public class IgnoreNullFluentIterableTypeAdapterFactory implements TypeAdapterFa
return (TypeAdapter<T>) newFluentIterableAdapter(elementAdapter); return (TypeAdapter<T>) newFluentIterableAdapter(elementAdapter);
} }
private <E> TypeAdapter<FluentIterable<E>> newFluentIterableAdapter(final TypeAdapter<E> elementAdapter) { protected <E> TypeAdapter<FluentIterable<E>> newFluentIterableAdapter(final TypeAdapter<E> elementAdapter) {
return new TypeAdapter<FluentIterable<E>>() { return new TypeAdapter<FluentIterable<E>>() {
public void write(JsonWriter out, FluentIterable<E> value) throws IOException { public void write(JsonWriter out, FluentIterable<E> value) throws IOException {
out.beginArray(); out.beginArray();

View File

@ -52,7 +52,7 @@ public class IgnoreNullIterableTypeAdapterFactory implements TypeAdapterFactory
return (TypeAdapter<T>) newIterableAdapter(elementAdapter); return (TypeAdapter<T>) newIterableAdapter(elementAdapter);
} }
private <E> TypeAdapter<Iterable<E>> newIterableAdapter(final TypeAdapter<E> elementAdapter) { protected <E> TypeAdapter<Iterable<E>> newIterableAdapter(final TypeAdapter<E> elementAdapter) {
return new TypeAdapter<Iterable<E>>() { return new TypeAdapter<Iterable<E>>() {
public void write(JsonWriter out, Iterable<E> value) throws IOException { public void write(JsonWriter out, Iterable<E> value) throws IOException {
out.beginArray(); out.beginArray();
@ -63,7 +63,7 @@ public class IgnoreNullIterableTypeAdapterFactory implements TypeAdapterFactory
} }
public Iterable<E> read(JsonReader in) throws IOException { public Iterable<E> read(JsonReader in) throws IOException {
in.beginArray(); in.beginArray();
Builder<E> builder = ImmutableList.<E>builder(); Builder<E> builder = ImmutableList.<E>builder();
while (in.hasNext()) { while (in.hasNext()) {
E element = elementAdapter.read(in); E element = elementAdapter.read(in);

View File

@ -54,7 +54,7 @@ public class IgnoreNullMapTypeAdapterFactory implements TypeAdapterFactory {
return (TypeAdapter<T>) newMapAdapter(keyAdapter, valueAdapter); return (TypeAdapter<T>) newMapAdapter(keyAdapter, valueAdapter);
} }
private <K,V> TypeAdapter<Map<K, V>> newMapAdapter(final TypeAdapter<K> keyAdapter, final TypeAdapter<V> valueAdapter) { protected <K,V> TypeAdapter<Map<K, V>> newMapAdapter(final TypeAdapter<K> keyAdapter, final TypeAdapter<V> valueAdapter) {
return new TypeAdapter<Map<K, V>>() { return new TypeAdapter<Map<K, V>>() {
public void write(JsonWriter out, Map<K, V> value) throws IOException { public void write(JsonWriter out, Map<K, V> value) throws IOException {
out.beginObject(); out.beginObject();

View File

@ -51,10 +51,10 @@ public class IgnoreNullMultimapTypeAdapterFactory implements TypeAdapterFactory
Type valueType = ((ParameterizedType) type).getActualTypeArguments()[1]; Type valueType = ((ParameterizedType) type).getActualTypeArguments()[1];
TypeAdapter<?> keyAdapter = gson.getAdapter(TypeToken.get(keyType)); TypeAdapter<?> keyAdapter = gson.getAdapter(TypeToken.get(keyType));
TypeAdapter<?> valueAdapter = gson.getAdapter(TypeToken.get(valueType)); TypeAdapter<?> valueAdapter = gson.getAdapter(TypeToken.get(valueType));
return (TypeAdapter<T>) newMapAdapter(keyAdapter, valueAdapter); return (TypeAdapter<T>) newMultimapAdapter(keyAdapter, valueAdapter);
} }
private <K,V> TypeAdapter<Multimap<K, V>> newMapAdapter(final TypeAdapter<K> keyAdapter, final TypeAdapter<V> valueAdapter) { protected <K,V> TypeAdapter<Multimap<K, V>> newMultimapAdapter(final TypeAdapter<K> keyAdapter, final TypeAdapter<V> valueAdapter) {
return new TypeAdapter<Multimap<K, V>>() { return new TypeAdapter<Multimap<K, V>>() {
public void write(JsonWriter out, Multimap<K, V> map) throws IOException { public void write(JsonWriter out, Multimap<K, V> map) throws IOException {
out.beginObject(); out.beginObject();

View File

@ -23,7 +23,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Set; import java.util.Set;
import com.google.common.collect.Sets; import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory; import com.google.gson.TypeAdapterFactory;
@ -32,7 +32,7 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
/** /**
* Eliminates null values when deserializing Sets * Eliminates null values when deserializing Sets.
* <p/> * <p/>
* Treats [null] as the empty set; [A, null] as [A]; etc. * Treats [null] as the empty set; [A, null] as [A]; etc.
* *
@ -52,7 +52,7 @@ public class IgnoreNullSetTypeAdapterFactory implements TypeAdapterFactory {
return (TypeAdapter<T>) newSetAdapter(elementAdapter); return (TypeAdapter<T>) newSetAdapter(elementAdapter);
} }
private <E> TypeAdapter<Set<E>> newSetAdapter(final TypeAdapter<E> elementAdapter) { protected <E> TypeAdapter<Set<E>> newSetAdapter(final TypeAdapter<E> elementAdapter) {
return new TypeAdapter<Set<E>>() { return new TypeAdapter<Set<E>>() {
public void write(JsonWriter out, Set<E> value) throws IOException { public void write(JsonWriter out, Set<E> value) throws IOException {
out.beginArray(); out.beginArray();
@ -63,14 +63,15 @@ public class IgnoreNullSetTypeAdapterFactory implements TypeAdapterFactory {
} }
public Set<E> read(JsonReader in) throws IOException { public Set<E> read(JsonReader in) throws IOException {
Set<E> result = Sets.newLinkedHashSet(); ImmutableSet.Builder<E> result = ImmutableSet.<E> builder();
in.beginArray(); in.beginArray();
while (in.hasNext()) { while (in.hasNext()) {
E element = elementAdapter.read(in); E element = elementAdapter.read(in);
if (element != null) result.add(element); if (element != null)
result.add(element);
} }
in.endArray(); in.endArray();
return result; return result.build();
} }
}.nullSafe(); }.nullSafe();
} }

View File

@ -48,10 +48,10 @@ public class OptionalTypeAdapterFactory implements TypeAdapterFactory {
Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0]; Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
TypeAdapter<?> elementAdapter = gson.getAdapter(TypeToken.get(elementType)); TypeAdapter<?> elementAdapter = gson.getAdapter(TypeToken.get(elementType));
return (TypeAdapter<T>) newMultisetAdapter(elementAdapter); return (TypeAdapter<T>) newOptionalAdapter(elementAdapter);
} }
private <E> TypeAdapter<Optional<E>> newMultisetAdapter( protected <E> TypeAdapter<Optional<E>> newOptionalAdapter(
final TypeAdapter<E> elementAdapter) { final TypeAdapter<E> elementAdapter) {
return new TypeAdapter<Optional<E>>() { return new TypeAdapter<Optional<E>>() {
public void write(JsonWriter out, Optional<E> value) throws IOException { public void write(JsonWriter out, Optional<E> value) throws IOException {