MessagePack for Scala Build Status

Compact serialization of Scala types in MessagePack format.

Installing

resolvers += Resolver.bintrayRepo("commodityvectors", "commodityvectors-releases")

libraryDependencies += "com.commodityvectors" %% "messagepack" % "1.0.0"

Examples

Auto serialization

The easiest way to start is to use macro-generated serializers.

import com.commodityvectors.messagepack.MessagePack
import MessagePack.auto._

case class Person(name: String, age: Int)

val person = Person("Bob", 30)

val data = MessagePack.pack(person)

val personAgain = MessagePack.unpack[Person](data).get

Custom serialization

However it is always possible to alter the way something is serialized by providing an implicit formatter.

import com.commodityvectors.messagepack.MessagePack
import com.commodityvectors.messagepack.Formatter

case class Person(name: String, age: Int)

implicit object PersonFormatter extends Formatter[Person] {
    
    override def read(unpacker: MessageUnpacker): Person = {
      Person (
        unpacker.unpack[String](),
        unpacker.unpack[Int]()
      )
    }
    
    override def write(value: Person, packer: MessagePacker): Unit = {
      packer.pack(value.name)
      packer.pack(value.age)
    }
}

val person = Person("Bob", 30)

val data = MessagePack.pack(person)

val personAgain = MessagePack.unpack[Person](data).get

More examples

The library supports serializing more complex case classes and sealed trait hierarchies. Just look up unit tests for more examples.

Limitations

  • This library was created to minimize the size of data sent over the wire or stored in transient queues. To reduce a data size it does not store any field names of your classes. What that means is that if you change your DTO classes, you will break binary format compatibility. Take care.
  • When using auto-generated serializers for some complex scenarios you may need to split your DTO models into a separate project. This is a limitation of Scala macro system.