Joel Steres
04/29/2022, 12:46 AMJoel Steres
04/29/2022, 12:47 AMclass DateFixer: JsonTransformingSerializer<Instant>(Instant.serializer()) {
override fun transformDeserialize(element: JsonElement): JsonElement = JsonPrimitive(fixBasicTz(element.jsonPrimitive.content))
}
@Serializable
data class MyData(
@Serializable(with = DateFixer::class)
val date: Instant? = null
)
It makes sense that with Properties
I get the error
This serializer can be used only with Json format.Expected Encoder to be JsonEncoder, got class kotlinx.serialization.properties.Properties$OutAnyMapper (Kotlin reflection is not available)
I started using JsonTransformingSerializer
as a model to try to write a generic KSerializer but I quickly see it uses Json specific encoders and decoders. I am wondering if there is an easy way to write a generic transformer.Joel Steres
04/29/2022, 1:06 AMclass DateFixer: KSerializer<Instant> {
private val tSerializer = Instant.serializer()
override val descriptor: SerialDescriptor get() = tSerializer.descriptor
override fun deserialize(decoder: Decoder): Instant {
return Instant.parse(fixBasicTz(decoder.decodeString()))
}
override fun serialize(encoder: Encoder, value: Instant) {
encoder.encodeSerializableValue(tSerializer, value)
}
}
Joel Steres
04/29/2022, 5:40 AMInstant
types so that I don’t have to be sure to decorate each occurrence in a serializable class?ephemient
04/30/2022, 3:21 AM@file:UseSerializers(DateFixer::class)
at the top of a file will apply to all Instant
types (that are not otherwise annotated) in serializable classes defined within the same fileJoel Steres
04/30/2022, 5:11 AMJoel Steres
04/30/2022, 5:33 AMJoel Steres
04/30/2022, 5:38 AMephemient
04/30/2022, 6:44 AMephemient
04/30/2022, 6:45 AM@Contextual
annotation on all the use sites