diff options
author | Robert Jaros <rjaros@finn.pl> | 2019-10-12 18:24:15 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2019-10-12 18:24:15 +0200 |
commit | 6ea600defefbe16e59c421785d9a09989e672083 (patch) | |
tree | 7f3344b56f9c3fe1f8c65bf67195539a149af15f /kvision-modules/kvision-server-ktor | |
parent | bcf7504392baccf1568e740c1d453eac7080fb5b (diff) | |
download | kvision-6ea600defefbe16e59c421785d9a09989e672083.tar.gz kvision-6ea600defefbe16e59c421785d9a09989e672083.tar.bz2 kvision-6ea600defefbe16e59c421785d9a09989e672083.zip |
Redesign Date type handling in server side interfaces.
Map js.Date class on the client side to different java.time.* classes on the server side.
Diffstat (limited to 'kvision-modules/kvision-server-ktor')
-rw-r--r-- | kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/types/Date.kt | 178 |
1 files changed, 173 insertions, 5 deletions
diff --git a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/types/Date.kt index 2cdcc76f..c9761b22 100644 --- a/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/types/Date.kt +++ b/kvision-modules/kvision-server-ktor/src/main/kotlin/pl/treksoft/kvision/types/Date.kt @@ -21,12 +21,180 @@ */ package pl.treksoft.kvision.types -import java.text.SimpleDateFormat +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonDeserializer +import com.fasterxml.jackson.databind.JsonSerializer +import com.fasterxml.jackson.databind.SerializerProvider +import java.io.IOException +import java.time.LocalDate +import java.time.LocalDateTime +import java.time.LocalTime +import java.time.OffsetDateTime +import java.time.OffsetTime +import java.time.format.DateTimeFormatter +import java.time.format.DateTimeParseException -const val KV_DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss" +@Deprecated("Compatibility with KVision 1. Use LocalDateTime or OffsetDateTime instead.") +actual typealias Date = LocalDateTime -actual typealias Date = java.util.Date +actual typealias LocalDateTime = LocalDateTime -fun String.toDateF(format: String = KV_DEFAULT_DATE_FORMAT): Date = SimpleDateFormat(format).parse(this) +actual typealias LocalDate = LocalDate -fun Date.toStringF(format: String = KV_DEFAULT_DATE_FORMAT): String = SimpleDateFormat(format).format(this) +actual typealias LocalTime = LocalTime + +actual typealias OffsetDateTime = OffsetDateTime + +actual typealias OffsetTime = OffsetTime + +fun String.toDateTimeF(): LocalDateTime = LocalDateTime.parse(this) + +fun String.toDateF(): LocalDate = LocalDate.parse(this) + +fun String.toTimeF(): LocalTime = LocalTime.parse(this) + +fun String.toOffsetDateTimeF(): OffsetDateTime = OffsetDateTime.parse(this) + +fun String.toOffsetTimeF(): OffsetTime = OffsetTime.parse(this) + +fun LocalDateTime.toStringF(): String = this.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + +fun LocalDate.toStringF(): String = this.format(DateTimeFormatter.ISO_LOCAL_DATE) + +fun LocalTime.toStringF(): String = this.format(DateTimeFormatter.ISO_LOCAL_TIME) + +fun OffsetDateTime.toStringF(): String = this.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) + +fun OffsetTime.toStringF(): String = this.format(DateTimeFormatter.ISO_OFFSET_TIME) + +class LocalDateTimeSerializer : JsonSerializer<LocalDateTime>() { + @Throws(IOException::class) + override fun serialize(value: LocalDateTime, gen: JsonGenerator, provider: SerializerProvider) { + try { + val s = value.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + gen.writeString(s) + } catch (e: DateTimeParseException) { + System.err.println(e) + gen.writeString("") + } + } +} + +class LocalDateTimeDeserializer : JsonDeserializer<LocalDateTime>() { + @Throws(IOException::class) + override fun deserialize(p: JsonParser, ctx: DeserializationContext): LocalDateTime? { + val str = p.text + try { + return LocalDateTime.parse(str.dropLast(6), DateTimeFormatter.ISO_LOCAL_DATE_TIME) + } catch (e: DateTimeParseException) { + System.err.println(e) + return null + } + } +} + +class LocalDateSerializer : JsonSerializer<LocalDate>() { + @Throws(IOException::class) + override fun serialize(value: LocalDate, gen: JsonGenerator, provider: SerializerProvider) { + try { + val s = value.atStartOfDay().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + gen.writeString(s) + } catch (e: DateTimeParseException) { + System.err.println(e) + gen.writeString("") + } + } +} + +class LocalDateDeserializer : JsonDeserializer<LocalDate>() { + @Throws(IOException::class) + override fun deserialize(p: JsonParser, ctx: DeserializationContext): LocalDate? { + val str = p.text + try { + return LocalDate.parse(str.dropLast(6), DateTimeFormatter.ISO_LOCAL_DATE_TIME) + } catch (e: DateTimeParseException) { + System.err.println(e) + return null + } + } +} + +class LocalTimeSerializer : JsonSerializer<LocalTime>() { + @Throws(IOException::class) + override fun serialize(value: LocalTime, gen: JsonGenerator, provider: SerializerProvider) { + try { + val s = value.atDate(LocalDate.now()).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + gen.writeString(s) + } catch (e: DateTimeParseException) { + System.err.println(e) + gen.writeString("") + } + } +} + +class LocalTimeDeserializer : JsonDeserializer<LocalTime>() { + @Throws(IOException::class) + override fun deserialize(p: JsonParser, ctx: DeserializationContext): LocalTime? { + val str = p.text + try { + return LocalTime.parse(str.dropLast(6), DateTimeFormatter.ISO_LOCAL_DATE_TIME) + } catch (e: DateTimeParseException) { + System.err.println(e) + return null + } + } +} + +class OffsetDateTimeSerializer : JsonSerializer<OffsetDateTime>() { + @Throws(IOException::class) + override fun serialize(value: OffsetDateTime, gen: JsonGenerator, provider: SerializerProvider) { + try { + val s = value.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) + gen.writeString(s) + } catch (e: DateTimeParseException) { + System.err.println(e) + gen.writeString("") + } + } +} + +class OffsetDateTimeDeserializer : JsonDeserializer<OffsetDateTime>() { + @Throws(IOException::class) + override fun deserialize(p: JsonParser, ctx: DeserializationContext): OffsetDateTime? { + val str = p.text + try { + return OffsetDateTime.parse(str, DateTimeFormatter.ISO_OFFSET_DATE_TIME) + } catch (e: DateTimeParseException) { + System.err.println(e) + return null + } + } +} + +class OffsetTimeSerializer : JsonSerializer<OffsetTime>() { + @Throws(IOException::class) + override fun serialize(value: OffsetTime, gen: JsonGenerator, provider: SerializerProvider) { + try { + val s = value.atDate(LocalDate.now()).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) + gen.writeString(s) + } catch (e: DateTimeParseException) { + System.err.println(e) + gen.writeString("") + } + } +} + +class OffsetTimeDeserializer : JsonDeserializer<OffsetTime>() { + @Throws(IOException::class) + override fun deserialize(p: JsonParser, ctx: DeserializationContext): OffsetTime? { + val str = p.text + try { + return OffsetTime.parse(str, DateTimeFormatter.ISO_OFFSET_DATE_TIME) + } catch (e: DateTimeParseException) { + System.err.println(e) + return null + } + } +} |