How to make a Generic Type Serializable API Model?

When i using Retrofit2, I wanna try to convert JSON to Object in Kotlin. I have no idea how to generate it to an Object.

My Api Response Json

[
    {
        "id": 1,
        "jsonrpc": "2.0",
        "result": [
            0,
            {
                "interface": [
                    {
                        "ipv6-address": [],
                        "l3_device": "br-lan",
                        "dns-search": [],
                        "data": [],
                        "delegation": true,
                        "available": true,
                        "metric": 0,
                        "ipv4-address": [
                            {
                                "mask": 24,
                                "address": "192.168.56.4"
                            }
                        ],
                        "interface": "lan",
                        "proto": "static",
                        "route": [],
                        "dynamic": false,
                        "device": "br-lan",
                        "pending": false,
                        "inactive": {
                            "dns-server": [],
                            "route": [],
                            "dns-search": [],
                            "neighbors": [],
                            "ipv4-address": [],
                            "ipv6-address": []
                        },
                        "dns_metric": 0,
                        "neighbors": [],
                        "ipv6-prefix": [],
                        "uptime": 7851,
                        "dns-server": [],
                        "autostart": true,
                        "ipv6-prefix-assignment": [
                            {
                                "mask": 60,
                                "local-address": {
                                    "mask": 60,
                                    "address": "fd2b:c20f:d999::1"
                                },
                                "address": "fd2b:c20f:d999::"
                            }
                        ],
                        "up": true,
                        "updated": [
                            "addresses"
                        ]
                    }
                ]
            }
        ]
    },   
    {
        "id": 5,
        "jsonrpc": "2.0",
        "result": [
            0,
            {
                "dhcp6_leases": [
                    {
                        "expires": 35369,
                        "hostname": "DESKTOP-NV4JH0F",
                        "ip6addrs": [
                            "fd2b:c20f:d999::946/128"
                        ],
                        "macaddr": "D8:BB:C1:44:8E:35",
                        "duid": "0001000128fd58a5d8bbc1448e35",
                        "ip6addr": "fd2b:c20f:d999::946"
                    }
                ],
                "dhcp_leases": []
            }
        ]
    }
]

This is my InfoResponseModelItem, but it is not enough for me to use.

data class InfoResponseModelItem(
    @SerializedName("id")
    val id: Int,
    @SerializedName("jsonrpc")
    val jsonrpc: String,
    @SerializedName("result")
    val result: List<Any>
)

This is the sample of NetworkInterface

data class NetworkInterface(
    @SerializedName("interface")
    val interfaceX: List<Interface>?
)
data class Interface(
    @SerializedName("ipv6-address")
    val ipv6Address: List<Any>?,
    @SerializedName("l3_device")
    val l3Device: String?,
    @SerializedName("dns-search")
    val dnsSearch: List<Any>?,
    @SerializedName("data")
    val `data`: Any?,
    @SerializedName("delegation")
    val delegation: Boolean?,
    @SerializedName("available")
    val available: Boolean?,
    @SerializedName("metric")
    val metric: Int?,
    @SerializedName("ipv4-address")
    val ipv4Address: List<Ipv4Addres>?,
    @SerializedName("interface")
    val interfaceX: String?,
    @SerializedName("proto")
    val proto: String?,
    @SerializedName("route")
    val route: List<Route>?,
    @SerializedName("dynamic")
    val `dynamic`: Boolean?,
    @SerializedName("device")
    val device: String?,
    @SerializedName("pending")
    val pending: Boolean?,
    @SerializedName("inactive")
    val inactive: Inactive?,
    @SerializedName("dns_metric")
    val dnsMetric: Int?,
    @SerializedName("neighbors")
    val neighbors: List<Any>?,
    @SerializedName("ipv6-prefix")
    val ipv6Prefix: List<Any>?,
    @SerializedName("uptime")
    val uptime: Int?,
    @SerializedName("dns-server")
    val dnsServer: List<String>?,
    @SerializedName("autostart")
    val autostart: Boolean?,
    @SerializedName("ipv6-prefix-assignment")
    val ipv6PrefixAssignment: List<Ipv6PrefixAssignment>?,
    @SerializedName("up")
    val up: Boolean?,
    @SerializedName("updated")
    val updated: List<String>?
)
data class Ipv4Addres(
    @SerializedName("mask")
    val mask: Int?,
    @SerializedName("address")
    val address: String?
)
data class Route(
    @SerializedName("target")
    val target: String?,
    @SerializedName("mask")
    val mask: Int?,
    @SerializedName("source")
    val source: String?,
    @SerializedName("nexthop")
    val nexthop: String?
)
data class Inactive(
    @SerializedName("dns-server")
    val dnsServer: List<Any>?,
    @SerializedName("route")
    val route: List<Any>?,
    @SerializedName("dns-search")
    val dnsSearch: List<Any>?,
    @SerializedName("neighbors")
    val neighbors: List<Any>?,
    @SerializedName("ipv4-address")
    val ipv4Address: List<Any>?,
    @SerializedName("ipv6-address")
    val ipv6Address: List<Any>?
)
data class Ipv6PrefixAssignment(
    @SerializedName("mask")
    val mask: Int?,
    @SerializedName("local-address")
    val localAddress: LocalAddress?,
    @SerializedName("address")
    val address: String?
)
data class LocalAddress(
    @SerializedName("mask")
    val mask: Int?,
    @SerializedName("address")
    val address: String?
)

I wanna handle like this. The reason i want to handle this is because i can use it easier in my code.

// there is a parameter that is 'context' will return List<InfoResponseModelItem>

Log.d(TAG, "id"+ (content[0].id))
Log.d(TAG,"id"+ ((content[0].result[1] as NetworkInterface).interfaceX?.get(0)?.device ?: ""))

Leave a Comment