ActiveAndroid TypeSerializer - 枚举列表

时间:2017-01-09 08:22:29

标签: java android enums activeandroid

在我的应用中,我正在使用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;
    }
}

MyEnumenum类型):

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>用于每个循环I'得到ClassCastException - 无法将String强制转换为Enum 如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我用来计算不同类型的方法

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();
    }
}

以下课程使用BaseGsonSerializer

 $ sudo apt-get install autogen
Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 autogen : Depends: guile-2.0-libs but it is not going to be installed
           Depends: libopts25-dev (= 1:5.18.4-3) but it is not going to be installed
           Recommends: autogen-doc but it is not going to be installed
 gnome-packagekit-session : Depends: gnome-packagekit-data (>= 3.14.0-1) but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

这给了我确切的我想要的是转换为json字符串的价格列表和转换为json字符串的价格对象

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