0

I want to save an object (an instance of a class) to a file. I didn't find any valuable example of it. Do I need to use serialization for it?

How do I do that?

UPDATE: Here is how I tried to do that

import scala.util.Marshal
import scala.io.Source
import scala.collection.immutable
import java.io._

object Example {
  class Foo(val message: String) extends scala.Serializable
  val foo = new Foo("qweqwe")                    
  val out = new FileOutputStream("out123.txt")
  out.write(Marshal.dump(foo))
  out.close
}

First of all, out123.txt contains many extra data and it was in a wrong encoding. My gut tells me there should be another proper way.

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205
  • 4
    Yes. The procedure is called serialization. – Nikita Volkov Jul 09 '13 at 03:39
  • 1
    possible duplicate of [Simple, hassle-free, zero-boilerplate serialization in Scala/Java similar to Python's Pickle?](http://stackoverflow.com/questions/7590557/simple-hassle-free-zero-boilerplate-serialization-in-scala-java-similar-to-pyt) – om-nom-nom Jul 09 '13 at 06:26

4 Answers4

6

On the last ScalaDays Heather introduced a new library which gives a new cool mechanism for serialization - pickling. I think it's would be an idiomatic way in scala to use serialization and just what you want.

Check out a paper on this topic, slides and talk on ScalaDays'13

4lex1v
  • 21,367
  • 6
  • 52
  • 86
  • `println(List(42).pickle)` -- what makes it write to a file? – Alan Coromano Jul 10 '13 at 04:18
  • @MariusKavansky nothing, serialization is just for transforming your object graph into the binary form, but writting to some target (file, network) you should handle yourself. Make a typeclass for this with some method like writeTo("filePath") – 4lex1v Jul 10 '13 at 04:53
  • I rather asking about writing to a file to be able to ready it later and it should be written so that a human can read easily. – Alan Coromano Jul 10 '13 at 04:55
  • besides, there is an error in sbt if I follow the instruction from their website, it doesn't find current picking version (SNAPSHOT). – Alan Coromano Jul 10 '13 at 04:57
  • You want to read in a file? Then that's not serialization, what's you need. Serialization is all about transforming your object into binary format – 4lex1v Jul 10 '13 at 05:05
  • what's the name of it then? I have many fields (val) and nested classes in the main class I want to store in a file. – Alan Coromano Jul 10 '13 at 05:17
  • @MariusKavansky sorry, never heard of such thing – 4lex1v Jul 10 '13 at 05:27
  • FWIW, it's possible to use scala/pickling to write to a file. We're working on a stable release + documentation at the moment, but interim instructions for writing JSON to file are [here](https://github.com/scala/pickling/issues/5). Hope it helps :) – Heather Miller Sep 05 '13 at 12:57
2

Take a look at Twitter Chill to handle your serialization: https://github.com/twitter/chill. It's a Scala helper for the Kyro serialization library. The documentation/example on the Github page looks to be sufficient for your needs.

Andy
  • 5,108
  • 3
  • 26
  • 37
2

It is also possible to serialize to and deserialize from JSON using Jackson. A nice wrapper that makes it Scala friendly is Jacks

JSON has the following advantages

  • a simple human readable text
  • a rather efficient format byte wise
  • it can be used directly by Javascript
  • and even be natively stored and queried using a DB like Mongo DB

(Edit) Example Usage

Serializing to JSON:

val json = JacksMapper.writeValueAsString[MyClass](instance)

... and deserializing

val obj = JacksMapper.readValue[MyClass](json)
Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
  • Does not work in Scala 12 :( No release on MVN for 6+ years https://mvnrepository.com/artifact/com.lambdaworks/jacks – ckloan Mar 27 '21 at 21:43
1

Just add my answer here for the convenience of someone like me.

The pickling library, which is mentioned by @4lex1v, only supports Scala 2.10/2.11 but I'm using Scala 2.12. So I'm not able to use it in my project.

And then I find out BooPickle. It supports Scala 2.11 as well as 2.12!

Here's the example:

import boopickle.Default._

val data = Seq("Hello", "World!")
val buf = Pickle.intoBytes(data)
val helloWorld = Unpickle[Seq[String]].fromBytes(buf)

More details please check here.

JavaNoScript
  • 2,345
  • 21
  • 27