Hello. I'm running a Ktor server with a particular...
# javascript
g
Hello. I'm running a Ktor server with a particular endpoint that responds a CommonDateTime class object. In a kotlin,js client, I'm making a fetch request to the endpoint and I'm trying to parse the JSON to CommonDateTime class like this:
Copy code
it.json().await().unsafeCast<CommonDateTime>()
CommonDateTime:
Copy code
expect class CommonDateTime(millis: Long) {
    fun getYear(): Int
}
However, calling
getYear()
function on a casted instance yields an error:
TypeError: is not a function
(since
unsafeCast
just uses dynamic JS values). Is there an alternative to
unsafeCast
that would actually create the objects from JSON so that I could call its instance methods?
t
You can use
kotlinx.serialization
to receive Kotlin/JS class instance from JSON or JS object https://github.com/Kotlin/kotlinx.serialization
i
Maybe it will be useful There is some problems with ktor + DCE, which will be unactual in 1.4-M2 https://youtrack.jetbrains.com/issue/KT-36484
👍 2