枚举的自定义unpickler

时间:2014-09-24 06:07:24

标签: scala-pickling

我有一个枚举,我试图用酸洗0.8.0和scala 2.11来腌渍和捣蛋:

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

Pickling cannot目前自动完成。自定义pickler-unpickler看起来像这样:

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)
  }
}

序列化枚举:

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

取消排版时,会抛出以下内容:ScalaReflectionException: : class scala.Enumeration.Value in JavaMirror with sun.misc.Launcher$AppClassLoader@5c3eeab3 of type class sun.misc.Launcher$AppClassLoader with classpath ... not found。我做错了什么?

0 个答案:

没有答案