I am trying to create a custom deserialization for...
# ktor
b
I am trying to create a custom deserialization for an enum. I recognise the enum based on an id. But in the JSON this value can be null, then I would like to take a default enum entry. But here the compile fails if I do the following:
Copy code
override fun deserialize(decoder: Decoder): MyEnum {
        return if (decoder.decodeNotNullMark()) {
            val enumValue = decoder.decodeInt()
            MyEnum.values().firstOrNull {
                it.id == enumValue
            } ?: MyEnum.DEFAULT
        } else {
            MyEnum.DEFAULT
        }
    }
Copy code
org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during file facade code generation
Anybody an idea how I can fix this?
s
An error like that means that it’s the compiler that’s broken, not your code. If you can reproduce it, you could raise an issue at kotl.in/issue 🎫
b
Ok, thanks. Yes I can reproduce it as long as I don’t write “decoder.decodeNull()” in the else block. But then I cannot return a default value.