TypeConverters cannot figure out how to save this

2019-06-14 12:05发布

问题:

I got an error when implementing the type converter of Room.

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it. private java.util.List<? extends java.util.Map<java.lang.String, mypage.profile.entity.OtherServiceEntity>> otherServices;

This is my implementation, Firstly config the entity like below, because the POJO contain List of Map I implemented the Room's TypeConverter like this

object OtherServicesTypeConverter {
    private val gson = Gson()
    @TypeConverter
    @JvmStatic
    fun stringToList(data: String?): List<Map<String, OtherServiceEntity>> {
        if (data == null) {
            return Collections.emptyList()
        }

        val listType = object : TypeToken<List<Map<String, OtherServiceEntity>>>() {

        }.type

        return gson.fromJson<List<Map<String, OtherServiceEntity>>>(data, listType)
    }

    @TypeConverter
    @JvmStatic
    fun listToString(objects: List<Map<String, OtherService>>): String {
        return gson.toJson(objects)
    }
}

Then using annotation inside the entity, and database as below

@Entity(tableName = "ProfileEntities")
    data class ProfileEntity(
            @PrimaryKey
            @SerializedName("masterId") var masterId: Long = 0,
            @SerializedName("nickname") var nickname: String = "",
            @TypeConverters(OtherServicesTypeConverter::class)
            @SerializedName("otherServices") var otherServices: List<Map<String, OtherServiceEntity>>,
            @Embedded
            @SerializedName("images") var images: ProfileImagesEntity = ProfileImagesEntity(),
            @SerializedName("updatedAt") var updatedAt: String = ""
    )


 @Database(entities = [ArticleFeatureEntity::class, ProfileEntity::class], version = 3)
    @TypeConverters(DataTypeConverter::class, ProfileTypeConverter::class, OtherServicesTypeConverter::class)
    abstract class AppDatabase : RoomDatabase() {
        abstract fun profileDao(): ProfileDao
        // Code here


    }

Please help to support me, thank you!

回答1:

Finally, I recognized that Room needs some special change in type converter implementation (change to ArrayList<HashMap<String, OtherServiceEntity>>). Still, don't know it is a feature or bug because other cases I am still able to use List as type. This is the detail what I did, and now I am able to run it

@Entity(tableName = "ProfileEntities")
data class ProfileEntity(
        @TypeConverters(ProfileTypeConverter::class)
        @SerializedName("details") var details: List<ProfileDetailEntity> = listOf(),
        @Embedded
        @SerializedName("images") var images: ProfileImagesEntity = ProfileImagesEntity(),
        @PrimaryKey
        @SerializedName("masterId") var masterId: Long = 0,
        @TypeConverters(OtherServicesTypeConverter::class)
        @SerializedName("otherServices") var otherServices: ArrayList<HashMap<String, OtherServiceEntity>> = arrayListOf(),
        @SerializedName("updatedAt") var updatedAt: String = ""
)

And the implementation of converters

class ProfileTypeConverter {
    private val gson = Gson()
    @TypeConverter
    fun stringToList(data: String?): List<ProfileDetailEntity> {
        if (data == null) {
            return Collections.emptyList()
        }

        val listType = object : TypeToken<List<ProfileDetailEntity>>() {

        }.type

        return gson.fromJson<List<ProfileDetailEntity>>(data, listType)
    }

    @TypeConverter
    fun listToString(someObjects: List<ProfileDetailEntity>): String {
        return gson.toJson(someObjects)
    }
}

class OtherServicesTypeConverter {
    private val gson = Gson()
    @TypeConverter
    fun stringToList(data: String?): ArrayList<HashMap<String, OtherServiceEntity>> {
        if (data == null) {
            return ArrayList()
        }

        val listType = object : TypeToken<ArrayList<HashMap<String, OtherServiceEntity>>>() {

        }.type

        return gson.fromJson<ArrayList<HashMap<String, OtherServiceEntity>>>(data, listType)
    }

    @TypeConverter
    fun listToString(objects: ArrayList<HashMap<String, OtherServiceEntity>>): String {
        return gson.toJson(objects)
    }
}

Happy coding!