I try to Json.encodeToString using function from extended class:
Child class
class Body(
var variable1: String,
var variable2: String,
var variable3: Int,
): Serializable()
Parent class
@Serializable
open class Serializable {
fun getJson(): String{
return Json.encodeToString(this)
}
}
Using
var body = Body("1","1",1)
var json = body.getJson()
And this get "{}". I think it takes properties from the parent class.
EDIT:
I need annotation in Serializable class because i need to put different objects to one function:
var body = Body("1","1",1)
var response = RequestServer.Request<Boolean>("route/set", body, HttpMethod.Put)
var body2 = Body2("2","2",2)
var response = RequestServer.Request<Boolean>("route/set", body2, HttpMethod.Put)
Function:
suspend inline fun <reified T> Request(route: String, body: Serializable, method: HttpMethod): BaseResponse<T> {
var response: HttpResponse = client.request(Address + route) {
this.method = method
contentType(ContentType.Application.Json)
setBody(Json.encodeToString(body))
}
return Json{ ignoreUnknownKeys = true }.decodeFromString(response.bodyAsText())
}
How to make it take properties from child class?