Search code examples
scalaserializationtwittercase-classkryo

Handling case classes in twitter chill (Scala interface to Kryo)?


Twitter-chill looks like a good solution to the problem of how to serialize efficiently in Scala without excessive boilerplate.

However, I don't see any evidence of how they handle case classes. Does this just work automatically or does something need to be done (e.g. creating a zero-arg constructor)?

I have some experience with the WireFormat serialization mechanism built into Scoobi, which is a Scala Hadoop wrapper similar to Scalding. They have serializers for case classes up to 22 arguments that use the apply and unapply methods and do type matching on the arguments to these functions to retrieve the types. (This might not be necessary in Kryo/chill.)


Solution

  • They generally just work (as long as the component members are also serializable by Kryo):

    case class Foo(id: Int, name: String)
    
    // setup
    val instantiator = new ScalaKryoInstantiator
    instantiator.setRegistrationRequired(false)
    val kryo = instantiator.newKryo()
    
    // write
    val data = Foo(1,"bob")
    val buffer = new Array[Byte](4096]
    val output = new Output(buffer)
    kryo.writeObject(output, data)
    
    // read
    val input = new Input(buffer)
    val data2 = kryo.readObject(input,classOf[Foo]).asInstanceOf[Foo]