In my app I'm using ActiveAndoid
.
In some models I have a column List<String>
and in other List<MyEnum>
.
I created a TypeSerializer
for each type:
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;
}
}
and for MyEnum
(which is an enum
type):
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;
}
}
My problem is that ActiveAndroid
serialize the MyEnum
list type as list of strings (with StringsListTypeSerializer
) and on runtime when I use List<MyEnum>
in for each loop I'm getting ClassCastException
- can't cast String
to Enum
.
How to solve that problem?
The approach I have used to figure out different types
Following classes use BaseGsonSerializer
This gives me exact what I want that is list of prices converted to json string and price object converted to json string
Hope this could help you to fix your problem.