我最近遇到了scala-pickling,我正试图看看如何在项目中使用它,所以我一直在研究一个带有不可变哈希映射的case类的简单示例。在这个例子中,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]
答案 0 :(得分:0)
我知道这是一篇旧帖子,但请尝试
import scala.pickling._, Defaults._, json._