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

Otis Goodman

01/08/2023, 2:45 AM
How do I write a polymorphic serializer for two primitive types? I am trying to write an SDK for an API, and there are responses that can either be an int or a string. Is it possible to make a polymorphic serializer, that will serialize and deserialize API calls?
e

ephemient

01/08/2023, 5:26 AM
easiest way is just to use
JsonElement
you can write a custom serializer that uses
(decoder as JsonDecoder).decodeJsonElement().jsonPrimitive
if you want it deserialized to your own custom type instead
e

Eugen Martynov

01/09/2023, 9:39 AM
Is there a notion in json when it will be a string and when it will be int?
8 Views