Type converters
ReActiveAndroid allows you to specify the following data types as the field type:
boolean, Boolean
int, Integer
short, Short
long, Long
float, Float
double, Double
byte, Byte
byte[]
String
If you want to use your own data type as a field type (for example, File or Enum), then you need to serialize this field into one of the supported types. Below is the serializer code for the File type:
public final class FileSerializer extends TypeSerializer<File, String> {
@NonNull
public String serialize(@NonNull File file) {
return file.getAbsolutePath();
}
@NonNull
public File deserialize(@NonNull String filePath) {
return new File(filePath);
}
}
Note: ReActiveAndroid already has ready serializers for classes File
, UUID
, BigDecimal
, Calendar
, java.util.Date
and java.sql.Date
.
After creating a serializer, you need to add it to the database configuration:
DatabaseConfig appDatabaseConfig = new DatabaseConfig.Builder(AppDatabase.class)
.addTypeSerializers(FileSerializer.class)
.build();