So, i have an Android Room entity that have a properties which is a list of a data class. Like this:
@ColumnInfo(name = "titles")
val titles: List<AnimeTitleEntity>,
And I'm using a type converter to handle this by converthing this value from list of data class into JSON and vice versa with Moshi.
@JsonClass(generateAdapter = true)
data class AnimeTitleEntity(
@Json(name = "type")
val type: String,
@Json(name = "title")
val title: String
)
import util.fromJson as MoshiFromJson
internal class AnimeTitlesConverter {
@TypeConverter
fun fromJson(json: String): List<AnimeTitleEntity> {
return MoshiFromJson<List<AnimeTitleEntity>>(json) ?: emptyList()
}
@TypeConverter
fun toJson(titles: List<AnimeTitleEntity>): String {
return MoshiToJson(titles)
}
}
val KotlinMoshi: Moshi = Moshi
.Builder()
.add(KotlinJsonAdapterFactory())
.build()
inline fun <reified T> fromJson(data: String): T? {
val adapter = KotlinMoshi.adapter(T::class.java)
return adapter.fromJson(data)
}
I can fetch this normally from room, and do some logging, and the app didn't even throw an error or crash if i don't do anything to this model like converting them into another model. But the problem starts happening when I'm trying to map this, and when I log the data type of the properties its actually a linkedHashMap. So, Room never really return a list of the data class but not having an error.
Entity type: class com.squareup.moshi.LinkedHashTreeMap
How can this happen while not causing error? Because the property should be a list of data class and not LinkedHashTreeMap. And what is the solution for this?