我试图通过酸洗将joda DateTime实例发送到json或从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测试甚至终止了。
答案 0 :(得分:0)
目前,酸洗缺乏文档和无法提供的错误消息。无论如何,我的unpickle方法被编写为类似于this answer,它假设二进制序列化。通过反复试验找到的正确的unpickle方法如下所示:
override def unpickle(tag: => FastTypeTag[_], reader: PReader): DateTime = {
val dateReader = reader.readField("date")
val tag = dateReader.beginEntry()
val value = stringUnpickler.unpickle(tag, dateReader).asInstanceOf[String]
dateReader.endEntry()
val timeZone = DateTimeZone.getDefault
DateTime.parse(value).withZone(timeZone) //otherwise the parsed DateTime won't equal the original
}