Osman Saral
04/10/2023, 8:15 AM{"date": {"$date": 1688649908793} }
This is a global type so I wanted to specify a serializer globally as documented here. Here is what I did:
@Serializable
data class Result(
val date: EDate
)
typealias EDate = @Serializable(EDateSerializer::class) Instant
@Serializable
//@SerialName("EDate")
//@SerialName("Instant")
data class InstantSurrogate(
@SerialName("\$date")
val date: Long
)
//@OptIn(ExperimentalSerializationApi::class)
//@Serializer(forClass = Instant::class)
object EDateSerializer : KSerializer<Instant> {
override val descriptor: SerialDescriptor = InstantSurrogate.serializer().descriptor
override fun serialize(encoder: Encoder, value: Instant) {
val millisecond = value.toEpochMilliseconds()
val eDate = InstantSurrogate(millisecond)
encoder.encodeSerializableValue(InstantSurrogate.serializer(), eDate)
}
override fun deserialize(decoder: Decoder): Instant {
val eDate = decoder.decodeSerializableValue(InstantSurrogate.serializer())
return Instant.fromEpochMilliseconds(eDate.date)
}
}
When I do this, serializer tries to use InstantIso8601Serializer
instead of mine. (I've also tried the commented out lines). Is there something I'm missing here?Adam S
04/10/2023, 12:28 PMobject EDateSerializer : KSerializer<Instant>
to
object EDateSerializer : KSerializer<EDate>
but I suspect that won’t help…Osman Saral
04/10/2023, 4:32 PM