Moshi parse json with different key

Viewed 223

I was looking towards PolymorphicAdapter but all the polymorphic example I could find had a key called "type" or something similar that could be use to differentiate the class to use. However in my case I don't have such key. I'm a bit lost on how to parse such a peculiar json.

{
    "infos": {
        "1588318": {
            "id": "1588318",
            "id_user": "9701",
            "profile_name": "Profile1",
            "views": 100
        },
        "1588319": {
            "id": "1588319",
            "id_user": "7391",
            "profile_name": "Profile2",
            "views": 10
        },
        "1588320": false,
        "1588321": {
            "id": "1588321",
            "deleted": true
        }
    }
}
data class UserInfo(val infos: Map<String, UserResult>)

sealed class UserResult {

    data class UserDeleted(val id: String, val deleted: Boolean): UserResult()
    data class UserInfoCard(
        val id: String,
        val title: String,
        @Json(name = "profile_name") val profileName: String,
        val views: Int
    ): UserResult()
    
}
1 Answers

In the end I didn't find any solution and after discussing with the API manager he said he would update with a key to determine if it's either a profile or a deleted_profile

Related