How to create kotlin data class for this network service?

289 views Asked by At

I want to know what approach you will take to make data class in this situation, In this JSON service

I have subCategories under the other-services field. every item in the subCategories has a slug field which is also one of the field in other data elements like - digital-currency, picture, print. now my requirement is I want to pick up appropriate data class based on user selection of subCategories item. if the user has selected below item

 {
    "title": "電子マネー",
    "slug": "digital-currency"
 }

then I should be able to match the slug field and then should pick up the digital-currency data element

    "digital-currency": {
    "slug": "digital-currency",
    "title": "電子マネー",
    "_id": "7j6OzPKVzim7xvW8BvI8zV",
    "isSub": true,
    "parent": "other-services"
  }

how can I make data class for this ?

2

There are 2 answers

0
chethan On

Below is my solution , If I understand it properly , it is nothing but finding object of given type in a heterogeneous List. here DigitalCurrency, Picture, Print, pet-supplies,... so on , are all heterogeneous type. To pick any one of this item , you uses slug field as identifier, you must have mapping mechanism to pick right data object for given type with given json structure.

however solution of mine goes adding WHEN condition every time when new data element added at backend. I believe , as per the requirement and Json structure made here , this cannot be generalize to any extend where It can map to appropriate data class without any additional changes in Code in future.

if you think that Data class can be construct in such way that it doesn‘t need any change in code to accommodate newly added data , Please let me know.

data class NetworkResponse(
    val otherServices: OtherServices,
    val digitalCurrency: DigitalCurrency,
    val picture: Picture,
    val print: Print
) {
    
    fun getUserSelectedCategory(slug : String) : BaseClass {
        when (slug) {
            "digital-currency" -> {
                return digitalCurrency
            }

            "picture" -> {
                return picture
            }

            "print" -> {
                return print
            }
        }
    }
    

}


data class OtherServices(val subCategory: List<SubCategory>) {
}

interface BaseClass
data class SubCategory(val title: String, val slug: String) : BaseClass
data class DigitalCurrency(val title: String, val slug: String) : BaseClass
data class Picture(val title: String, val slug: String) : BaseClass
data class Print(val title: String, val slug: String) : BaseClass
0
Kivia Martins Brito On

You could simplify your code by..

data class NetworkResponse(
    val otherServices: OtherServices,
    val digitalCurrency: SubCategory,
    val picture: SubCategory,
    val print: SubCategory
) {
    fun getUserSelectedCategory(slug : String) : SubCategory? {
       return when (slug) {
            "digital-currency" -> digitalCurrency
            "picture" -> picture
            "print" -> print
            else -> null
        }
    }
}

data class OtherServices(val subCategory: List<SubCategory>)
data class SubCategory(val title: String, val slug: String)