Custom unpickler for Enumeration

320 Views Asked by At

I have an enumeration that I'm trying to pickle and unpickle with pickling 0.8.0 and scala 2.11:

object CommandType extends Enumeration {
  val Push, Pop = Value
}

Pickling cannot do it automagically at the moment. The custom pickler-unpickler looks like this:

class CommandTypePickler(implicit val format: PickleFormat)
  extends SPickler[CommandType.Value] with Unpickler[CommandType.Value] with LazyLogging {

  def pickle(picklee: CommandType.Value, builder: PBuilder): Unit = {
    builder.beginEntry(picklee)
    builder.putField("commandType", b =>
      b.hintTag(stringTag).beginEntry(picklee.toString).endEntry()
    )
    builder.endEntry()
  }

  override def unpickle(tag: => FastTypeTag[_], reader: PReader): CommandType.Value = {
    val ctReader = reader.readField("commandType")
    val tag = ctReader.beginEntry()
    logger.debug(s"tag is ${tag.toString}")
    val value = stringUnpickler.unpickle(tag, ctReader).asInstanceOf[String]
    ctReader.endEntry()

    CommandType.withName(value)
  }
}

Serialized enumeration:

{
  "tpe": "scala.Enumeration.Value",
  "commandType": {
    "tpe": "java.lang.String",
    "value": "Push"
  }
}

When unpickling, this throws the following: ScalaReflectionException: : class scala.Enumeration.Value in JavaMirror with sun.misc.Launcher$AppClassLoader@5c3eeab3 of type class sun.misc.Launcher$AppClassLoader with classpath ... not found. What am I doing wrong?

0

There are 0 best solutions below