我最近遇到了 scala-pickling,我正在尝试了解如何在项目中使用它,所以我一直在研究一个带有不可变哈希图的案例类的简单示例。在这个例子中,scala-pickling 不会生成 unpickler,我不知道为什么。以下是演示该问题的 REPL 会话:
scala> case class Foo(a: HashMap[Symbol,Symbol], b: HashMap[Symbol,Double], c: Symbol, d: Double)
defined class Foo
scala> val bar = Foo(new HashMap[Symbol,Symbol](), new HashMap[Symbol,Double](), 'A, 1.4)
bar: Foo = Foo(Map(),Map(),'A,1.4)
scala> val pickled = bar.pickle
pickled: scala.pickling.json.JSONPickle =
JSONPickle({
"tpe": "Foo",
"c": {
"name": "A"
},
"d": 1.4,
"a": {
},
"b": {
}
})
scala> val unpickled = pickled.unpickle[Foo]
<console>:18: error: Cannot generate an unpickler for Foo. Recompile with -Xlog-implicits for details
val unpickled = pickled.unpickle[Foo]
谁能指出我做错了什么?或者 scala-pickling 有什么问题吗?
编辑:实际上,当我生成一个具有一个属性的类时,似乎也会发生同样的情况,该属性只是一个符号(我将发布另一个 REPL 会话)。有没有一种特殊的方法来处理 scala-pickling 中的符号?
scala> case class Foo(symb: Symbol)
defined class Foo
scala> val foo = Foo('A)
foo: Foo = Foo('A)
scala> val pick = foo.pickle
pick: scala.pickling.json.JSONPickle =
JSONPickle({
"tpe": "Foo",
"symb": {
"name": "A"
}
})
scala> val unpick = pick.unpickle[Foo]
<console>:17: error: Cannot generate an unpickler for Foo. Recompile with -Xlog-implicits for details
val unpick = pick.unpickle[Foo]