我正在使用scala pickling library(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(类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