Given a class `data class Node<T: Any>(val i...
# serialization
a
Given a class
data class Node<T: Any>(val id: String, val type: String, val value: T)
, is it possible to deserialize
value
based on the
type
field?
g
yes, see documentation for polymorphic deserialization https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md
a
Thanks for the quick response. Perhaps I'm reading it wrong but I don't see any examples matching my use case. I do not want multiple implementations of the
Node
class, and I do not control objects of type
T
(3rd party classes). So the type information of the
value
property I want to keep external to the instance.
I would also like to later put them in a container class
data class Graph<T: Any>(val nodes: List<Node<out T>>)
g
not sure that this use case supported directly, with generic List, but maybe you could have a base class Node<T> and particular implementation NodeFoo, NodeBar, it already supported with custom serializer registration
a
Thanks, I will try it out in unit test but I'm wondering how it will know the type without it being present in the json
g
But type is presented in Node, so parent will pass type to child serializer