scala / pickling

Fast, customizable, boilerplate-free pickling support for Scala
lampwww.epfl.ch/~hmiller/pickling
BSD 3-Clause "New" or "Revised" License
831 stars 79 forks source link

Enumeration Pickling Failure When Pickling As Superclass Even With Custom Pickler #386

Open lambdaknight opened 8 years ago

lambdaknight commented 8 years ago

import scala.pickling._
import scala.pickling.Defaults._
import scala.pickling.json._

object Foo extends Enumeration {
  type Foo = Value
  val Bar = Value(0)
  val Baz = Value(1)
}

implicit object fooPickler extends Pickler[Foo.Foo] {
  override def pickle(picklee: Foo.Foo, builder: PBuilder): Unit = {
    builder.hintTag(tag)
    builder.beginEntry(picklee)
    builder.putField("value", b => picklee.id.pickleInto(builder))
    builder.endEntry
  }

  override val tag: FastTypeTag[Foo.Foo] = FastTypeTag[Foo.Foo]
}

trait Qux

case class Qax(a: Foo.Foo) extends Qux

val a: Qux = Qax(Foo.Bar)

a.pickle```

This fails with the following exception:
```java.lang.RuntimeException: error: cannot find class or module with type name 'scala.Enumeration.Val'
full type string: 'scala.Enumeration.Val'```

`Qax(Foo.Bar).pickle` does work just fine and returns the expected result.
jsuereth commented 8 years ago

Your fooPickler doesn't look like it's actually getting used for foo. SPECIFICALLY, you're using the runtime pickler when you pickle Qux, and the hand-generated pickler for Foo has not been registered with dynamic pickler registry.

If you're doing a lot of dynamic pickling, you'll want to register with the dynamic runtime, otherwise you should statically generate the pickler.