本文介绍了Json Coast到Coast Play框架:序列化Joda DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

大家好,我是新手,如果有人知道下面提到的更好的方法,请告诉我.

Hi all I am new to play framework, if someone knows of a better approach then mentioned below please let me know.

所以我有一个模型,并为其读取/写入/格式化

So I have a model and Reads/Writes/Format for it

case class Schedule (startDate: DateTime, endDate: DateTime)


object ScheduleSerializers {


  val userDateFormatter = "dd/MM/yyyy HH:mm:ss"
  val nonImplicitUserFormatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss")
  implicit val jodaDateTimeReads = Reads.jodaDateReads(userDateFormatter)
  implicit val jodaDateTimeWrites = Writes.jodaDateWrites(userDateFormatter)

  implicit val readSchedule: Reads[Schedule] = (
    (__ \ "startDate").read[String].map[DateTime](dt => DateTime.parse(dt, nonImplicitUserFormatter)) and
      (__ \ "endDate").read[String].map[DateTime](dt => DateTime.parse(dt, nonImplicitUserFormatter))
    )(Schedule)

  implicit val writeSchedule: Writes[Schedule] = (
    (__ \ "startDate").write[String].contramap[DateTime](dt => nonImplicitUserFormatter.print(dt)) and
      (__ \ "endDate").write[String].contramap[DateTime](dt => nonImplicitUserFormatter.print(dt))
    )(unlift(Schedule.unapply))

  implicit val formatSchdule = Format(readSchedule, writeSchedule)

}

现在我打开播放控制台并执行

Now I open the play console and do this

val sch = Json.parse(""" {
     |
     |   "schedule" : { "starDate" : "04/02/2011 20:27:05" , "endDate" : "04/02/2011 20:27:05" }
     | }
     | """)
sch: play.api.libs.json.JsValue = {"schedule":{"starDate":"04/02/2011 20:27:05","endDate":"04/02/2011 20:27:05"}}

sch.validate[Schedule]
res0: play.api.libs.json.JsResult[models.experiment.Schedule] = JsError(List((/endDate,List(ValidationError(error.path.missing,WrappedArray()))), (/startDate,List(ValidationError(error.path.missing,WrappedArray())))))

我收到一个错误消息,但是如果我尝试解析一个日期,例如:

I am getting an Error, but if I try to parse a single date for ex:

scala> val singleDate = Json.parse(""" "04/02/2011 20:27:05" """)
singleDate: play.api.libs.json.JsValue = "04/02/2011 20:27:05"

singleDate.validate[DateTime]
res1: play.api.libs.json.JsResult[org.joda.time.DateTime] = JsSuccess(2011-02-04T20:27:05.000-08:00,)

我对为什么'singleDate'有效但对'Schedule'模型的验证失败感到困惑.在此先感谢您的帮助.

I am confused as to why 'singleDate' works but the validation on the 'Schedule' model fails.Thanks in advance, any help will be appreciated.

推荐答案

错误非常明显:路径丢失".

The error is rather clear: "path missing".

代替:

 (__ \ "startDate") ...
 (__ \ "endDate") ...

您必须给出真实的路径:

You have to give the real path:

 (__ \ "schedule" \ "startDate") ...
 (__ \ "schedule" \ "endDate") ...

顺便说一句,当您将jodaDateTimeReads定义为implicit时,您无需手工粘贴.而且,您进行读写操作时,只需使用Format.

By the way, as you define jodaDateTimeReads as implicit, you don't need to do the pasing by hand. And as you reads and writes are doing the same, just go with Format.

那应该足够了:

implicit val formatSchedule: Format[Schedule] = (
  (__ \ "startDate").read[DateTime] and
  (__ \ "endDate").read[DateTime]
)(Schedule.apply, unlift(Schedule.unapply)))

这篇关于Json Coast到Coast Play框架:序列化Joda DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:26