Hi folks, there is anyway to add a new annotation ...
# serialization
s
Hi folks, there is anyway to add a new annotation and use to map primitive types. Example:
@Abs val num: Int
=>
{"num": -3}
that deserialize to 3. I already create my owns annotation for Kotlinx.serialization, I know that supports it, but was for a new Enconder of mine, not from a existing one like Json.
d
You want a transformation. You'll probably just want to write a thin custom serializer that call
abs
. Then annotate the property with the it.
Copy code
object AbsSerializer : KSerializer<Int> {
    override val descriptor = PrimitiveDescriptor("AbsoluteInteger", <http://PrimitiveKind.INT|PrimitiveKind.INT>)

    override fun deserialize(decoder: Decoder): Int {
        return abs(decoder.decodeInt())
    }

    override fun serialize(encoder: Encoder, value: Int) {
        encoder.encodeInt(value)
    }
}
@Serializable(with=AbsSerializer::class) val num: Int
s
This could solve, tkx Dominic