我正在使用 scala 酸洗库(0.9.0-snapshot)进行 Json 序列化。
我想编写一个自定义pickler和unpickler,用于将sql.Timestamp字段序列化和反序列化为普通字段,我可以通过覆盖Spickler中的pickle方法来序列化时间戳字段,我还想反序列化json字符串中的时间戳值(字符串值)到时间戳字段。可能吗?
我是说,
case class T1(id: Long, name: String, time: Timestamp)
以 json 为
{
"tpe" : "T1",
"id" : "42",
"name": "Name",
"time": "2014-10-17 17:19:29.97"
}
是可能的,但无法解开回到案例类
T1(42, "Name", new Timestamp(new Date().getTime)).pickle.value.unpickle[T1]
// 显示错误 scala.MatchError: 2014-10-17 17:19:29.97 (of class java.lang.String)
我当前的实现看起来像这样。
class TimestampPickler(implicit val format: PickleFormat) extends SPickler[Timestamp] with Unpickler[Timestamp] {
private val stringUnpickler = implicitly[Unpickler[String]]
override def pickle(picklee: Timestamp, builder: PBuilder): Unit = {
builder.hintTag(FastTypeTag.String).beginEntry(picklee.toString).endEntry()
}
override def unpickle(tag: => FastTypeTag[_], reader: PReader): Timestamp = {
// ToDo
}
}
implicit def genTimestampPickler(implicit format: PickleFormat) = new TimestampPickler