diff --git a/core/src/main/kotlin/com/labijie/caching/kryo/InstantSerializer.kt b/core/src/main/kotlin/com/labijie/caching/kryo/InstantSerializer.kt new file mode 100644 index 0000000..59d4884 --- /dev/null +++ b/core/src/main/kotlin/com/labijie/caching/kryo/InstantSerializer.kt @@ -0,0 +1,22 @@ +/** + * @author Anders Xiao + * @date 2024-06-19 + */ +package com.labijie.caching.kryo + +import com.esotericsoftware.kryo.Kryo +import com.esotericsoftware.kryo.Serializer +import com.esotericsoftware.kryo.io.Input +import com.esotericsoftware.kryo.io.Output +import java.time.Instant + + +object InstantSerializer : Serializer() { + override fun write(kryo: Kryo, output: Output, instant: Instant) { + output.writeLong(instant.toEpochMilli()) + } + + override fun read(kryo: Kryo, input: Input, instantClass: Class): Instant { + return Instant.ofEpochMilli(input.readLong()) + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/labijie/caching/kryo/KryoUtils.kt b/core/src/main/kotlin/com/labijie/caching/kryo/KryoUtils.kt index de98e22..6e17ebf 100644 --- a/core/src/main/kotlin/com/labijie/caching/kryo/KryoUtils.kt +++ b/core/src/main/kotlin/com/labijie/caching/kryo/KryoUtils.kt @@ -76,7 +76,7 @@ object KryoUtils { this.register(Charset::class.java, 73) this.register(URL::class.java, 74) this.register(Duration::class.java, DurationSerializer, 75) - this.register(LocalDateTime::class.java, 76) - this.register(Instant::class.java, 77) + this.register(LocalDateTime::class.java, LocalDateTimeSerializer, 76) + this.register(Instant::class.java, InstantSerializer, 77) } } \ No newline at end of file diff --git a/core/src/main/kotlin/com/labijie/caching/kryo/LocalDateTimeSerializer.kt b/core/src/main/kotlin/com/labijie/caching/kryo/LocalDateTimeSerializer.kt new file mode 100644 index 0000000..0d1f63d --- /dev/null +++ b/core/src/main/kotlin/com/labijie/caching/kryo/LocalDateTimeSerializer.kt @@ -0,0 +1,25 @@ +/** + * @author Anders Xiao + * @date 2024-06-19 + */ +package com.labijie.caching.kryo + +import com.esotericsoftware.kryo.Kryo +import com.esotericsoftware.kryo.Serializer +import com.esotericsoftware.kryo.io.Input +import com.esotericsoftware.kryo.io.Output +import java.time.Instant +import java.time.LocalDateTime +import java.time.ZoneOffset + + +object LocalDateTimeSerializer : Serializer() { + override fun write(kryo: Kryo, output: Output, datetime: LocalDateTime) { + output.writeLong(datetime.toInstant(ZoneOffset.UTC).toEpochMilli()) + } + + override fun read(kryo: Kryo, input: Input, datetime: Class): LocalDateTime { + val instant = Instant.ofEpochMilli(input.readLong()) + return LocalDateTime.ofInstant(instant, ZoneOffset.UTC) + } +} \ No newline at end of file