Skip to content

Commit

Permalink
move kryo to core
Browse files Browse the repository at this point in the history
  • Loading branch information
endink committed Jun 19, 2024
1 parent 8577029 commit 5b335bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
22 changes: 22 additions & 0 deletions core/src/main/kotlin/com/labijie/caching/kryo/InstantSerializer.kt
Original file line number Diff line number Diff line change
@@ -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<Instant>() {
override fun write(kryo: Kryo, output: Output, instant: Instant) {
output.writeLong(instant.toEpochMilli())
}

override fun read(kryo: Kryo, input: Input, instantClass: Class<out Instant>): Instant {
return Instant.ofEpochMilli(input.readLong())
}
}
4 changes: 2 additions & 2 deletions core/src/main/kotlin/com/labijie/caching/kryo/KryoUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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<LocalDateTime>() {
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<out LocalDateTime>): LocalDateTime {
val instant = Instant.ofEpochMilli(input.readLong())
return LocalDateTime.ofInstant(instant, ZoneOffset.UTC)
}
}

0 comments on commit 5b335bc

Please sign in to comment.