I want to be able to store and obtain serialized List of objects using GSON.
However when I am trying to obtain the List I am getting weird structure of ArrayList of LinkedTreeMaps (each for the object property)
Here is my method to store something to prefs storage:
fun <T> putAsJson(key: String, valueObject: T?) {
if (valueObject != null) {
put(key, gson.toJson(valueObject))
} else {
put(key, null)
}
}
And here is how I obtain it:
inline fun <reified T: Any> getFromJson(key: String): T? {
val jsonValue = get<String>(key)
return try {
gson.fromJson(jsonValue, T::class.java)
} catch (ex: Exception) {
Timber.e(ex, "Error when parsing JSON representing ${T::class.java} class")
null
}
}
So i simply store it as:
fun saveSomeList(list: List<SomeObject>?) {
someStorage.putAsJson(KEY, list)
}
And then I try to obtain it using this method:
fun getSomeList(): List<SomeObject> {
return someStorage.getFromJson<List<SomeObject>>(KEY) ?: emptyList()
}
What I am doing wrong?