Hi there, is there a way to change the way an Enum...
# serialization
l
Hi there, is there a way to change the way an Enum is serialized / deserialized when you can't modify it? I want to change the way the deserialization works to transform an unknown enum value to "MyEnum.OTHER" instead of throwing an exception. I implemented my own KSerializer(MyEnum), registered it in
Json { serializersModule = SerializersModule { contextual(MyEnum::class, MyEnumSerializer) } }
, but kotlinx.serialiaztion keeps using
kotlinx.serialization.internal.EnumSerializer
instead of the serializer I registered. Is there a way to force the use of my serializer ?
1
e
Copy code
@Serializable
data class Foo(
    // uses the default enum serializer
    val myEnum1: MyEnum,

    // uses the contextual serializer set up in the SerializersModule
    @Contextual
    val myEnum2: MyEnum,

    // uses a specific serializer
    @Serializable(with = MyEnumSerializer::class)
    val myEnum3: MyEnum,
)
l
Thank you, turns out that my serializer generator did not add any annotations on the enum fields, so it kept using the built-in one. My bad, thank you for the clarifications 🙂