tylerwilson
03/04/2020, 8:02 PM@Serializer(forClass = Boolean::class)
object BooleanSerializer: KSerializer<Boolean> {
override val descriptor: SerialDescriptor = //StringDescriptor.withName("Format")
PrimitiveDescriptor("BooleanSerializer", PrimitiveKind.BOOLEAN)
override fun serialize(encoder: Encoder, obj: Boolean) {
encoder.encodeInt(if (obj) 1 else 0)
}
override fun deserialize(decoder: Decoder): Boolean {
val stringValue = decoder.decodeString()
return stringValue == "Y" || stringValue == "1" || stringValue == "true"
}
}
You can see the old definition for the descriptor of StringDescriptor, but I changed it to a PrimitiveDescriptor. I am still unclear what I should give as the serialName (does it include the package name, or just any name we want) and the PrimitiveKind (should this be a Boolean or a String in this case).
Thank you to anyone who can edify me.Vsevolod Tolstopyatov [JB]
03/04/2020, 8:38 PMPrimitiveKind
— it is recommended to be a <http://PrimitiveKind.INT|PrimitiveKind.INT>
, because you actually write an int and represent your boolean as a primitive inttylerwilson
03/04/2020, 9:36 PMtylerwilson
03/04/2020, 9:37 PMtylerwilson
03/04/2020, 9:40 PM