Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON序列化程序中数据时间的自定义写入_Json_Scala_Playframework_Playframework 2.3 - Fatal编程技术网

JSON序列化程序中数据时间的自定义写入

JSON序列化程序中数据时间的自定义写入,json,scala,playframework,playframework-2.3,Json,Scala,Playframework,Playframework 2.3,如何在DateTime的JSON序列化程序中获得自定义行为? 我的目标是只把这一年连载起来 这是我的模型: case class Model(id: Option[Int], name: String, userID: String, date: DateTime, material: String, location: String, text: String, pathObject: Option[String], pathTexure: Option[String], pathThumbn

如何在DateTime的JSON序列化程序中获得自定义行为? 我的目标是只把这一年连载起来

这是我的模型:

case class Model(id: Option[Int], name: String, userID: String, date: DateTime, material: String, location: String, text: String, pathObject: Option[String], pathTexure: Option[String], pathThumbnail: Option[String])

object Model {
  implicit val tswrites: Writes[DateTime] = Writes { (dt: DateTime) => JsString(dt.year.get.toString) }

  implicit val modelWrites: Writes[Model] = (
    (JsPath \ "id").write[Option[Int]] and
    (JsPath \ "name").write[String] and
    (JsPath \ "userId").write[String] and
    (JsPath \ "date").write[DateTime] and
    (JsPath \ "material").write[String] and
    (JsPath \ "location").write[String] and
    (JsPath \ "text").write[String] and
    (JsPath \ "f1").write[Option[String]] and
    (JsPath \ "f2").write[Option[String]] and
    (JsPath \ "f3").write[Option[String]])(unlift(models.Model.unapply))
}
日期字段序列化为631148400000

所需的日期字段序列化为1990,我已经解决了它

以下是解决方案:

object Model {

  implicit val tswrites: Writes[DateTime] = Writes { (dt: DateTime) => JsString(dt.year.get.toString) }

  implicit val modelWrites: Writes[Model] = (
    (JsPath \ "id").write[Option[Int]] and
    (JsPath \ "name").write[String] and
    (JsPath \ "userId").write[String] and
    Writes.at[DateTime]((JsPath \ "date"))(tswrites) and
    (JsPath \ "material").write[String] and
    (JsPath \ "location").write[String] and
    (JsPath \ "text").write[String] and
    (JsPath \ "f1").write[Option[String]] and
    (JsPath \ "f2").write[Option[String]] and
    (JsPath \ "f3").write[Option[String]])(unlift(models.Model.unapply))

}