Assaf Shouval
07/26/2023, 11:49 AMinterface HasId {
val Id: Int
}
and various classes implementing it:
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:
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:
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(...)
}
Ben Woodworth
07/26/2023, 4:22 PMif (serializer == HasId.serializer())
Assaf Shouval
07/27/2023, 9:51 AM