Skip to content

Instantly share code, notes, and snippets.

@timperrett
Created December 31, 2012 04:08
Show Gist options
  • Select an option

  • Save timperrett/4417294 to your computer and use it in GitHub Desktop.

Select an option

Save timperrett/4417294 to your computer and use it in GitHub Desktop.
Kryo2 scala example
package demo
class User(name: String, age: Int)
object Example {
import com.esotericsoftware.kryo.{Kryo,io}, io.{Output,Input}
val kryo = new Kryo
kryo.register(classOf[User])
val user = new User("Tim", 26)
lazy val foo = {
using(new Output(1024)){ output =>
kryo.writeObject(output, user)
output.toBytes
}
}
lazy val bar = {
using(new Input(foo)){ input =>
kryo.readObject(input, classOf[User])
}
}
def using[A <: { def close(): Unit }, B](r: A)(f: A => B): B =
try f(r)
finally r.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment