我有一个简单的案例类位置(这几乎是ScalaJsonCombinators教程)
<TypeOfId> id = hm.get(c.getPhone());
if(id != null) {
registered_phone_contacts.add(new Contact(id, c.getName());
... ^^
}
我想在构造上设置当前时间,以便我的位置看起来像:
// controller
implicit val locationWrites: Writes[Location] =
(
(JsPath \ "latitude").write[Double] and
(JsPath \ "longitude").write[Double]
) (unlift(Location.unapply))
implicit val locationReads: Reads[Location] =
(
(JsPath \ "latitude").read[Double] and
(JsPath \ "longitude").read[Double]
) (Location.apply _)
// model
case class Location(latitude: Double, longitude: Double)
object Location {
implicit val formatter = Json.format[Location]
}
不幸的是,我无法弄清楚如何使用Play Json传递LocalDateTime.now()而不发送额外的字段或实际上如何传递LocalDateTime。
请求正文
case class Location(latitude: Double, longitude: Double, date: LocalDateTime)
答案 0 :(得分:2)
不是将Location.unapply
/ Location.apply
传递给FunctionalBuilder
,而是可以传递自己的功能:
case class Location(latitude: Double, longitude: Double, date: LocalDateTime)
implicit val locationWrites: Writes[Location] =
(
(JsPath \ "latitude").write[Double] and
(JsPath \ "longitude").write[Double]
) ((l: Location) => (l.latitude, l.longitude))
implicit val locationReads: Reads[Location] =
(
(JsPath \ "latitude").read[Double] and
(JsPath \ "longitude").read[Double]
) ((lat: Double, lon: Double) => Location(lat, lon, LocalDateTime.now()))