Giter Club home page Giter Club logo

kryo-macros's Introduction

Kryo Macros Build Status license version

Scala macros that generate com.esotericsoftware.kryo.Serializer implementations in compile time, based on compile time reflection.

Features and limitations

  • On top level only case classes are supported
  • Fields of case classes can be other case classes, Scala collections, options, primitive or AnyVal types & classes, tuples, Scala enums, standard types & classes: String, Either, BigDecimal, java.time.Instant, scala.concurrent.duration.FiniteDuration, org.joda.time.DateTime
  • Fields can be annotated as transient or just be not defined in constructor to avoid parsing and serializing
  • For nested structures need to generate serializers for all case classes
  • Implicitly defined mapping helpers are supported for ADT structures, simple alternative mappings, etc.
  • Manual serializers can be used in generated code when defined as implicits

How to use

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

How to develop

Run tests, check coverage & binary compatibility for both supported Scala versions

sbt clean +coverage +test +coverageReport +mimaReportBinaryIssues

Run benchmarks

sbt -no-colors clean 'benchmark/jmh:run -prof gc .*SerializerBenchmark.*' >results.txt

Release

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

kryo-macros's People

Contributors

fancywriter avatar juanmirocks avatar migesok avatar mr-git avatar olegkovalenko avatar plokhotnyuk avatar scala-steward avatar stasimus avatar t3hnar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kryo-macros's Issues

Ordering is lost

When serializing a TreeMap (and probably TreeSet), the Ordering is lost silently.

OOM on some BigDecimal inputs during serialization

For a serializer generated for a case class which contains a BigDecimal field, if a number with sufficiently big scale supplied (like BigDecimal("3E1000000000")), serialization logic blows in memory causing OOM.
This could be a possible source for DoS attacks.

Add support of any types on top-level

Use cases:

  • to allow serialization of collections and standard types on top-level and avoid redundant wrapping by case classes
  • to allow reuse generated serializers for collections and standard types in custom codecs

More compact binary representation

Consider more compact binary representation for:

  • smaller than Int primitive types (Byte, Short, Char, Boolean)
  • use variable length representation for primitive types and their collections (see current BitSet serializer implementation)

Publish to JCenter

Since this is already in Bintray, it should take a button click to have this automatically publish to JCenter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.