I'm trying to set up a generic class to retrieve data from Firebase but I'm stuck on the parsing part.
override fun onDataChange(snapshot: DataSnapshot) {
try {
val data: T? = snapshot.getValue(dataType)
onDataReadFromDatabase(data, d, snapshot, changeListener)
} catch(e: Exception) {
d.resumeWithException(e)
}
}
T
is the type of my data and dataType
is Class<T>
. This is working fine for flat data structures but when there is a list
as a child of T
it fails with Expected a List while deserializing, but got a class java.util.HashMap
.
An example of data structure that fails:
{
"id": "xxx",
"name": "test",
"items": {
"a": {"name": "itemA"},
"b": {"name": "itemB"},
"c": {"name": "itemC"},
"d": {"name": "itemD"}
}
}
With a model like this:
data class ItemList(val id: String, val name: String, val items: MutableList<Item>) {
...
}
I know there is a way to parse children by looping on them like it is said here but this is by knowing the class of the items.
What I would expect is a way to say to the Firebase parser: each time you need to convert a map to a list, use the function x.