eu.kevin.api.serializers.LocalDateTimeSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kevin-jvm Show documentation
Show all versions of kevin-jvm Show documentation
JVM client implementing kevin. platform API
The newest version!
package eu.kevin.api.serializers
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException
@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = LocalDateTime::class)
object LocalDateTimeSerializer : KSerializer {
private val dateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private val dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd")
override fun serialize(encoder: Encoder, value: LocalDateTime) {
encoder.encodeString(value.format(dateTimeFormat))
}
override fun deserialize(decoder: Decoder): LocalDateTime {
return try {
LocalDateTime.parse(decoder.decodeString(), dateTimeFormat)
} catch (exception: DateTimeParseException) {
LocalDate.parse(decoder.decodeString(), dateFormat).atStartOfDay()
}
}
}