I've got an API that lets me send objects across a...
# javascript
b
I've got an API that lets me send objects across a channel (websocket wrapper Socket.io) and these objects can often get quite complex; just force cast them to external interfaces or is there a benefit to JSON.stringify() them and parse them onto a data class using serialization? (mainly concerned about the performance and bundle size impact)
1
t
@JsPlainObject
?
b
yes, simply casting to an external interface (good enough (tm))?
there's a certain benefit of going with serialization
e
I'd go with kotlinx-serialization first, just because it provides validation of the data you're de-serializing. If you see performance is subpar, switch to unsafe casting.
b
the main benefit for me would be polymorphic deserialization
there's nothing like interface Action { type: "doSomething" } in kotlin
where matching on a property would cast to the correct interface iirc
I could deserialize and simply instance of it
compared to ifs with multiple casts
e
where matching on a property would cast to the correct interface iirc
You mean discriminated unions? If you want the same kind of behavior I guess kotlinx-serialization should allow you to do that, that is, check a property and de-serialize accordingly.
b
yes, exactly
built into serialization but not the type system
e
Discriminated unions in Kotlin are sealed classes pretty much.