我认为在这里使用JsonDeserializer
不是一个好的选择:
- 你需要在
GsonBuilder
中绑定每个类型到Gson
实例,这种方法容易出错,或者使用registerTypeHierarchyAdapter
。
- 对于后者,你可能会遇到无限递归的问题(如果我没有错的话:因为上下文只提供了反序列化同一类型实例的机制)。
以下类型适配器工厂可以克服上述限制:
final class PolymorphicTypeAdapterFactory
implements TypeAdapterFactory {
private final Predicate<? super Class<?>> predicate;
private PolymorphicTypeAdapterFactory(final Predicate<? super Class<?>> predicate) {
this.predicate = predicate;
}
static TypeAdapterFactory get(final Predicate<? super Class<?>> predicate) {
return new PolymorphicTypeAdapterFactory(predicate);
}
@Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
final Class<? super T> rawClass = typeToken.getRawType();
if ( !predicate.test(rawClass) ) {
return null;
}
final TypeAdapter<T> writeTypeAdapter = gson.getDelegateAdapter(this, typeToken);
final Function<? super Class<T>, ? extends TypeAdapter<T>> readTypeAdapterResolver = actualRawClass -> {
if ( !rawClass.isAssignableFrom(actualRawClass) ) {
throw new IllegalStateException("Cannot parse as " + actualRawClass);
}
return gson.getDelegateAdapter(this, TypeToken.get(actualRawClass));
};
return PolymorphicTypeAdapter.get(rawClass, writeTypeAdapter, readTypeAdapterResolver);
}
private static final class PolymorphicTypeAdapter<T>
extends TypeAdapter<T> {
private final Class<? super T> rawClass;
private final TypeAdapter<T> writeTypeAdapter;
private final Function<? super Class<T>, ? extends TypeAdapter<T>> readTypeAdapterResolver;
private PolymorphicTypeAdapter(final Class<? super T> rawClass, final TypeAdapter<T> writeTypeAdapter,
final Function<? super Class<T>, ? extends TypeAdapter<T>> readTypeAdapterResolver) {
this.rawClass = rawClass;
this.writeTypeAdapter = writeTypeAdapter;
this.readTypeAdapterResolver = readTypeAdapterResolver;
}
private static <T> TypeAdapter<T> get(final Class<? super T> rawClass, final TypeAdapter<T> writeTypeAdapter,
final Function<? super Class<T>, ? extends TypeAdapter<T>> readTypeAdapterResolver) {
return new PolymorphicTypeAdapter<>(rawClass, writeTypeAdapter, readTypeAdapterResolver)
.nullSafe();
}
@Override
@SuppressWarnings("resource")
public void write(final JsonWriter jsonWriter, final T value)
throws IOException {
jsonWriter.beginObject();
jsonWriter.name("type");
jsonWriter.value(rawClass.getName());
jsonWriter.name("properties");
writeTypeAdapter.write(jsonWriter, value);
jsonWriter.endObject();
}
@Override
public T read(final JsonReader jsonReader)
throws IOException {
jsonReader.beginObject();
final Class<? super T> actualRawClass = readActualRawClass(jsonReader);
final T value = readValue(jsonReader, actualRawClass);
jsonReader.endObject();
return value;
}
private Class<? super T> readActualRawClass(final JsonReader jsonReader)
throws IOException {
try {
requireProperty(jsonReader, "type");
final String value = jsonReader.nextString();
@SuppressWarnings("unchecked")
final Class<? super T> actualRawClass = (Class<? super T>) Class.forName(value);
return actualRawClass;
} catch ( final ClassNotFoundException ex ) {
throw new AssertionError(ex);
}
}
private T readValue(final JsonReader jsonReader, final Class<? super T> rawClass)
throws IOException {
requireProperty(jsonReader, "properties");
@SuppressWarnings("unchecked")
final Class<T> castRawClass = (Class<T>) rawClass;
final TypeAdapter<T> readTypeAdapter = readTypeAdapterResolver.apply(castRawClass);
return readTypeAdapter.read(jsonReader);
}
private static void requireProperty(final JsonReader jsonReader, final String propertyName)
throws IOException {
final String name = jsonReader.nextName();
if ( !name.equals(propertyName) ) {
throw new JsonParseException("Unexpected property: " + name);
}
}
}
}
以下是针对您的
Kit
类的使用示例(下面的方法引用仅检查给定实际原始类是否是
Kit
的超类或后者本身是
Kit
):
private static final Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapterFactory(PolymorphicTypeAdapterFactory.get(Kit.class::isAssignableFrom))
.create();
请注意,你的问题并不是独一无二的,而且你的情况几乎已经被
RuntimeTypeAdapterFactory
覆盖,但是
RuntimeTypeAdapterFactory
并没有像你的例子那样将
type
和
properties
分开。
P.S. 请注意,这个类型适配器工厂远非真正的通用:它不能处理
类型(类是类型的一个特例)、
泛型 类型等。如果感兴趣,但不过度工程化,您可以参考我的解决方案,使用带有其参数化的类型来进行编码,其中使用
Type
实例
object serialization mechanism(太神秘和与具体平台紧密绑定)或使用类型和泛型类型符号
parsing using JParsec 进行解析(两个链接都指向俄语 StackExchange 网站)。
Kit
而不需要额外的属性,那么是否意味着你提供了没有额外属性且包含"type": "com.driima.test.Kit"
的 Json? - pirho