Created
September 14, 2014 18:17
-
-
Save hvisser/3568913bba3e67f6e109 to your computer and use it in GitHub Desktop.
Another example of "embedding" entities in another Cupboard entity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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
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