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

app.cybrid.cybrid_api_id.client.infrastructure.LocalDateTimeAdapter.kt Maven / Gradle / Ivy

There is a newer version: 0.122.72
Show newest version
package app.cybrid.cybrid_api_id.client.infrastructure

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import com.google.gson.stream.JsonToken.NULL
import java.io.IOException
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class LocalDateTimeAdapter(private val formatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME) : TypeAdapter() {
    @Throws(IOException::class)
    override fun write(out: JsonWriter?, value: LocalDateTime?) {
        if (value == null) {
            out?.nullValue()
        } else {
            out?.value(formatter.format(value))
        }
    }

    @Throws(IOException::class)
    override fun read(out: JsonReader?): LocalDateTime? {
        out ?: return null

        when (out.peek()) {
            NULL -> {
                out.nextNull()
                return null
            }
            else -> {
                return LocalDateTime.parse(out.nextString(), formatter)
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy