Lucas
01/31/2025, 10:36 AMcommonMain, jsMain, and jvmMain. The goal is to share data classes in commonMain so that my backend (in jvmMain) can serialize them to JSON and send them over the network using Ktor, which is working fine.
However, when trying to parse the JSON on the frontend (jsMain), I encounter an issue where I have to convert the parsed JSON into a dynamic type and then manually reconstruct the data class. This step is necessary to prevent the properties from being null.
This issue seems similar to https://discuss.kotlinlang.org/t/unable-to-read-properties-from-data-object-in-kotlin-js/26897/2
// Code that works
window.fetch("<http://localhost:8080/>").then { response ->
response.text()
}.then { data ->
val parsed = JSON.parse<Example>(data).asDynamic()
val example = Example(parsed.i)
println(example.i)
}.catch {
console.error(it)
}
// Code that fails
window.fetch("<http://localhost:8080/>").then { response ->
response.text()
}.then { data ->
val parsed = JSON.parse<Example>(data)
println(parsed.i)
}.catch {
console.error(it)
}
@Serializable
data class Example(val i: Int)jamshedalamqaderi
01/31/2025, 10:40 AMJson.decodeFromString() methodLucas
01/31/2025, 10:50 AMjamshedalamqaderi
01/31/2025, 2:17 PMLucas
02/01/2025, 9:08 AM