I am having an issue with my brain while using Kot...
# announcements
r
I am having an issue with my brain while using KotlinX. I have a few separate classes in a project that parses json from an outside source. There are a few separate flag fields that are mostly human readable (using strings) but some are the bit shifted int value of the flag and do not have any documentation on what they are named. My question is: How do I write a generic serializer that can take both an
Int
and a
String
and output a generic class? How would I do something that essentially looks like this but actually works? Because this currently does NOT work for me
Copy code
open class FlagSerDes<T>(
    val decodeFromString: (String) -> T,
    val getEncodeString: (T) -> String
): KSerializer<T> {
    override fun deserialize(decoder: Decoder): T = decodeFromString(decoder.decodeString())

    override fun serialize(encoder: Encoder, value: T) {
        encoder.encodeString(getEncodeString(value))
    }
}

class Flag1(val flags: Int) {
    @Serializer(forClass = Flag1::class)
    companion object: FlagSerDes<Flag1>( { Flag1(it.toInt()) }, { it.flags.toString() }
}


class Flag2(val flags: Int) {
    @serializer(forClass = Flag2::class)
   companion object: FlagSerDes<Flag2>( { Flag2(it.toInt()) }, { it.flags.toString() }
}