`JSON.stringify(data class)` adds `_` underscore t...
# javascript
s
JSON.stringify(data class)
adds
_
underscore to parameters. is there a way to avoid that?
Copy code
data class Test(val email: String)
val test = Test("<mailto:abc@x.com|abc@x.com>")
val json = JSON.stringify(test)
resulting json is
{"_email":"<mailto:abc@x.com|abc@x.com>"}
why is there an
underscore
before
email
?
a
Are you using the new JS IR? What happens if you
@JsExport
your entire data class?
Unfortunately Kotlin just doesn't represent `data class`es as plain JS Objects, so options are limited. We use
kotlinx.serialization
`encodeToDynamic`/`decodeFromDynamic` in some places for this, and manually convert to/from `external interface`s in other places for this Not an ideal workflow
s
Let me try
JsExport
there.
yeah both data and regular class with @JsExport has same result with
_
Yes I'm using JS IR.
Copy code
js(IR) {
        binaries.library()
        nodejs()
    }
so without
kotlinx.serialialization
what option do I have? Just manually creating a
json
object on kotlin/js side?
b
JSON.stringify should only be used with external interfaces. Declare one yourself and convert your data class before stringifying. Alternatively you can use Json {} builder from stdlib
There's also KON to help you
s
Yeah I wanted to avoid manual creation if possible so we have more in common code. Let me try to add some manual code to see if I can have a graceful solution
b
It's either living with manual conversions or adding heavy dependency on kotlinx.serialization
s
😞
a
yup, this is a big issue with using kotlin/js in a multiplatform project right now 😞
107 Views