gpt4 book ai didi

java - ActiveAndroid TypeSerializer - 枚举列表

转载 作者:太空狗 更新时间:2023-10-29 13:10:36 29 4
gpt4 key购买 nike

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

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

最佳答案

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

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 字符串的价格对象

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

关于java - ActiveAndroid TypeSerializer - 枚举列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41543740/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com