From 6ea600defefbe16e59c421785d9a09989e672083 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sat, 12 Oct 2019 18:24:15 +0200 Subject: 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. --- .../main/kotlin/pl/treksoft/kvision/types/Date.kt | 178 ++++++++++++++++++++- 1 file changed, 173 insertions(+), 5 deletions(-) (limited to 'kvision-modules/kvision-server-spring-boot') diff --git a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/types/Date.kt index 2cdcc76f..c9761b22 100644 --- a/kvision-modules/kvision-server-spring-boot/src/main/kotlin/pl/treksoft/kvision/types/Date.kt +++ b/kvision-modules/kvision-server-spring-boot/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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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() { + @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 + } + } +} -- cgit