使用MkFieldLens类型的字段序列化对象

时间:2016-07-13 22:21:47

标签: scala serialization typeclass shapeless lens

如何通过修改或分支Shapeless并修改MkFieldLens的来源来序列化以下类

  case class IdHolder[T](t: T)(implicit
                           l: MkFieldLens.Aux[T, Witness.`'id`.T, Option[Long]]){
    private def idLens = lens[T] >> 'id
  }

运行此测试代码:

case class Cow(sound: String, id: Option[Long])

object Testy extends App {
  val s = IdHolder(Cow("moo", Some(123L)))
  val tmp = File.createTempFile("save_test", "bin")
  val oos = new ObjectOutputStream(new FileOutputStream(tmp))
  oos.writeObject(s)
}

产生此错误:

Exception in thread "main" java.io.NotSerializableException: shapeless.MkFieldLens$$anon$33

0 个答案:

没有答案