scala-plicking used in common classes

1

I'm trying to use scala-pickling because the site in github makes it look like a very simple tool to use . The fact is that I am not able to make the simple statement (REPL) work:

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
                    ^

What can I be doing wrong?

Note: I am using the following reference in build.sbt :

"org.scala-lang.modules" %% "scala-pickling" % "0.10.1"
    
asked by anonymous 22.08.2015 / 15:27

1 answer

0

There are many implicit functions with the same signature being fetched


// importa a case class PickleOps que define a função pickle
import scala.pickling._
// importa as funções do objeto Defaults
// que por herança traz tb o método pickleOps
import scala.pickling.Defaults._

Try decreasing imports and using only what you need

I still prefer to use case class instead of a "normal" class, this is a good practice for model entities, you will notice that the serialization result will be "cleaner" because of the case class brings

Another tip, to serialize json I recommend json lib from playframework that tb is very simple and efficient

    
25.08.2015 / 17:33