我最近遇到了scala-pickling,我正在尝试了解如何在项目中使用它,所以我一直在研究一个简单的案例类示例,其中包含不可变的哈希图。在这个例子中,scala-pickling不会产生一个解腌器,我不知道为什么。以下是演示该问题的 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]
谁能指出我做错了什么? 或者斯卡拉酸洗有什么问题吗?
编辑:实际上,当我生成一个具有一个属性的类时,似乎也会发生同样的情况,该属性只是一个符号(我将发布另一个 REPL 会话)。在斯卡拉酸洗中,有没有一种特殊的方法来处理符号?
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]
我知道
这是一个旧帖子,但请尝试
import scala.pickling._, Defaults._, json._