Another question, can we serialization in desired ...
# serialization
v
Another question, can we serialization in desired format ?
Copy code
{
    "TopicOne": [
        {
            "id": 1,
            "seconds": 5.856,
            "date": "21/10/2010"
        },
        .... // more data 
    ],
    "TopicTwo": [
        {
            "gameNumber": 6,
            "name": "Heat",
            "date": "21/11/2010"
        },
        ..... // more data
    ],
    "TopicThree": [
        {
            "id": 1,
            "numberOfSets": 3,
            "date": "21/11/2011",
   
        }, 
        ....// more data
    ]
}
and see more in thread..
I have created data class for this
Copy code
data class TopicResponse(
    val topicOne: List<TopicOne>? = null,
    val topicTwo: List<TopicTwo>? = null,
    val topicThree: List<TopicThree>? = null
)

data class TopicOne(val id: Int? = null, val seconds: String? = null, val date: String? = null)
data class TopicTwo(val gameNumber: Int? = null, val name: String? = null, val date: String? = null)
data class TopicThree(val id: Int? = null, val numberOfSets: String? = null, val date: String? = null)
So
date
is common so created a base class
Copy code
@Serializable
abstract class TopicBase {
    abstract val date: String
}
inherit this to child class
Copy code
@Serializable
class ChildTopicOne(override val date: String, private val id: Int, private val seconds: String?) : TopicBase() {
   fun getString() :String {
       return "Id $id has to complete the race in $seconds seconds"
   }
}

@Serializable
class ChildTopicTwo(override val date: String, val gameNumber: Int, val name: String?) : TopicBase() {
    fun getString() :String {
        return "gameNumber $gameNumber has to complete the race in $name name"
    }
}

@Serializable
class ChildTopicThree(override val date: String, val id: Int, val numberOfSets: String?) : TopicBase() {
    fun getString() :String {
        return "id $id has to complete the race in $numberOfSets numberOfSets"
    }
}
I tried some code but it not giving me correct output, what I want
Copy code
response.topicOne?.forEach { topicOne ->
     topicOne.date?.let {
          val value: TopicBase = ChildTopicOne(it, topicOne.id, topicOne.seconds)
          val data = Json.encodeToJsonElement(value)
          Log.e("data", "${data}")
     }
}
Actual Output
Copy code
{
  "type": "com.vivek.example.data.ChildTopicOne",
  "date": "21/10/2010",
  "seconds": "5.856",
  "id": "1"
}
Expected Output
Copy code
{
  "type": "com.vivek.example.data.ChildTopicOne",
  "getString": "Id 1 has to complete the race in 5.856 seconds"
}
Is it possible to get this type of output?
@Javier @mbonnin do you any idea about this?