GSON序列化/反序列化自定义通用集合类型 [英] GSON to serialize/deserialize to/from a custom generic collection type

查看:103
本文介绍了GSON序列化/反序列化自定义通用集合类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种配置GSON以便从定制通用集合类型序列化和反序列化的方法.

I am looking for a way to configure GSON to serialize to and deserialize from a custom generic collection type.

有问题的集合是 LibGDX'数组类型.

我找不到有关如何实现此目的的文档.有人可以帮忙吗?

I am unable to find documentation of how to achieve this. Can anyone help?

推荐答案

解决方案1:

转换为标准Java数组.由于Array实际上只是标准Java数组的包装器,因此我们不会丢失对其进行转换的数据,Gson可以轻松地为我们处理标准数组的序列化和反序列化.

Converting into standard Java array. Since Array is actually just a wrapper around standard Java array we won't lose data converting it and Gson can easily handle serializing and deserializing standard arrays for us.

Array<Human> array = new Array<Human>();
array.add(new Human("Jack"));
array.add(new Human("Tom"));
array.add(new Human("Mel"));
array.add(new Human("Anne"));

Gdx.app.log("JSON", "To json");
for (Human human : array) {
    Gdx.app.log("Human", human.name);
}

Gson gson = new Gson();
String json = gson.toJson(array.toArray(), Human[].class);
Array<Human> arrayFromJson = new Array<Human>(gson.fromJson(json, Human[].class));

Gdx.app.log("JSON", "From json");
for (Human human : arrayFromJson) {
    Gdx.app.log("Human", human.name);
}

输出:

JSON: To json
Human: Jack
Human: Tom
Human: Mel
Human: Anne
JSON: From json
Human: Jack
Human: Tom
Human: Mel
Human: Anne

解决方案2:

如果您在某种对象中具有 Array< Human> ,则需要手动将Java数组转换为LibGDX数组,这会有些混乱.

If you had Array<Human> in some kind of object you would need to manually convert from Java array to LibGDX array, which will be a little bit messy.

编写自己的序列化器是解决此问题的方法,但是用泛型编写序列化器很复杂,但是有可能.

Writing own serializers is solution to this, but writing serializers with generics is complicated, but possible.

最困难的部分是解串器:

The hardest part is deserializer:

public class ArrayDeserializer<T> implements JsonDeserializer<Array<T>> {
    Class<T[]> tClass;

    public ArrayDeserializer(Class<T[]> tClass) {
        this.tClass = tClass;
    }

    @Override
    public Array<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        T[] objects = context.deserialize(json, tClass);
        return new Array<T>(objects);
    }
}

Serializer非常笨.

Serializer is pretty dumb.

public class ArraySerializer implements JsonSerializer<Array> {
    @Override
    public JsonElement serialize(Array src, Type typeOfSrc, JsonSerializationContext context) {
        return context.serialize(src.toArray(), src.toArray().getClass());
    }
}

然后在代码中使用:

Type type = TypeToken.get(array.getClass()).getType(); //Array<Human>
...
gsonBuilder.registerTypeAdapter(type, new ArrayDeserializer<Human>(Human[].class));
gsonBuilder.registerTypeAdapter(type, new ArraySerializer());

然后,您将构建新的Gson实例,该实例能够序列化libGDX的Array.

Then you build new Gson instance that is capable of serializing libGDX's Array.

这篇关于GSON序列化/反序列化自定义通用集合类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆