'Kotlin reflection
With Retrofit, I am fetching data and getting JSONObject with the structure like this for example:
{
"token": "some-token",
"tiles": [
{
"id": "id-1",
"type": "RedTile",
"title": "This red title",
"stuff": "hello world"
},
{
"id": "id-2",
"type": "BlueTile",
"title": "This blue title",
"value": 200
}
]
}
Now, when I get this response, I need to make a parser that will actually take the type from every tile and match it with the class that already exists inside application. I need to basically from this response to create new one with token and tiles list but in this case tiles will reflect to actual class in app.
Any idea what is proper way to do it?!
Solution 1:[1]
I don't know what your classes look like, but suppose it's this for example:
sealed interface Tile
data class RedTile(val title: String, val stuff: String) : Tile
data class BlueTile(val title: String, val value: Int) : Tile
Then you could make this as response object
data class Response (
val token: String,
val tiles: List<TileResponse>
) {
fun getTilesObjects() = tiles.mapNotNull { it.toTile() }
}
data class TileResponse (
val id: String,
val type: String,
val title: String,
val stuff: String? = null,
val value: Int? = null
){
fun toTile() : Tile? {
if (type == "RedTile" && stuff != null) return RedTile(title, stuff)
if (type == "BlueTile" && value != null) return BlueTile(title, value)
return null
}
}
And then you can call toTile() on the individual TileResponse objects or get the full list by doing getTilesObjects() on the response object
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ivo Beckers |
