Good afternoon, I encountered an error
D:projectsDictionaryDictionary_ANDappbuildtmpkapt3stubsdebugcomexampledictionarydictionarydatalocalentityWordInfoEntity.java:14: error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
private final java.util.List<com.example.dictionary.dictionary.domain.model.ExampleModel> Example = null;
As I understand it, this error appears when you use the list in the Room model.
@Entity
data class WordInfoEntity(
@PrimaryKey val id:Int?=null,
val Word:String,
val Translation:String,
val Example:List<ExampleModel>
){
fun toWordModel(): WordModel{
return WordModel(
id=id!!,
Word=Word,
Translation=Translation,
Example=Example
)
}
}
But I have a converter that should convert the model
interface JsonParser {
fun <T> fromJson(json: String, type: Type): T?
fun <T> toJson(obj: T, type: Type): String?
}
.
class GsonParser(
private val gson: Gson
): JsonParser {
override fun <T> fromJson(json: String, type: Type): T? {
return gson.fromJson(json, type)
}
override fun <T> toJson(obj: T, type: Type): String? {
return gson.toJson(obj, type)
}
}
.
@ProvidedTypeConverter
class Converters(
private val jsonParser: JsonParser
) {
@TypeConverter
fun fromWordsJson(json:String):List<WordModel>{
return jsonParser.fromJson<ArrayList<WordModel>>(
json,
object :TypeToken<ArrayList<WordModel>>(){}.type
)?: emptyList()
}
@TypeConverter
fun toWordsJson(word:List<WordModel>):String{
return jsonParser.toJson(
word,
object :TypeToken<ArrayList<WordModel>>(){}.type
)?:"[]"
}
}
Here I use a converter
@Database(
entities = [WordInfoEntity::class],
version = 1
)
@TypeConverters(Converters::class)
abstract class WordInfoDataBase:RoomDatabase() {
abstract val dao:WordInfoDao
}
And
@Provides
@Singleton
fun provideWordInfoDatabase(app:Application):WordInfoDataBase{
return Room.databaseBuilder(
app, WordInfoDataBase::class.java, "word_db"
).addTypeConverter(Converters(GsonParser(Gson())))
.build()
}
and at the same moment, the functions are not highlighted as used
I do not understand why the functions from the converter do not want to be used, provided that I have linked them.