I tried the following Unit Test.
First test testUnpickleJsonPickleFormat
works well. It pickles a String and unpickle the pickle.
testUnpickleString{1,2}
tests to unserialize a String. But they don't work at all. I don't know where something miss me.
import org.junit.Test
import scala.pickling._
import scala.pickling.json._
class PicklerTest {
@Test
def testUnpickleJsonPickleFormat {
val src = "elem 1"
val pckl = src.pickle
val res = pckl.unpickle[String]
println(src)
println(pckl.toString +"\n")
println(res)
}
@Test
def testUnpickleString1 {
val json = """JSONPickle({
| "tpe": "java.lang.String",
| "value": "elem 1"
| })""".stripMargin.trim
val pckl = JSONPickle(json.toString)
val res = pckl.unpickle[String]
}
@Test
def testUnpickleString2 {
val src = "elem 1"
val pckl = src.pickle
val pckl2 = JSONPickle(pckl.toString)
val res = pckl2.unpickle[String]
println(src)
println(pckl.toString +"\n")
println(res)
}
}
I don't know how to use unpickle with the Scala Pickle framework.
You should use
without
JSONPickle(...)
:JSONPickle({...})
is not a validJSON
.You should also use
pckl.value
instead ofpckl.toString
to getJSON
withoutJSONPickle(...)
:You could also use pattern matching like this: