scala-plicking used in common classes

Asked

Viewed 44 times

1

I’m trying to use scala-pickling because the site on github makes it look like it’s a very simple tool to use. The fact is that I’m not able to make the simple following work (in 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
                    ^

What could I be doing wrong?

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

"org.scala-lang.modules" %% "scala-pickling" % "0.10.1"
  • I was able to compile this code with this same version of pickling. The only difference is that I used a Worksheet instead of REPL....

1 answer

0

there are many implicit functions with the same signature being brought


// 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 to decrease the imports and use only what you need

I still prefer to use a case class rather than a "normal" class, this is good practice for model type entities, you will notice that the result of serialization will be more "clean", because of facilities that a case class tail

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.