diff --git a/library/src/main/java/com/orm/util/QueryBuilder.java b/library/src/main/java/com/orm/util/QueryBuilder.java index e1c3557f..91d725a9 100644 --- a/library/src/main/java/com/orm/util/QueryBuilder.java +++ b/library/src/main/java/com/orm/util/QueryBuilder.java @@ -2,6 +2,7 @@ import com.orm.SugarRecord; +import java.io.Serializable; import java.lang.RuntimeException; import java.lang.StringBuilder; import java.math.BigDecimal; @@ -40,6 +41,10 @@ public static String getColumnType(Class type) { return "TEXT"; } + if(Serializable.class.isAssignableFrom(type)) { + return "BLOB"; + } + return ""; } diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 7c0f168a..5be83839 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -12,8 +12,13 @@ import com.orm.helper.MultiDexHelper; import com.orm.helper.NamingHelper; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -139,6 +144,17 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj values.putNull(columnName); } else if (columnType.isEnum()) { values.put(columnName, ((Enum) columnValue).name()); + } else if (columnType.equals(String.class)) { + values.put(columnName, (String)columnValue); + } else if (Serializable.class.isAssignableFrom(columnType)) { + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(columnValue); + values.put(columnName, bos.toByteArray()); + } catch(IOException e) { + values.put(columnName, "".getBytes()); + } } else { values.put(columnName, String.valueOf(columnValue)); } @@ -228,6 +244,15 @@ public static void setFieldValueFromCursor(Cursor cursor, Field field, Object ob Log.e("Sugar", "Enum cannot be read from Sqlite3 database. Please check the type of field " + field.getName()); } } + } else if(Serializable.class.isAssignableFrom(fieldType)) { + try { + ByteArrayInputStream bis = new ByteArrayInputStream(cursor.getBlob(columnIndex)); + ObjectInputStream ois = new ObjectInputStream(bis); + Object obj = ois.readObject(); + field.set(object, obj); + } catch(IOException|ClassNotFoundException e) { + field.set(object, null); + } } else { if (ManifestHelper.isDebugEnabled()) { Log.e("Sugar", "Class cannot be read from Sqlite3 database. Please check the type of field " + field.getName() + "(" + field.getType().getName() + ")");