ActiveAndroid TypeSerializer - 枚举列表

ActiveAndroid TypeSerializer - list of enums

在我的应用程序中,我使用 ActiveAndoid
在某些模型中,我有一列 List<String>,在其他模型中,我有一列 List<MyEnum>.
我为每种类型创建了一个 TypeSerializer

public class StringsListTypeSerializer extends TypeSerializer
{
    private static GsonBuilder sGsonBuilder;

    static{
        sGsonBuilder = new GsonBuilder();
    }

    @Override
    public Class<?> getDeserializedType() {
        return List.class;
    }

    @Override
    public Class<?> getSerializedType() {
        return String.class;
    }

    @Override
    public String serialize(Object data) {
        if(data == null){
            return null;
        }

        final Gson gson = sGsonBuilder.create();
        return gson.toJson(data);
    }

    @Override
    public List<String> deserialize(Object data) {
        if(data == null){
            return null;
        }

        final Gson gson = sGsonBuilder.create();
        List<String> strings = gson.fromJson(data.toString(), List.class);

        return strings;
    }
}

MyEnum (这是一个 enum 类型):

public class MyEnumListTypeSerializer extends TypeSerializer {

    private static GsonBuilder sGsonBuilder;

    static{
        sGsonBuilder = new GsonBuilder();
        sGsonBuilder.registerTypeAdapter(MyEnum.class, new MyEnumJsonDeserializer());
    }

    @Override
    public Class<?> getDeserializedType() {
        return new TypeToken<ArrayList<MyEnum>>(){}.getType().getClass();
    }

    @Override
    public Class<?> getSerializedType() {
        return String.class;
    }

    @Override
    public String serialize(Object data) {
        if(data == null){
            return null;
        }

        final Gson gson = sGsonBuilder.create();

        return gson.toJson(data);
    }

    @Override
    public ArrayList<MyEnum> deserialize(Object data) {
        if(data == null){
            return null;
        }

        final Gson gson = sGsonBuilder.create();
        Type listType = new TypeToken<ArrayList<MyEnum>>(){}.getType();
        ArrayList<MyEnum> myList = gson.fromJson(data.toString(), listType);

        return myList;
    }
}

我的问题是 ActiveAndroidMyEnum 列表类型序列化为字符串列表(使用 StringsListTypeSerializer),并且当我在每个循环中使用 List<MyEnum> 时在运行时我得到 ClassCastException - 无法将 String 转换为 Enum
如何解决这个问题?

我用来找出不同类型的方法

public abstract class BaseGsonSerializer extends TypeSerializer {

    private Gson gson;

    public BaseGsonSerializer() {
        this.gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    }

    @DebugLog
    @Override
    public Class<?> getSerializedType() {
        return String.class;
    }

    @Override
    @DebugLog
    public Class<?> getDeserializedType() {
        return TypeToken.get(getTypeDeserialize()).getRawType();
    }

    @Override
    public Object serialize(Object data) {
        if (data != null) {
            return gson.toJson(data);
        }
        return null;
    }

    @Override
    public Object deserialize(Object data) {
        if (data != null) {
            return gson.fromJson(data.toString(), getTypeDeserialize());
        }
        return null;
    }

    protected abstract Type getTypeDeserialize();

}

以下 类 使用 BaseGsonSerializer

public class PriceSerializer extends BaseGsonSerializer {

    protected Type getTypeDeserialize() {
        return new TypeToken<Price>() {
        }.getType();
    }
}

public class PriceListSerializer extends BaseGsonSerializer {

    protected Type getTypeDeserialize() {
        return new TypeToken<List<Price>>() {
        }.getType();
    }
}

这正是我想要的,即价格列表转换为 json 字符串和价格对象转换为 json 字符串

希望这可以帮助您解决问题。