Skip to content

Instantly share code, notes, and snippets.

@hvisser
Created September 14, 2014 18:17
Show Gist options
  • Save hvisser/3568913bba3e67f6e109 to your computer and use it in GitHub Desktop.
Save hvisser/3568913bba3e67f6e109 to your computer and use it in GitHub Desktop.
Another example of "embedding" entities in another Cupboard entity
// TypeToken is a Gson class
Type type = new TypeToken<List<Author>>(){}.getType();
GsonFieldConverterFactory factory = new GsonFieldConverterFactory(type);
// Register the factory and set the instance as the global Cupboard instance
CupboardFactory.setCupboard(new CupboardBuilder().registerFieldConverterFactory(factory).build());
import com.google.gson.Gson;
import android.content.ContentValues;
import android.database.Cursor;
import java.lang.reflect.Type;
import nl.qbusict.cupboard.convert.EntityConverter;
import nl.qbusict.cupboard.convert.FieldConverter;
public class GsonFieldConverter<T> implements FieldConverter<T> {
private final Gson mGson;
private final Type mType;
public GsonFieldConverter(Gson gson, Type type) {
mGson = gson;
mType = type;
}
@Override
public T fromCursorValue(Cursor cursor, int columnIndex) {
return mGson.fromJson(cursor.getString(columnIndex), mType);
}
@Override
public EntityConverter.ColumnType getColumnType() {
return EntityConverter.ColumnType.TEXT;
}
@Override
public void toContentValue(T value, String key, ContentValues values) {
values.put(key, mGson.toJson(value));
}
}
import com.google.gson.Gson;
import java.lang.reflect.Type;
import nl.qbusict.cupboard.Cupboard;
import nl.qbusict.cupboard.convert.FieldConverter;
import nl.qbusict.cupboard.convert.FieldConverterFactory;
public class GsonFieldConverterFactory implements FieldConverterFactory {
private Type mType;
private Gson mGson;
public GsonFieldConverterFactory(Gson gson, Type type) {
this.mGson = gson;
this.mType = type;
}
public GsonFieldConverterFactory(Type type) {
this.mType = type;
this.mGson = new Gson();
}
@Override
public FieldConverter<?> create(Cupboard cupboard, Type type) {
if (type == mType) {
return new GsonFieldConverter(mGson, type);
}
return null;
}
}
@fdonzello
Copy link

Tried this but doesn't work. Even setting the new Cupboard instance when querying I alway get Cannot convert field of type java.util.List.

Debugging I see that the Cupboard instance has the converter correctly registered.

@borisdb
Copy link

borisdb commented Oct 11, 2014

In my case I have an object with just an id and a List
in the create method of the factory the type is never == to mType , I found a not very elegant workaround :
@OverRide
public FieldConverter<?> create(Cupboard cupboard, Type type) {
if (type.toString().equals(mType.toString())) {
return new GsonFieldConverter(mGson, type);
}
return null;
}

@hugo, thanks for the library

@hvisser
Copy link
Author

hvisser commented Oct 12, 2014

OK, sorry, this doesn't work for generic types, that needs more work.

If you debug it, you'll notice that Type is of ParameterizedType in the case of a generic type. You need to go from there to determine if your factory is supporting that type by using getRawType(). It's not pretty, but that's how it works with generic Java.

mType.equals(type) might do the trick as well, if you are looking for the exact type. The == comparison only works for class types basically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment