Hi This might be more a kotlinjs question: I have...
# kvision
k
Hi This might be more a kotlinjs question: I have a dynamic typed js object, from my frontend which I want to pass as MyObject to backend When I try this..
Copy code
//frontend code
val jsonString = JSON.stringify(dynamicData)
val myObj  = JSON.parse<MyObject>(jsonString)
myService.someMethod(myObj)
//backend This myObj is coming as "[object Object]"
Copy code
and getting this error:
[reactor-http-nio-8] ERROR io.kvision.remote.KVServiceManager - Unexpected JSON token at offset 0: Expected '{, kind: CLASS'
JSON input: "[object Object]"
I have a workaround to pass the stringified json to backend and then parse as my type. But just wanted to get some idea whether this can be done in frontend.
r
is
MyObject
serializable?
it needs to be
@Serializable
with kotlinx.serialization
k
Yes, it is.
all the nested types are also annotated with this Serializable.
r
I think you will not get true Kotlin class if you are using JS native
JSON
instead of
JSON.parse
you should use
Json.decodeFromString
from
kotlinx.serialization
you can use helper object
io.kvision.utils.JSON.plain
(or
.nostrict
) which has custom serializers for
Date
class already declared
k
Copy code
kotlinx.serialization.json.Json{ ignoreUnknownKeys = true }
                .decodeFromString<MyObject>(JSON.stringify(dynamicData))
This worked.. Cheers