Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Serializable fields #642

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions library/src/main/java/com/orm/util/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.orm.SugarRecord;

import java.io.Serializable;
import java.lang.RuntimeException;
import java.lang.StringBuilder;
import java.math.BigDecimal;
Expand Down Expand Up @@ -40,6 +41,10 @@ public static String getColumnType(Class<?> type) {
return "TEXT";
}

if(Serializable.class.isAssignableFrom(type)) {
return "BLOB";
}

return "";
}

Expand Down
25 changes: 25 additions & 0 deletions library/src/main/java/com/orm/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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() + ")");
Expand Down