Search code examples
scalaserializationupickle

How to serialize generic case classes with µPickle?


The µPickle docs say generic case classes can be serialized:

Out of the box, uPickle supports writing and reading the following types:

  • Stand-alone case classes and case objects, and their generic equivalents,

However no example is given and I was unable to find a correct way so far. My attempt is:

import upickle.default._

object Container {
  implicit def rw[T]: ReadWriter[Container[T]] = macroRW
}
case class Container[T](value: T)

object Main extends App {
  val c = new Container(0)

  val cString = write(c)
  println("c " + cString)
}

This fails with error:

Error:(7, 50) could not find implicit value for parameter e:

upickle.default.Reader[T]

implicit def rw[T]: ReadWriter[Container[T]] = macroRW

How should generic case classes be serialized using µPickle?


Solution

  • Don't forget to use context bound

    implicit def rw[T: ReadWriter]: ReadWriter[Container[T]] = macroRW