I would like to be able to use scala pickling in order to store binary representation of a case class.
I would like to know if there is a way to manage versioning of case class (the way protocol buffer allows to do)
Here is my example
I make a program at a certain date, with the following case class
case class MessageTest(a:String,b:String)
Then I serialize an instance of this class
import scala.pickling._
import binary._
val bytes=MessageTest("1","2").pickle
And then I store the result into a file
Later on, I might now have to make evolution on my case class, to add a new optional field
case class MessageTest (a:String,b:String,c:Option[String]=None)
I would like to be able to reuse the data that I stored previously in my file , to deserialize it and be able to recover an instance of a case class (with default value for the new parameter)
But when I use the following code
import scala.pickling._
import binary._
val messageback=bytes.unpickle[MessageTest]
I got the following error :
java.lang.ArrayIndexOutOfBoundsException: 26 at scala.pickling.binary.BinaryPickleReader$$anonfun$2.apply(BinaryPickleFormat.scala:446) at scala.pickling.binary.BinaryPickleReader$$anonfun$2.apply(BinaryPickleFormat.scala:434) at scala.pickling.PickleTools$class.withHints(Tools.scala:498) at scala.pickling.binary.BinaryPickleReader.withHints(BinaryPickleFormat.scala:425) at scala.pickling.binary.BinaryPickleReader.beginEntryNoTagDebug(BinaryPickleFormat.scala:434) at scala.pickling.binary.BinaryPickleReader.beginEntryNoTag(BinaryPickleFormat.scala:431)
Did I do something wrong ?
Is there an existing way to make my scenario work ?
Regards
Well the problem is that you are trying to deserialize back to a different object than what you serialized to.
Consider this. The first object
Now with the changed case object...
There is no way the library can know what you mean in this case because it's just expecting signatures to match up.
https://github.com/scala/pickling/issues/39 but you can at least go one way with it. as demonstrated here.
Hopefully this helps a little.