Pickling (or serializing) an object is as easy as:
import scala.pickling._ import json._ val pckl = List(1, 2, 3, 4).pickle
Unpickling is just as easy:
val lst = pckl.unpickle[List[Int]]
Details of the pickling framework can be found in our draft paper (under review):Instant Pickles: Generating Object-Oriented Pickler Combinators for Fast and Extensible Serialization, by Heather Miller, Philipp Haller, Eugene Burmako, and Martin Odersky. In OOPSLA'13, Indianapolis, IN, USA, October 26-31 2013.
On Pickles and Spores: Improving Support for Distributed Programming in Scala,
by Heather Miller, June 13th 2013
Strange Loop 2013
Spores: Distributable Functions in Scala,
by Heather Miller, September 18th 2013
(this talk covers mostly spores, closures that you can serialize and distribute in Scala)
Scala Pickling for Scala 2.10.3 is available on Sonatype! You can find Scala Pickling under groupID:
org.scala-lang and artifactID:
scala-pickling_2.10. The current version is 0.8.0-SNAPSHOT.
You can use Scala Pickling in your SBT project by simply adding the following dependency to your build file:
libraryDependencies += "org.scala-lang" %% "scala-pickling" % "0.8.0-SNAPSHOT"
For a more illustrative example, see a sample SBT project which uses Scala Pickling.
Or you can just directly download the jar.
What Makes It Different?
- … can be language-neutral if you want it to be. Changing the format of your serialized data is as easy as importing the correct implicit pickle format into scope. Out of the box, we currently support a fast Scala binary format, as well as JSON. Support is currently planned for other formats. Or, you can even roll your own custom pickle format!
- … is automatic. That is, without any boilerplate at all, one can instruct the framework to figure out how to serialize an arbitrary class instance. No need to register classes, no need to implement any methods.
- … allows for unanticipated evolution. That means that you don’t have to extend some marker trait in order to serialize a given Scala class. Just import the
scala.picklingpackage and call
pickleon the instance that you would like to serialize.
- … gives you more typesafety. No more errors from serialization/deserialization propagating to arbitrary points in your program. Unlike Java Serialization, errors either manifest themselves as compile-time errors, or runtime errors only at the point of unpickling.
- … has robust support for object-orientation. While Scala Pickling is based on the elegant notion of pickler combinators from functional programming, it goes on to extend pickler combinators to be able to handle subtyping, a notion which does not exist in the purely functional paradigm. That means that if you pickle an instance of a subclass, and then try to unpickle as an instance of a superclass, you will still get back the original subclass which you initially pickled.
- … happens at compile-time. That means that it’s super-performant because serialization-related code is typically generated at compile-time and inlined where it is needed in your code. Scala Pickling is essentially fully-static, reflection is only used as a fallback when static (compile-time) generation fails.