Skip to content

Instantly share code, notes, and snippets.

@jboner
Created July 27, 2009 16:59
Show Gist options
  • Save jboner/156629 to your computer and use it in GitHub Desktop.
Save jboner/156629 to your computer and use it in GitHub Desktop.
Example of Akka's binary Scala serialization protocol
case class User(val usernamePassword: Tuple2[String, String],
val email: String,
val age: Int)
extends Serializable.SBinary[User] {
def this() = this(null, null, 0)
import sbinary.DefaultProtocol._
implicit object UserFormat extends Format[User] {
def reads(in : Input) = User(
read[Tuple2[String, String]](in),
read[String](in),
read[Int](in))
def writes(out: Output, value: User) = {
write[Tuple2[String, String]](out, value.usernamePassword)
write[String](out, value.email)
write[Int](out, value.age)
}
}
def fromBytes(bytes: Array[Byte]) = fromByteArray[User](bytes)
def toBytes: Array[Byte] = toByteArray(this)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment