Support type exclusion strategies.

This commit is contained in:
Jesse Wilson 2011-08-05 00:25:49 +00:00
parent e9a971f680
commit fc99556f22
2 changed files with 25 additions and 2 deletions

View File

@ -164,8 +164,8 @@ public final class Gson {
false, LongSerializationPolicy.DEFAULT);
}
Gson(ExclusionStrategy deserializationExclusionStrategy,
ExclusionStrategy serializationExclusionStrategy,
Gson(final ExclusionStrategy deserializationExclusionStrategy,
final ExclusionStrategy serializationExclusionStrategy,
final FieldNamingStrategy2 fieldNamingPolicy,
final MappedObjectConstructor objectConstructor, boolean serializeNulls,
final ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
@ -208,6 +208,18 @@ public final class Gson {
}
};
TypeAdapter.Factory excludedTypeFactory = new TypeAdapter.Factory() {
@Override public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> type) {
Class<?> rawType = type.getRawType();
if (serializationExclusionStrategy.shouldSkipClass(rawType)
|| deserializationExclusionStrategy.shouldSkipClass(rawType)) {
return TypeAdapters.EXCLUDED_TYPE_ADAPTER;
} else {
return null;
}
}
};
MiniGson.Builder builder = new MiniGson.Builder()
.withoutDefaultFactories()
.factory(TypeAdapters.BOOLEAN_FACTORY)
@ -219,6 +231,7 @@ public final class Gson {
.factory(TypeAdapters.newFactory(long.class, Long.class,
longAdapter(longSerializationPolicy)))
.factory(TypeAdapters.STRING_FACTORY)
.factory(excludedTypeFactory)
.factory(new GsonToMiniGsonTypeAdapter(serializers, deserializers, serializeNulls))
.factory(CollectionTypeAdapter.FACTORY)
.factory(StringToValueMapTypeAdapter.FACTORY)

View File

@ -98,6 +98,16 @@ public final class TypeAdapters {
public static final TypeAdapter.Factory STRING_FACTORY = newFactory(String.class, STRING);
public static final TypeAdapter EXCLUDED_TYPE_ADAPTER = new TypeAdapter<Object>() {
@Override public Object read(JsonReader reader) throws IOException {
reader.skipValue();
return null;
}
@Override public void write(JsonWriter writer, Object value) throws IOException {
writer.nullValue();
}
};
public static <T> TypeAdapter.Factory newFactory(
final TypeToken<T> type, final TypeAdapter<T> typeAdapter) {
return new TypeAdapter.Factory() {