gpt4 book ai didi

Scala pickling 案例类版本控制

转载 作者:行者123 更新时间:2023-12-03 01:25:41 26 4
gpt4 key购买 nike

我希望能够使用 scala pickling 来存储案例类的二进制表示。

我想知道是否有一种方法可以管理案例类的版本控制( Protocol Buffer 允许这样做)

<小时/>

这是我的例子

我在某个日期制作一个程序,具有以下案例类

case class MessageTest(a:String,b:String) 

然后我序列化此类的实例

import scala.pickling._
import binary._
val bytes=MessageTest("1","2").pickle

然后我将结果存储到文件中

<小时/>

稍后,我现在可能必须对我的案例类进行改进,以添加新的可选字段

case class MessageTest (a:String,b:String,c:Option[String]=None)

我希望能够重用之前存储在文件中的数据,将其反序列化并能够恢复案例类的实例(使用新参数的默认值)

但是当我使用以下代码时

import scala.pickling._
import binary._
val messageback=bytes.unpickle[MessageTest]

我收到以下错误:

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)

<小时/>

我做错了什么吗?

有现有的方法可以让我的场景发挥作用吗?

问候

最佳答案

问题是您正在尝试反序列化回与序列化到的对象不同的对象。

考虑一下这一点。第一个对象

scala> case class MessageTest(a: String, b:String)
defined class MessageTest

scala> val bytes = MessageTest("a", "b").pickle
bytes: pickling.binary.pickleFormat.PickleType = BinaryPickle([0,0,0,81,36,108,105,110,101,53,49,46,36,114,101,97,100,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,77,101,115,115,97,103,101,84,101,115,116,0,0,0,1,97,0,0,0,1,98])

现在更改了 case 对象...

scala> case class MessageTest(a: String, b: String, c: Option[String] = None)
defined class MessageTest

scala> val bytes = MessageTest("a", "b").pickle
bytes: pickling.binary.pickleFormat.PickleType = BinaryPickle([0,0,0,81,36,108,105,110,101,53,51,46,36,114,101,97,100,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,36,105,119,46,77,101,115,115,97,103,101,84,101,115,116,0,0,0,1,97,0,0,0,1,98,0,0,0,15,115,99,97,108,97,46,78,111,110,101,46,116,121,112,101])

在这种情况下,图书馆无法知道您的意思,因为它只是期望签名匹配。

https://github.com/scala/pickling/issues/39但你至少可以采取一种方式。如此处所示。

import scala.pickling._
import scala.pickling.Defaults._
import scala.pickling.binary._

case class LegacyMessage(a: String, b: String)
case class Message(a: String, b: String, c: Option[String] = None)

implicit val legacyUnpickler = Unpickler.generate[LegacyMessage]
implicit val messageUnpickler = Unpickler.generate[Message]

val legacyBytes = LegacyMessage("a", "b")
val msgBytes = Message("a", "b", None)

val pickledBytes = msgBytes.pickle
val pickledLegacy = legacyBytes.pickle

// New Message can Serialize back to Legacy Messages
val newToOld = pickledBytes.unpickle[LegacyMessage]

// Old Messages can not serialize up to the new message schema
// println(pickledLegacy.unpickle[Message])

val old = pickledLegacy.unpickle[LegacyMessage]

if(newToOld == old){
println(true)
}

希望这能有所帮助。

关于Scala pickling 案例类版本控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25960694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com