Hi, I’m trying to implement a serializer for a dif...
# serialization
o
Hi, I’m trying to implement a serializer for a different kind of date format. which looks like this
{"date": {"$date": 1688649908793} }
This is a global type so I wanted to specify a serializer globally as documented here. Here is what I did:
Copy code
@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?
a
try changing
Copy code
object EDateSerializer : KSerializer<Instant>
to
Copy code
object EDateSerializer : KSerializer<EDate>
but I suspect that won’t help…
o
no it didn't. but thanks