2017-01-09 74 views
3

在我的應用程序中,我使用的是ActiveAndoid
在某些型號中,我有一列List<String>和其他List<MyEnum>
我創建了一個TypeSerializer對於每種類型:
ActiveAndroid 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
如何解決這個問題?

回答

2

的方法我都用過找出不同類型

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字符串

希望這可以幫助您解決您的問題。