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(JsonBall.class, jsonAdapter.nullSafe());
builder.registerTypeAdapterFactory(optional);
builder.registerTypeAdapterFactory(iterable);
builder.registerTypeAdapterFactory(set);
builder.registerTypeAdapterFactory(map);
builder.registerTypeAdapterFactory(multimap);
builder.registerTypeAdapterFactory(iterable);
builder.registerTypeAdapterFactory(fluentIterable);
AnnotationConstructorNamingStrategy deserializationPolicy =

View File

@ -53,7 +53,7 @@ public class IgnoreNullFluentIterableTypeAdapterFactory implements TypeAdapterFa
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>>() {
public void write(JsonWriter out, FluentIterable<E> value) throws IOException {
out.beginArray();

View File

@ -52,7 +52,7 @@ public class IgnoreNullIterableTypeAdapterFactory implements TypeAdapterFactory
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>>() {
public void write(JsonWriter out, Iterable<E> value) throws IOException {
out.beginArray();

View File

@ -54,7 +54,7 @@ public class IgnoreNullMapTypeAdapterFactory implements TypeAdapterFactory {
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>>() {
public void write(JsonWriter out, Map<K, V> value) throws IOException {
out.beginObject();

View File

@ -51,10 +51,10 @@ public class IgnoreNullMultimapTypeAdapterFactory implements TypeAdapterFactory
Type valueType = ((ParameterizedType) type).getActualTypeArguments()[1];
TypeAdapter<?> keyAdapter = gson.getAdapter(TypeToken.get(keyType));
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>>() {
public void write(JsonWriter out, Multimap<K, V> map) throws IOException {
out.beginObject();

View File

@ -23,7 +23,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
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.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
@ -32,7 +32,7 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/**
* Eliminates null values when deserializing Sets
* Eliminates null values when deserializing Sets.
* <p/>
* 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);
}
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>>() {
public void write(JsonWriter out, Set<E> value) throws IOException {
out.beginArray();
@ -63,14 +63,15 @@ public class IgnoreNullSetTypeAdapterFactory implements TypeAdapterFactory {
}
public Set<E> read(JsonReader in) throws IOException {
Set<E> result = Sets.newLinkedHashSet();
ImmutableSet.Builder<E> result = ImmutableSet.<E> builder();
in.beginArray();
while (in.hasNext()) {
E element = elementAdapter.read(in);
if (element != null) result.add(element);
if (element != null)
result.add(element);
}
in.endArray();
return result;
return result.build();
}
}.nullSafe();
}

View File

@ -48,10 +48,10 @@ public class OptionalTypeAdapterFactory implements TypeAdapterFactory {
Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
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) {
return new TypeAdapter<Optional<E>>() {
public void write(JsonWriter out, Optional<E> value) throws IOException {