All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.kevin.api.serializers.LocalDateTimeSerializer.kt Maven / Gradle / Ivy

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()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy