Scala Pickling: Writing a custom pickler / unpickler for nested structures

最后都变了- 提交于 2019-12-05 10:16:50

Here is a working example:

case class MyClass[A](myString: String, a: A)

Note that the type parameter of MyClass does not need context bounds. Only the custom pickler class needs the corresponding implicits:

class MyClassPickler[A](implicit val format: PickleFormat, aTypeTag: FastTypeTag[A],
                                     aPickler: SPickler[A], aUnpickler: Unpickler[A])
  extends SPickler[MyClass[A]] with Unpickler[MyClass[A]] {

  private val stringUnpickler = implicitly[Unpickler[String]]

  override def pickle(picklee: MyClass[A], builder: PBuilder) = {
    builder.beginEntry(picklee)

    builder.putField("myString",
      b => b.hintTag(FastTypeTag.ScalaString).beginEntry(picklee.myString).endEntry()
    )

    builder.putField("a",
      b => {      
        b.hintTag(aTypeTag)
        aPickler.pickle(picklee.a, b)
      }
    )

    builder.endEntry()
  }

  override def unpickle(tag: => FastTypeTag[_], reader: PReader): MyClass[A] = {
    reader.hintTag(FastTypeTag.ScalaString)
    val tag = reader.beginEntry()
    val myStringUnpickled = stringUnpickler.unpickle(tag, reader).asInstanceOf[String]
    reader.endEntry()

    reader.hintTag(aTypeTag)
    val aTag = reader.beginEntry()
    val aUnpickled = aUnpickler.unpickle(aTag, reader).asInstanceOf[A]
    reader.endEntry()

    MyClass(myStringUnpickled, aUnpickled)
  }

}

In addition to the custom pickler class, we also need an implicit def which returns a pickler instance specialized for concrete type arguments:

implicit def myClassPickler[A: SPickler: Unpickler: FastTypeTag](implicit pf: PickleFormat) =
  new MyClassPickler
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!