'Kotlin Using Genrics for Dynamic Response Parsing

I had the response structure given below

@JsonClass(generateAdapter = true)
data class ResponseMessage(
    @field:Json(name = "ClientID") val clientID: String,
    @field:Json(name = "MessageID") val messageID: String,
    @field:Json(name = "Payload") val payload: Payload,
    @field:Json(name = "Timeout") val timeout: Int,
    @field:Json(name = "Timestamp") val timestamp: Long,
    @field:Json(name = "Type") val type: String
)

@JsonClass(generateAdapter = true)
data class Payload(
    @field:Json(name = "Action") val action: String,
    @field:Json(name = "Params") val params: Params?
)

@JsonClass(generateAdapter = true)
data class Params(
    @field:Json(name = "room") val userDetails: UserDetails?,
    @field:Json(name = "configuration") val configurationDetails: ConfigurationDetails?,
    @field:Json(name = "category") val category: String?
)

Here Params could be changed in response as per the requested service. However in some cases params will not required at all. How this can be done by making Params as Generic Type so that when i need UserDetails i will pass that and likewise for other params.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source