1

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?

1 Answer 1

1

That's because the use of @Serializable in the parent class and the method getJson() tries to serialize itself.

So, what you can do is to put the annotation in the child class, instead of the parent class.

Also, for best practices, you can change your parent class name, to avoid confusion with the standard Kotlin interface.

EDIT

I get it now you are trying to build some API, right? So what I do actually when using Kotlin + Spring is to use a data class. The data class does the magic to serializing and deserializing JSON.

Check this article in Baeldung, it basically does what you need and also uses the extension function: https://www.baeldung.com/kotlin/data-class-json-serialize-default-values

Kotlin docs about data classes: https://kotlinlang.org/docs/data-classes.html

Sign up to request clarification or add additional context in comments.

4 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
@joaquimchianca i need annotation in Serializable class, because I send an Body object to a function that accepts such an object. I use this function to send different types of objects, not only Body
@joaquimchianca look at edited in post
@joaquimchianca i use Retrofit lib to requests and it works perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.