Support type exclusion strategies.
This commit is contained in:
parent
e9a971f680
commit
fc99556f22
@ -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)
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user