How would you write TypeConverter for Map? My approach was to do it by Moshi
class Converters() {
val moshi = Moshi
.Builder()
.add(KotlinJsonAdapterFactory())
.build()
val mapOfStringsType = Types.newParameterizedType(Map::class.java, String::class.java, String::class.java)
val mapOfStringsAdapter = moshi.adapter<Map<String, String>>(mapOfStringsType)
@TypeConverter
fun stringToMap(data: String): Map<String, String> {
return mapOfStringsAdapter.fromJson(data).orEmpty()
}
@TypeConverter
fun mapToString(map: Map<String, String>): String {
return mapOfStringsAdapter.toJson(map)
}
}
However, it smells, because I can't inject Moshi by Converters()
constructor.
And, I'm afraid, it's not the best performance either.