scala中的scala-pickling 2.11中的POJO - 它真的很简单吗?

时间:2015-08-22 13:56:01

标签: scala scala-pickling

我试图使用scala-pickling,因为在网站github它似乎很容易和干净。但是,我在这个简单的 REPL 中未能使用它:

scala> import scala.pickling._
import scala.pickling._

scala> import scala.pickling.Defaults._
import scala.pickling.Defaults._

scala> import binary._
import binary._

scala> class Xpto { var a = 0D; var b = 0 }
defined class Xpto

scala> val v = new Xpto { a = 1.23; b = 5 }
v: Xpto = $anon$1@636d2b03

scala> v.pickle
<console>:19: error: type mismatch;
 found   : v.type (with underlying type Xpto)
 required: ?{def pickle: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method PickleOps in package pickling of type [T](picklee: T)pickling.PickleOps[T]
 and method pickleOps in trait Ops of type [T](picklee: T)scala.pickling.PickleOps[T]
 are possible conversion functions from v.type to ?{def pickle: ?}
          v.pickle
            ^
<console>:19: error: value pickle is not a member of Xpto
                  v.pickle
                    ^

有什么问题?

我确实使用相同类型的问题访问StackOverflow上的其他问题,例如:

Scala pickling: Simple custom pickler for my own class?

Obs。:我在 build.sbt 中使用此引用:

"org.scala-lang.modules" %% "scala-pickling" % "0.10.1"

2 个答案:

答案 0 :(得分:0)

您确定这些是在REPL中使用的唯一导入吗?上面的错误是,正如它所说:

  

请注意,隐式转换不适用,因为它们不明确:    两种方法PickleOps在包装酸洗类型[T](picklee:T)pickling.PickleOps [T]    和方法pickleOps in trait Ops类型[T](picklee:T)scala.pickling.PickleOps [T]    可能的转换函数从v.type到?{def pickle:?}

因此,scala.pickling.PickleOps[T]()scala.pickling.Ops.pickleOps至少有两次隐式转换。这很奇怪,因为PickleOps不是隐式类。

对我来说,它在新的REPL中运行(Scala version 2.11.7 Java 1.7.0_79)

scala> import scala.pickling._
scala> import scala.pickling.Defaults._
scala> import binary._
scala> class Xpto { var a = 0D; var b = 0 }
defined class Xpto
scala> val v = new Xpto { a = 1.23; b = 5 }
v: Xpto = cmd5$$anonfun$1$$anon$1@244da0ed
scala> v.pickle
res6: pickleFormat.PickleType = BinaryPickle([0,0,0,23,99,109,100,53,36,36,97,110,111,110,102,117,110,36,49,36,36,97,110,111,110,36,49,63,-13,-82,20,122,-31,71,-82,0,0,0,5])

答案 1 :(得分:0)

大!它运行!

我开始使用新的scala控制台。

我在build.sbt中使用了对scala.pickling的引用:

"org.scala-lang" %% "scala-pickling" % "0.10.1"

现在我正在使用

"org.scala-lang.modules" %% "scala-pickling" % "0.10.1"

我也在使用Scala 2.11.6

现在它完美无缺,真的很简单。

scala> import scala.pickling._
import scala.pickling._

scala> import scala.pickling.binary._
import scala.pickling.binary._

scala> import scala.pickling.Defaults._
import scala.pickling.Defaults._

scala> class Xpto { var a = 0D; var b = 0; }
defined class Xpto

scala> val v = new Xpto { a = 1.23; b = 4; }
v: Xpto = $anon$1@1e7bd4df

scala> v.pickle
res0: pickling.binary.pickleFormat.PickleType = BinaryPickle([0,0,0,52,46,108,105,110,101,55,46,46,114,101,97,100,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,97,110,111,110,46,49,63,-13,-82,20,122,-31,71,-82,0,0,0,4])

如果我的其他图书馆引用产生了那种含糊不清的拒绝,我现在不会。我在 build.sbt 中的引用是:

libraryDependencies ++= Seq(
  "log4j" % "log4j" % "1.2.17",
  "javax.transaction" % "jta" % "1.1",
  "com.typesafe.akka" %% "akka-actor" % "2.3.10",
  "com.typesafe.akka" %% "akka-testkit" % "2.3.10",
  "org.scalatest" %% "scalatest" % "3.0.0-SNAP4" % "test",
  "org.apache.commons" % "commons-io" % "1.3.2",
  "com.typesafe.akka" %% "akka-slf4j" % "2.3.11",
  "ch.qos.logback" % "logback-classic" % "1.0.9",
  "org.scala-lang.modules" %% "scala-pickling" % "0.10.1"
)

感谢马库斯。