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 | |
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.
6 files changed, 596 insertions, 18 deletions
diff --git a/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Date.kt index 55e1b15f..75a40bcf 100644 --- a/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Date.kt +++ b/kvision-modules/kvision-common-types/src/main/kotlin/pl/treksoft/kvision/types/Date.kt @@ -21,4 +21,15 @@ */ package pl.treksoft.kvision.types +@Deprecated("Compatibility with KVision 1. Use LocalDateTime or OffsetDateTime instead.") expect class Date + +expect class LocalDateTime + +expect class LocalDate + +expect class LocalTime + +expect class OffsetDateTime + +expect class OffsetTime diff --git a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt index 55025512..947bb62b 100644 --- a/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt +++ b/kvision-modules/kvision-remote/src/main/kotlin/pl/treksoft/kvision/types/Date.kt @@ -27,20 +27,72 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.SerialDescriptor import kotlinx.serialization.internal.SerialClassDescImpl import kotlin.js.Date +import kotlin.math.absoluteValue +@Deprecated("Compatibility with KVision 1. Use LocalDateTime or OffsetDateTime instead.") actual typealias Date = Date +actual typealias LocalDateTime = Date + +actual typealias LocalDate = Date + +actual typealias LocalTime = Date + +actual typealias OffsetDateTime = Date + +actual typealias OffsetTime = Date + /** * JSON date serializer. */ -object JsonDateSerializer : KSerializer<Date> { +internal object JsonDateSerializer : KSerializer<Date> { override val descriptor: SerialDescriptor = SerialClassDescImpl("kotlin.js.Date") override fun deserialize(decoder: Decoder): Date { - return Date(decoder.decodeLong()) + return decoder.decodeString().toDateInternal() } override fun serialize(encoder: Encoder, obj: Date) { - encoder.encodeLong(obj.getTime().toLong()) + encoder.encodeString(obj.toStringInternal()) } } + +internal fun String.toDateInternal(): Date { + val dt = this.split(':', 'T', '-', '+') + val utcCheck = this[length - 1] == 'Z' + val ds = if (utcCheck) dt[5].dropLast(1).split(".") else dt[5].split(".") + val tzCheck = this[length - 6] + return if (!utcCheck && tzCheck != '-' && tzCheck != '+') { + Date( + dt[0].toInt(), + dt[1].toInt() - 1, + dt[2].toInt(), + dt[3].toInt(), + dt[4].toInt(), + ds[0].toInt(), + if (ds.size == 2) ds[1].toInt() else 0 + ) + } else { + val sign = if (utcCheck || tzCheck == '+') 1 else -1 + Date( + Date.UTC( + dt[0].toInt(), + dt[1].toInt() - 1, + dt[2].toInt(), + if (utcCheck) { dt[3].toInt() } else { dt[3].toInt() - sign * dt[6].toInt() }, + dt[4].toInt(), + ds[0].toInt(), + if (ds.size == 2) ds[1].toInt() else 0 + ) + ) + } +} + +internal fun Date.toStringInternal(): String { + val tz = this.getTimezoneOffset() / 60 + val sign = if (tz > 0) "-" else "+" + return "" + this.getFullYear() + "-" + ("0" + (this.getMonth() + 1)).takeLast(2) + "-" + + ("0" + this.getDate()).takeLast(2) + "T" + ("0" + this.getHours()).takeLast(2) + ":" + + ("0" + this.getMinutes()).takeLast(2) + ":" + ("0" + this.getSeconds()).takeLast(2) + "." + + this.getMilliseconds() + sign + ("0${tz.absoluteValue}").takeLast(2) + ":00" +} diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Date.kt index 2cdcc76f..c9761b22 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/types/Date.kt +++ b/kvision-modules/kvision-server-jooby/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 + } + } +} 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 + } + } +} 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<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 + } + } +} diff --git a/src/main/kotlin/pl/treksoft/kvision/types/Date.kt b/src/main/kotlin/pl/treksoft/kvision/types/Date.kt index 022eb6a0..165bd723 100644 --- a/src/main/kotlin/pl/treksoft/kvision/types/Date.kt +++ b/src/main/kotlin/pl/treksoft/kvision/types/Date.kt @@ -31,8 +31,19 @@ import kotlin.js.Date 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 = Date +actual typealias LocalDateTime = Date + +actual typealias LocalDate = Date + +actual typealias LocalTime = Date + +actual typealias OffsetDateTime = Date + +actual typealias OffsetTime = Date + /** * Extension function to convert String to Date with a given date format. * @param format date/time format |