how do you all typically use `data` classes when i...
# javascript
d
how do you all typically use
data
classes when integrating with existing javascript projects?
my colleague is stating that the objects he gets back on the javascript side are more complex than plain javascript objects (especially if they use enums) - should we be using kotlinx.serialization when using kotlin/js data classes generated from a kotlin multiplatform project?
👌 1
t
Hide inside application 🙂
Copy code
@Serializable
private data class User {
    val id: Id,
    val name: String
}

@JsExport
class UserView: HTMLElement {
    private var _data: User? = null

    val data: Any
        get() = _data.toJsObject()
        set(value) { _data = value.parse<User>() }
}
In JS:
Copy code
const data = { id: "42", name: "Darran" }

const view = new UserView()
view.data = data

data.id = "13"

// { id: "42", name: "Darran" }
console.log(view.data)
r
It's generally not a big problem when passing kotlin data classes directly to JS libraries or components. Some additional props won't harm, unless something on the JS side will try to enumerate object properties. The real problem is getting data back from JS to Kotlin - deserialization is the answer to this.
t
especially if they use enums
One more case - default values
Some additional props won’t harm, unless something on the JS side will try to enumerate object properties.
.. and send this properties on server 🙂
r
Why would anyone want to send data from JS when we have Kotlin with coroutines and serialization? 😉
t
@dazza5000 Common serialization pluses: 1. Clean contract (no additional properties) 2. Safer 3. More flexible (defaults, enums)
Why would anyone want to send data from JS when we have Kotlin with coroutines and serialization? 😉
You can create “stupid” webcomponent with Kotlin/JS Outer system will send data as it wants 🙂
r
I strongly deny the need to create JS libraries in Kotlin ;-) It's a waste of good code ;-)
d
can you import kotlinx.serialization in a standard js project?
t
Yes, dependency on runtime required.
I strongly deny the need to create JS libraries in Kotlin ;-) It’s a waste of good code 😉
@Robert Jaros Powerful webcomponents with minimalistic contract

Small example :)