我正在尝试通过酸洗将 joda DateTime 实例与 json 进行酸洗/解酸。使用酸洗 0.8.0,如果我不提供自定义酸洗器,我会得到
JSONPickle({
"tpe": "org.joda.time.DateTime"
})
当我做:
class DateTimePickler(implicit val format: PickleFormat) extends
SPickler[DateTime] with Unpickler[DateTime] {
private val stringUnpickler = implicitly[Unpickler[String]]
def pickle(picklee: DateTime, builder: PBuilder): Unit = {
builder.beginEntry(picklee)
builder.putField("date", b =>
b.hintTag(stringTag).beginEntry(picklee.toString).endEntry()
)
builder.endEntry()
}
override def unpickle(tag: => FastTypeTag[_], reader: PReader): DateTime = {
reader.hintTag(stringTag)
val tag = reader.beginEntry()
logger.debug(s"tag is ${tag.toString}")
val value = stringUnpickler.unpickle(tag, reader).asInstanceOf[String] //can't debug NoSuchElementException: : key not found: value
logger.debug(s"value is $value")
reader.endEntry()
val timeZone = DateTimeZone.getDefault
DateTime.parse(value).withZone(timeZone) //otherwise the parsed DateTime won't equal the original
}
}
implicit def genDateTimePickler(implicit format: PickleFormat) = new DateTimePickler
我明白了
JSONPickle({
"tpe": "org.joda.time.DateTime",
"date": {
"tpe": "java.lang.String",
"value": "2014-09-16T17:59:25.865+03:00"
}
})
并且 unpickling 失败并出现 NoSuchElementException: : key not found: value。使用酸洗 0.9.0-SNAPSHOT 我的 specs2 测试甚至不会终止。