Way to save Map<String, String> to Room Database
Asked Answered
T

1

8

I'm trying to store Map<String, String> to Room database.

Json is like below.

{
  "version": 1,
  "data": {
    "en": {"name": "english"},
    "ko": {"name": "korean"},
    "de": {"name": "germany"}
  }
}

Entity is like below.

@Entity
data class Translation(

        @PrimaryKey(autoGenerate = true)
        val version: Int,

        @SerializedName("data")
        @Embedded
        val data: Data
) {

    data class Data(
            @SerializedName("en")
            val english: Map<String, String>,

            @SerializedName("ko")
            val korean: Map<String, String>,

            @SerializedName("de")
            val german: Map<String, String>
    )
}

Room Database class is like.

    @Database(
            entities = [
                Translation::class
            ],
            version = 1,
            exportSchema = false
    )
    @TypeConverters(ClassTypeConverter::class)
    abstract class AppDatabase : RoomDatabase() {

        abstract fun translationDao(): TranslationDao
    }

At last, ClassTypeConvert is below.

object MapTypeConverter {

    @TypeConverter
    fun stringToMap(value: JsonElement): Map<String, String> {
        return Gson().fromJson(value,  object : TypeToken<Map<String, String>>() {}.type)
    }

    @TypeConverter
    fun mapToString(value: Map<String, String>?): String {
        return if(value == null) "" else Gson().toJson(value)
    }
}

But showing an error,

Cannot figure out how to save this field into database. You can consider adding a type converter for it.
        private final java.util.Map<java.lang.String, java.lang.String> english = null;

Is this wrong way to save Map type to Room database?

Tubman answered 21/9, 2018 at 10:53 Comment(2)
Please, provide you DAO class. Maybe the problem is that the insert() method in the DAO class hasn't @TypeConverters annotationAcademician
You should define TypeConverter on top of declared member variable i.e. data variable in your case by using @TypeConverter notation.Rolf
E
14

First thing is there seems to be a mistake with names. You're adding a type converter called ClassTypeConverter

@TypeConverters(ClassTypeConverter::class)
abstract class AppDatabase : RoomDatabase() {...}

however the converter you say has problems is called MapTypeConverter

object MapTypeConverter {...}

I believe you intend these to be the same


Also, as you are writing kotlin, you need to use the @JvmStatic annotation on the type converter methods as room is expecting static methods

object MapTypeConverter {

    @TypeConverter
    @JvmStatic
    fun stringToMap(value: JsonElement): Map<String, String> {
        return Gson().fromJson(value,  object : TypeToken<Map<String, String>>() {}.type)
    }

    @TypeConverter
    @JvmStatic
    fun mapToString(value: Map<String, String>?): String {
        return if(value == null) "" else Gson().toJson(value)
    }
}
Ephemerality answered 29/7, 2019 at 1:36 Comment(1)
stringToMap should take a String as an argument not JsonElementDiabolic

© 2022 - 2024 — McMap. All rights reserved.