Scala macros for compile-time generation of Kryo serializers
Scala macros that generate com.esotericsoftware.kryo.Serializer
implementations in compile time, based on compile time reflection.
AnyVal
types & classes,String
, Either
, BigDecimal
, java.time.Instant
,scala.concurrent.duration.FiniteDuration
, org.joda.time.DateTime
Add the following resolver
resolvers += Resolver.bintrayRepo("evolutiongaming", "maven")
Add the library to your dependencies list
libraryDependencies += "com.evolutiongaming" %% "kryo-macros" % "1.3.0"
Generate some serializers for your case classes
import com.evolutiongaming.kryo.Serializer
case class Player(name: String)
val serializer = Serializer.make[Player]
That’s it! You have generated a com.esotericsoftware.kryo.Serializer
implementation for your Player
.
You must know what to do with it if you are here :)
To serialize objects that extends sealed traits/class use Serializer.makeCommon
call:
import com.evolutiongaming.kryo.{ConstSerializer, Serializer}
sealed trait Reason
object Reason {
case object Close extends Reason
case object Pause extends Reason
}
val reasonSerializer = Serializer.makeCommon[Reason] {
case 0 => ConstSerializer(Reason.Close)
case 1 => ConstSerializer(Reason.Pause)
}
sealed abstract class Message(val text: String)
object Message {
case object Common extends Message("common")
case object Notification extends Message("notification")
}
private implicit val messageSerializer = Serializer.makeMapping[Message] {
case 0 => Message.Common
case 1 => Message.Notification
}
To see generated code just add the following line to your sbt build file
scalacOptions += "-Xmacro-settings:print-serializers"
For more examples, please, check out
SerializerMacroSpec
sbt clean +coverage +test +coverageReport +mimaReportBinaryIssues
sbt -no-colors clean 'benchmark/jmh:run -prof gc .*SerializerBenchmark.*' >results.txt
For version numbering use Recommended Versioning Scheme
that is widely adopted in the Scala ecosystem.
Double-check binary & source compatibility and release using following command (credentials required):
sbt release