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
Dominaezzz
04/17/2020, 5:00 PM
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.
Dominaezzz
04/17/2020, 5:02 PM
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)
}
}
Dominaezzz
04/17/2020, 5:03 PM
@Serializable(with=AbsSerializer::class) val num: Int