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

io.github.wulkanowy.sdk.scrapper.adapter.GradeDateDeserializer.kt Maven / Gradle / Ivy

Go to download

Unified way of retrieving data from the UONET+ register through mobile api and scraping api

There is a newer version: 2.7.0
Show newest version
package io.github.wulkanowy.sdk.scrapper.adapter

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.format.DateTimeFormatter

@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = LocalDate::class)
internal object GradeDateDeserializer : KSerializer {

    private const val SERVER_FORMAT = "dd.MM.yyyy"
    private const val SERVER_FORMAT_2 = "dd.M.yyyy"
    private val formatter = DateTimeFormatter.ofPattern("[$SERVER_FORMAT][$SERVER_FORMAT_2]")

    override fun deserialize(decoder: Decoder): LocalDate {
        val date = when {
            decoder.decodeNotNullMark() -> decoder.decodeString()
            else -> "01.01.1970"
        }

        return LocalDate.parse(date, formatter)
    }

    override fun serialize(encoder: Encoder, value: LocalDate) {
        encoder.encodeString(value.format(formatter))
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy