I want to convert JSON string into objects and I'm trying to find the best way to do that since those objects are really similar and the only change is the "data" field between them:
{
"event_type": "REGISTER",
"day": "2021-01-01",
"data": {
"name": "Kyore",
"age": 10
}
}
{
"event_type": "DELETE",
"day": "2021-01-01",
"data": {
"id": "1234-1234-1234",
"reason": "user requested"
}
}
Those events come from a SQS Listener as a JSON string and I'm trying to find the best way to handle them with Kotlin, but I'm not sure how to pick which class will be the one to be converted in my listener:
abstract class Event {
abstract val event_type: String
abstract val day: String
abstract val data: EventData
abstract class EventData
}
class RegisterEvent(
override val eventType: String,
override val day: String,
override val data: RegisterData
) : Event() {
data class RegisterData(
val name: String,
val age: Int
) : Event.EventData()
}
class DeleteEvent(
override val eventType: String,
override val day: String,
override val data: DeleteData
) : Event() {
data class RegisterData(
val id: String,
val reason: String
) : Event.EventData()
}
What is the best way to convert the message string in the correct event?