You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using jsonConfig.encodeToString(Start()) will produce a string { "match": { ... } } with no class discriminator, which afaik is the expected behavior for sealed classes (closed polymorphism). The problem arises when trying to decode from string, where jsonConfig.decodeFromString<Action>(string) will throw an exception regarding the missing discriminator:
Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')
JSON input: {"player":{"id":"ab2b1b63-6c4e","matchID":null,"name":"ab2b1b63-6c4e"}}
Aside from using open polymorphism and forcing the encoder to include a discriminator, what are some solutions to this issue?
Thanks in advance
The text was updated successfully, but these errors were encountered:
That's the common polymorphism pitfall. From the docs:
When serializing polymorphic class hierarchies you must ensure that the compile-time type of the serialized object is a polymorphic one, not a concrete one.
To solve your problem, try to do the following: jsonConfig.encodeToString<Action>(Start())
My polymorphic serializer expects a class discriminator when decoding, but does not include one when encoding.
I have a multi-module project with a client, server, and common modules. In
common
, the Json module is defined for the sealed classAction
like this:Using
jsonConfig.encodeToString(Start())
will produce a string{ "match": { ... } }
with no class discriminator, which afaik is the expected behavior for sealed classes (closed polymorphism). The problem arises when trying to decode from string, wherejsonConfig.decodeFromString<Action>(string)
will throw an exception regarding the missing discriminator:Aside from using open polymorphism and forcing the encoder to include a discriminator, what are some solutions to this issue?
Thanks in advance
The text was updated successfully, but these errors were encountered: