Gson反序列化泛型类型适配器的基类

3

我有以下类:

public class Kit {
    private String name;
    private int num;
}

我有一个类,它扩展了Kit并具有额外的功能:

public class ExtendedKit extends Kit {
    private String extraProperty;
}

使用Gson,我希望能够反序列化这两个类以及其他不同类型的类,而无需为它们创建一堆类型适配器,因为它们都具有相同的Json结构:

{
    "type": "com.driima.test.ExtendedKit",
    "properties": {
        "name": "An Extended Kit",
        "num": 124,
        "extra_property": "An extra property"
    }
}

这将被传递到我的GsonBuilder注册的以下类型适配器中:

public class GenericAdapter<T> implements JsonDeserializer<T> {
    @Override
    public T deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
        final JsonObject object = json.getAsJsonObject();
        String classType = object.get("type").getAsString();
        JsonElement element = object.get("properties");

        try {
            return context.deserialize(element, Class.forName(classType));
        } catch (ClassNotFoundException e) {
            throw new JsonParseException("Unknown element type: " + type, e);
        }
    }
}

情况是这样的,这对于ExtendedKit有效,但如果我想只反序列化一个没有extraProperty的Kit,它就不起作用了,因为它会在尝试在属性对象上调用context.deserialize()时导致NullPointerException。有什么办法可以解决这个问题吗?


这是我正在使用的GsonBuilder的代码:

private static final GsonBuilder GSON_BUILDER = new GsonBuilder()
        .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
        .registerTypeAdapterFactory(new PostProcessExecutor())
        .registerTypeAdapter(Kit.class, new GenericAdapter<Kit>());

注意:添加PostProcessExecutor是为了让我能够对任何可以进行后处理的反序列化对象应用后处理。这里有一篇文章(链接)帮助我实现了该功能。

这句话的意思是,如果我只想反序列化一个 Kit 而不需要额外的属性,那么是否意味着你提供了没有额外属性且包含 "type": "com.driima.test.Kit" 的 Json? - pirho
是的,这是正确的。抱歉在我的问题中没有澄清。我正在使用那种类型。 - driima
2个回答

1

我认为在这里使用JsonDeserializer不是一个好的选择:

  • 你需要在GsonBuilder中绑定每个类型到Gson实例,这种方法容易出错,或者使用registerTypeHierarchyAdapter
  • 对于后者,你可能会遇到无限递归的问题(如果我没有错的话:因为上下文只提供了反序列化同一类型实例的机制)。

以下类型适配器工厂可以克服上述限制:

final class PolymorphicTypeAdapterFactory
        implements TypeAdapterFactory {

    // Let's not hard-code `Kit.class` here and let a user pick up types at a call-site
    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) ) {
            // Something we cannot handle? Try pick the next best type adapter factory
            return null;
        }
        // This is what JsonDeserializer fails at:
        final TypeAdapter<T> writeTypeAdapter = gson.getDelegateAdapter(this, typeToken);
        // Despite it's possible to use the above type adapter for both read and write, what if the `type` property points to another class?
        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;
        }

        // Since constructors are meant only to assign parameters to fields, encapsulate the null-safety handling in the factory method
        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();
            // For simplicity's sake, let's assume that the class property `type` always precedes the `properties` property
            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 并没有像你的例子那样将 typeproperties 分开。
P.S. 请注意,这个类型适配器工厂远非真正的通用:它不能处理 类型(类是类型的一个特例)、泛型 类型等。如果感兴趣,但不过度工程化,您可以参考我的解决方案,使用带有其参数化的类型来进行编码,其中使用 Type 实例 object serialization mechanism(太神秘和与具体平台紧密绑定)或使用类型和泛型类型符号 parsing using JParsec 进行解析(两个链接都指向俄语 StackExchange 网站)。

这太棒了。它完全符合我的要求,我仍然可以使用我的PostProcessExecutor。但是我正在努力解决一个小问题。我有两种后处理方式——第一种只是使用空的postProcess()方法进行后处理。如果我不需要任何其他信息,那么这很好,但在某些情况下,我的json中有一些数据,我不想暂时存储,而是想评估,所以我有另一个postProcess方法,它将“属性”对象从json中传递过来。有没有办法让我获得相同的功能?postProcess(JsonObject properties); - driima
没关系 - 我刚刚创建了一个额外的类型适配器工厂来处理这个。再次感谢您的帮助! - driima
我在将生成的 JSON 反序列化为实际对象时遇到了一些问题。 - Noushad

0

我认为你的适配器存在问题,它实际上从未针对ExtendedKit进行调用,而只是针对Kit。所以这就是为什么它能够与ExtendedKit正常工作的原因,我猜测。无论如何,Gson默认情况下无法处理泛型,因为类型擦除。

不管怎样:最好和清晰的做法是声明要反序列化的对象与Json的呈现方式一致,因为通常可以减少适配器等逻辑编码的编写...

我建议您为Kit声明一个包装类,例如:

@Getter
@AllArgsConstructor
public class KitWrapper {
    private String type;
    @SerializedName("properties") // 
    private Kit kit;
}

这个可以使用TypeAdapter更容易地反序列化:

@Slf4j
public class KitWrapperAdapter implements JsonDeserializer<KitWrapper>  {
    @Override
    public KitWrapper deserialize(JsonElement json, Type typeOfT,
                   JsonDeserializationContext context)
            throws JsonParseException {
        try {
            @SuppressWarnings("unchecked")
            Class<? extends Kit> classKit =
                    (Class<? extends Kit>)Class.forName(json.getAsJsonObject()
                         .get("type").getAsString() );
            JsonElement jeProperties = json.getAsJsonObject().get("properties");
            Kit kit = context.deserialize(jeProperties, classKit);
            // Not needed to parse anymore, new KitWrapper can be created
            // with this information
            return new KitWrapper(classKit.getName(), kit);

        } catch (Exception e) {
            log.error("{}", e.toString());
            return null;
        }
    }
}

因此,通过以下方式注册此适配器并获取套件:

Kit kit = kitWrapper.getKit();

正如Lyubomyr Shaydariv他的回答中提到的,您的情况几乎适合使用RunTimeAdapterFactory。只是似乎type应该是反序列化对象的属性,而不是顶级属性和实际对象作为较低级别的属性。换句话说,如果您可以相应地更改您的Json和Kit

{
    "type": "com.driima.test.ExtendedKit",
    "name": "An Extended Kit",
    "num": 124,
    "extra_property": "An extra property"
}

如果它能正常工作,那么您可能会对如何将Gson extras添加到Android项目中?感兴趣(即使不是Maven或Gradle用户)。

但如果不能,则建议使用包装类。

然而,正如您所看到的,自己编写代码也并不是什么大问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接