Is it possible to determine which `KSerializer` of...
# serialization
b
Is it possible to determine which
KSerializer
of a List of KSerializers can be used for an object? Maybe with the descriptor?
d
What do you mean? Like if deserialization will be possible?
f
Technically you could try to deserialize and catch the exceptions that might occur. Could you provide more info on your use case?
b
I have polymorphic serialization, which is customizable by users of my library. They can put a
String
,
KClass
-Type and a
KSerializer
into a set. On serialization my own
KSerializer
searches for a matching
KClass
in this set, uses the users
KSerializer
to serialize the content and puts a
type
field into the Json from the
String
in the set. This works very good, as long as the user does not use generics. With generics, the search of a matching
KClass
does not include the generic type (->type erasure). Therefore it would be cool to use something like the
descriptor
to find a serializer, which can be used to serialize a given (generic) object.
d
That's tricky, how is the deserialiser supposed to know which generic type to use based on the JSON data?
What does this hierarchy look like? It's probably matrix isn't it 😄 .
b
Yeah, Matrix... I decided to not support generic types, because it seems to be to compicated to detect the right serializer^^
d
Also, you should use a serialiser module instead of manually using a set. Works better with kotlinx.
👍 1