Question

I was trying to get into the new Scala Pickling library that was presented at the ScalaDays 2013: Scala Pickling

What I am really missing are some simple examples how the library is used.

I understood that I can pickle some object an unpickle it again like that:

import scala.pickling._

val pckl = List(1, 2, 3, 4).pickle
val lst = pckl.unpickle[List[Int]]

In this example, pckl is of the type Pickle. What exactly is the use of this type and how can I get for example get an Array[Byte] of it?

Was it helpful?

Solution

If you want wanted to pickle into bytes, then the code will look like this:

import scala.pickling._
import binary._         
val pckl = List(1, 2, 3, 4).pickle
val bytes = pckl.value

If you wanted json, the code would look almost the exact same with a minor change of imports:

import scala.pickling._
import json._         
val pckl = List(1, 2, 3, 4).pickle
val json = pckl.value

How the object is pickled depends on the import type that you chose under scala.pickling (being either binary or json). Import binary and the value property is an Array[Byte]. Import json and it's a json String.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top