I have an ArrayList of objects in a Sprinkles model which I was hoping to serialize to a string to store and pass around. I'm using a TypeSerializer to attempt to convert the ArrayList to json but I'm getting an error when trying to register the type.
I believe it is something to do with the class I'm passing as the first argument of the registerType method, but I don't know what to put there.
My TypeSerializer:
public static class NoteListSerializer implements TypeSerializer<ArrayList<Note>> {
@Override
public ArrayList<Note> unpack(Cursor cursor, String s) {
return new Gson().fromJson(cursor.getString(cursor.getColumnIndex(s)), new TypeToken<ArrayList<Note>>(){}.getType());
}
@Override
public void pack(ArrayList<Note> notes, ContentValues contentValues, String s) {
contentValues.put(s, new Gson().toJson(notes));
}
@Override
public String toSql(ArrayList<Note> notes) {
Gson gson = new Gson();
return gson.toJson(notes);
}
@Override
public SqlType getSqlType() {
return SqlType.TEXT;
}
}
And the register line:
sprinkles.registerType(new ArrayList<Note>().getClass(), new NoteListSerializer());
is returning the following compile time error:
Error:(105, 18) error: method registerType in class Sprinkles cannot be applied to given
types;
required: Class<T>,TypeSerializer<T>
found: Class<CAP#1>,NoteListSerializer
reason: inferred type does not conform to equality constraint(s)
inferred: ArrayList<Note>
equality constraints(s): ArrayList<Note>,CAP#2
where T is a type-variable:
T extends Object declared in method <T>registerType(Class<T>,TypeSerializer<T>)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends ArrayList from capture of ? extends ArrayList
CAP#2 extends ArrayList from capture of ? extends ArrayList
0 comments:
Post a Comment