@TypeConverters(CommonConverter::class)
abstract class AppDatabase : RoomDatabase() {}
here I used CommonConverter for Type converted.
package com.uch.common.db;
import android.text.TextUtils;
import androidx.room.TypeConverter;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.uch.common.utils.DateHandlingUtil;
import com.uch.common.model.AiSpecialtyReport;
import com.uch.common.model.Result;
import com.uch.common.utils.Logger;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class CommonConverter {
private final static DateFormat df = new SimpleDateFormat(
DateHandlingUtil.AppDateFormat.SERVER_DATE_TIME_FORMAT.getValue(), Locale.ENGLISH);
private final static String TAG = CommonConverter.class.getSimpleName();
/* @TypeConverter
public static Date fromTimestamp(Long value) {
return value == null ? null : new Date(value);
}
@TypeConverter
public static Long dateToTimestamp(Date date) {
return date == null ? null : date.getTime();
}*/
@TypeConverter
public static Date fromTimestamp(String value) {
if (value != null) {
try {
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.parse(value);
} catch (ParseException e) {
Logger.error(TAG, e.getMessage());
}
}
return null;
}
@TypeConverter
public static String fromDate(Date value) {
if (value != null) {
try {
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.format(value);
} catch (Exception e) {
Logger.error(TAG, e.getMessage());
}
}
return null;
}
@TypeConverter
public static Object fromObjectString(String value) {
Type objType;
try {
objType = new TypeToken<ArrayList<String>>() {
}.getType();
return new Gson().fromJson(value, objType);
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
objType = new TypeToken<String>() {
}.getType();
}
return new Gson().fromJson(value, objType);
}
@TypeConverter
public static String fromObject(Object value) {
return new Gson().toJson(value);
}
@TypeConverter
public static ArrayList<String> toArrayListString(String value) {
Type objType;
try {
objType = new TypeToken<ArrayList<String>>() {
}.getType();
return new Gson().fromJson(value, objType);
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return new ArrayList<>();
}
@TypeConverter
public static String fromArrayListString(ArrayList<String> value) {
return new Gson().toJson(value);
}
@TypeConverter
public static ArrayList<Result> toArrayListResult(String value) {
Type objType;
try {
objType = new TypeToken<ArrayList<Result>>() {
}.getType();
return new Gson().fromJson(value, objType);
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return new ArrayList<>();
}
@TypeConverter
public static String fromArrayListResult(ArrayList<Result> value) {
return new Gson().toJson(value);
}
@TypeConverter
public static String fromJsonArray(JsonArray value) {
try {
return value.toString();
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return "";
}
@TypeConverter
public static JsonArray toJsonArray(String value) {
try {
return JsonParser.parseString(value).getAsJsonArray();
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return null;
}
@TypeConverter
public static String fromCategory(AiSpecialtyReport.Category value) {
try {
return value.name();
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return "";
}
@TypeConverter
public static AiSpecialtyReport.Category toCategory(String value) {
try {
return AiSpecialtyReport.Category.valueOf(value);
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return null;
}
@TypeConverter
public static String fromDevice(AiSpecialtyReport.Device value) {
try {
return value.name();
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return "";
}
@TypeConverter
public static AiSpecialtyReport.Device toDevice(String value) {
try {
return AiSpecialtyReport.Device.valueOf(value);
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return null;
}
@TypeConverter
public static String fromVerificationStatus(AiSpecialtyReport.VerificationStatus value) {
try {
return value.name();
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return "";
}
@TypeConverter
public static AiSpecialtyReport.VerificationStatus toVerificationStatus(String value) {
try {
return AiSpecialtyReport.VerificationStatus.valueOf(value);
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return null;
}
@TypeConverter
public static String fromProcessStatus(AiSpecialtyReport.ProcessStatus value) {
try {
return value.toString();
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return "";
}
@TypeConverter
public static AiSpecialtyReport.ProcessStatus toProcessStatus(String value) {
try {
return AiSpecialtyReport.ProcessStatus.valueOf(value);
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return null;
}
@TypeConverter
public String fromStringMap(HashMap<String, String> value) {
try {
final Map<String, String> sortedMap = new TreeMap<>(value);
return TextUtils.join(",", sortedMap.keySet())
.concat("<divider>")
.concat(TextUtils.join(",", sortedMap.values()));
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return null;
}
@TypeConverter
public HashMap<String, String> toStringMap(String value) {
try {
final String[] keysValsSep = value.split("<divider>");
final String[] keys = keysValsSep[0].split(",");
final Iterator<String> valuesIterator = Arrays.asList(keysValsSep[1].split(",")).iterator();
final HashMap<String, String> strMap = new HashMap<>();
for (String key : keys) {
strMap.put(key, valuesIterator.next());
}
return strMap;
} catch (Exception e) {
Logger.warn(TAG, e.getMessage());
}
return null;
}
}
Above code I used CommonConverter.Earlier it’s working fine but now I upgrade gradle system 8.3.4 after shows error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
Error facing place
@ColumnInfo(name = DBConstants.COL_ECG_DATA)
@SerializedName(DBConstants.COL_ECG_DATA)
var ecgData: ArrayList<String>? = null
how solve this error anyone help me? Note: Gradle system 8.0.0 no error occurred and also Dao_impl file generate successfully. now Dao_impl file is also not generated. After the Gradle system update this error comes