Created
April 22, 2015 10:54
-
-
Save trumanw/c56e257f0bbc9b1d8e6a to your computer and use it in GitHub Desktop.
Serializable vs Pickle vs MsgPack (file input & output)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name := "SerialCmp" | |
| version := "0.1.0" | |
| scalaVersion := "2.10.4" | |
| libraryDependencies += "org.scala-lang.modules" %% "scala-pickling" % "0.10.0" | |
| libraryDependencies += "org.msgpack" %% "msgpack-scala" % "0.6.11" | |
| libraryDependencies += "org.slf4j" % "slf4j-api" % "1.6.4" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io._ | |
| // Pickling binary | |
| import scala.pickling._ | |
| import binary._ | |
| import scala.pickling.Defaults._ | |
| // msgpack | |
| import org.msgpack.annotation.Message | |
| import org.msgpack.ScalaMessagePack | |
| @Message | |
| @SerialVersionUID(123L) | |
| class Brotherhood( | |
| var name: String, | |
| var brothers: Option[Seq[Brotherhood]]) | |
| extends Serializable { | |
| def this() = this("", None) | |
| override def toString = { | |
| var brothersPackSize = 0 | |
| brothers.foreach { hood => | |
| brothersPackSize = hood.length | |
| } | |
| f"Brotherhood $name%s has $brothersPackSize%d brothers." | |
| } | |
| } | |
| object SerializationDemo extends App { | |
| val tom = new Brotherhood("Tom", None) | |
| val smith = new Brotherhood("Smith", None) | |
| val richard = new Brotherhood("Richard", None) | |
| val brothers = new Brotherhood("Assassin", Option(Seq(tom, smith, richard))) | |
| pickleTo(brothers, "PickleBinaryFile") | |
| msgPackTo(brothers, "MsgPackBinaryFile") | |
| serialTo(brothers, "SerialBinaryFile") | |
| // deserialize | |
| val brotherhoodUnpickle = unpickleFrom("PickleBinaryFile") | |
| println("Pickle from file: ") | |
| println(brotherhoodUnpickle) | |
| val brotherhoodMsgUnpack = msgUnpackFrom("MsgPackBinaryFile") | |
| println("MsgPack from file: ") | |
| println(brotherhoodMsgUnpack) | |
| val brotherhoodDeserialized = deserialFrom("SerialBinaryFile") | |
| println("Deserialize from file: ") | |
| println(brotherhoodDeserialized) | |
| def pickleTo(brothers: Brotherhood, filename: String) = { | |
| // convert to Pickle binary object | |
| val brothersPickle = brothers.pickle | |
| // get the pickle bytes array | |
| val brothersPickleByteArray = brothersPickle.value | |
| // write bytes array into the file | |
| val fos = new FileOutputStream("./" + filename) | |
| fos.write(brothersPickleByteArray) | |
| fos.close | |
| } | |
| def msgPackTo(brothers: Brotherhood, filename: String) = { | |
| // convert brother hood into msgpack bytes array | |
| val brothersMsgPackBytes = ScalaMessagePack.write(brothers) | |
| // write bytes array into the file | |
| val fos = new FileOutputStream("./" + filename) | |
| fos.write(brothersMsgPackBytes) | |
| fos.close | |
| } | |
| def serialTo(brothers: Brotherhood, filename: String) = { | |
| val oos = new ObjectOutputStream(new FileOutputStream("./" + filename)) | |
| oos.writeObject(brothers) | |
| oos.close | |
| } | |
| def unpickleFrom(filename: String): Brotherhood = { | |
| val brothersRawFromFile = scala.io.Source.fromFile("./" + filename).map(_.toByte).toArray | |
| val brothersUnpickleValue = BinaryPickle(brothersRawFromFile) | |
| val brothersUnpickle = brothersUnpickleValue.unpickle[Brotherhood] | |
| brothersUnpickle | |
| } | |
| def msgUnpackFrom(filename: String): Brotherhood = { | |
| val fio = new FileInputStream("./" + filename) | |
| val brothersMsgUnpack = ScalaMessagePack.read[Brotherhood](fio) | |
| brothersMsgUnpack | |
| } | |
| def deserialFrom(filename: String): Brotherhood = { | |
| val ois = new ObjectInputStream(new FileInputStream("./" + filename)) | |
| val brothersDeserial = ois.readObject().asInstanceOf[Brotherhood] | |
| ois.close | |
| brothersDeserial | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment