I'm making my own Implementation of AbstractEncode...
# serialization
a
I'm making my own Implementation of AbstractEncoder, Trying to ignore specific interface when implementing Encoder I have an interface
Copy code
interface HasId {
    val Id: Int
}
and various classes implementing it:
Copy code
data class A(val j: Int, override val Id: Int) : HasId 
data class B(val bunchOfOtherStuff : Bunch, val k: Int, override val Id: Int) : HasId
I want to ignore this element. for example:
Copy code
class MyEncoder : AbstractEncoder { ...
    override fun encodeElement(descriptor: SerialDescriptor, index: Int): Boolean {
        val elem = descriptor.getElementDescriptor(index)
        if (elem.kind == <http://PrimitiveKind.INT|PrimitiveKind.INT> && descriptor.getElementName(index) == "Id") {
            println("skip Serializing ${elem.serialName}")
        }
        return !(elem.kind == <http://PrimitiveKind.INT|PrimitiveKind.INT> && descriptor.getElementName(index) == "Id")

    }
}
but that seems like a too weak condition. Is there a better way to implement this? given that I have an interface for indication I thought of something in the direction of:
Copy code
override fun <T : Any?> encodeSerializableElement(
        descriptor: SerialDescriptor,
        index: Int,
        serializer: SerializationStrategy<T>,
        value: T
    ) {
        if (value is HasId) {
            //pass a descriptor that will ignore it
        }
        else super.encodeSerializableElement(...)

    }
b
In general the encoder should never look at the value itself when deciding what to do. You could directly compare the serializer or the descriptor though.
if (serializer == HasId.serializer())
a
thanks. about comparing the serializer, the serializer passed to serialize Id, won't be HasId.serializer(), will be an Int.serializer instead. Any other way comes to mind? Maybe something from the serialDescriptor?