Hi. Just started with kotlinx.serialization, found...
# serialization
a
Hi. Just started with kotlinx.serialization, found the limitation on constructor parameters. It's actually makes impossible to use inheritance and I'm looking for ways to work around this. One of my attempts was to make parent's property open and override it in child. Like this:
Copy code
@Serializable
abstract class Tree(open val health: Int = 100)

@Serializable
class Willow(override val health: Int = 95) : Tree(health)
But I was surprised when saw the JSON:
Copy code
["Willow",{"health":95,"health":95}]
Actually, I'm not sure it's a valid one... How can I handle inheritance with serialization?
s
Reason for json array here is that when polymorphism is involved, there’s a need to record type information to json. Unfortunately, Inheritance is not well supported for now. You can search for workarounds among open issues, e.g. https://github.com/Kotlin/kotlinx.serialization/issues/75
a
I understand about array and polymorphism. The question was about json object with two similar properties
And I tried to remove the
@Serialization
annotation from parent class. Now I have only one
health
, but parent class's fields are not being serialized (not presented in my sample)
@sandwwraith, I've just found a ticket https://github.com/Kotlin/kotlinx.serialization/issues/101 and understand that all my attempts lose sense since I can't use inheritance on the client side at all. Correct my understanding, please
👍 1
s
I’m afraid so, inheritance is indeed not working on Kotlin/JS
😱 1
a
That's sad.
It was the last reason for trying multiplatform approach...
j
Can work around it by making your val’s abstract?
a
Don't remember if I tried this... I'll try when reach my laptop. But seems it would also cause field duplication. Anyway it's not a key problem. The really big issue is that I can't use serialize open classes in JavaScript. It ruined all my plans on using multiplatform:(
@Jonathan Walsh no that doesn't work. I get two similar keys in json in this case as well.
v
@apomelov it’s not yet fully designed
a
@Vsevolod Tolstopyatov [JB] but you have this in the roadmap, do you?
v
@apomelov sure, we’re working on full-blown serialization
❤️ 1
a
@Vsevolod Tolstopyatov [JB] how about constructor parameters (non val/var)?
v
@apomelov can’t tell yet. But it seems essential for inheritance/polymorphism, so should be supported in some way
a
@Vsevolod Tolstopyatov [JB] that's probably minor. It can be workarounded with mutable classes, and default constructors. Inheritance itself much more important.
s
@apomelov Current limitation on inheritance induced by the fact it uses
Class.forName
on JVM and we don’t have such reflection support in Kotlin/JS. We are working on design that can bypass this limitation for polymorphism (likely, by global registering of all inheritors or something like that)
v
@Vsevolod Tolstopyatov [JB] @apomelov good to hear you are working on a solution. 🙂 I myself am also working on a multiplatform project, encountered this issue and am eagerly waiting for a solution
a
@vpriscan actually a lot of things have been done for multiplatform, but I see three directions to make this approach production usable. 1. Transparent tooling for calls from JS to Kotlin (https://youtrack.jetbrains.net/issue/KT-25987) 2. Full typescript to kotlin support. It's actually quite a difficult thing. So now my bet is still on JS-based client that can use some Kotlin classes (models) and APIs (serialization) 3. Serialization with inheritance support to bring it all together.
👍 1