https://kotlinlang.org logo
#serialization
Title
# serialization
e

elizarov

10/12/2017, 1:56 PM
@karelpeeters Yes. It is possible write a “JS” format implementation that does exactly what you describe. We are considering to include in some future update.
k

karelpeeters

10/12/2017, 1:59 PM
Amazing, thanks!
e

elizarov

10/12/2017, 2:01 PM
Please, create an issue here https://github.com/kotlin/kotlinx.serialization/issues I would also appreciate if you elaborate on your use-cases for this format
k

karelpeeters

10/12/2017, 7:13 PM
I'll create an issue, I'm not sure about the use cases though, we really just have some JS data structures that would take some effort to migrate to Kotlin.
b

bashor

10/13/2017, 3:26 PM
what is wrong with external interface?
If you don’t want to verify your data it’s exactly that you need
If you can guarantee that some field will be exist just declare it as nullable in Kotlin side.
e

elizarov

10/13/2017, 4:16 PM
As far as I understand there are two key features people need: verify that data conforms to the expected format (all non nullable fields are present, default values are there if data is absent, etc), support data classes (so that they can be destructured, other custom Kotlin functions invoked, etc)
k

karelpeeters

10/13/2017, 4:41 PM
A problem with this approach is that
equals
,
hashcode
and
toString
can't be implemented using extension functions.
I'll be using this util function until there's a proper solution:
Copy code
import kotlinx.serialization.json.JSON as JSONKt
import kotlin.js.JSON as JSONJs

inline fun <reified T: Any> jsToKotlinObject(jsObject: Any) 
        = JSONKt.plain.parse<T>(JSONJs.stringify(jsObject))
Still feels terrible 🙂.
2 Views