我正在处理以下代码行。
val list = Car.getNames()
Ok(Json.toJson(list))
我收到以下错误....
[错误] my_app/app/models/Car.scala:51: 找不到 java.util.Date 类型的 Json 反序列化器。尝试为此类型实现隐式读取或格式。
Car
将java.util.date
对象作为参数之一,我实现了读取和写入以支持该java.util.date
对象,因为import play.api.libs.json.*
不支持它。
你能指出我的错误吗?
implicit object CarFormat extends Format[Car] {
def reads(json: JsValue): Car = Car(
(json \ "id").as[Long],
(json \ "height").as[Double],
(json \ "weight").as[Double],
(json \ "date").asOpt[java.util.Date]
)
def writes(car: Car) =
JsObject(
Seq(
"id" -> JsString(car.id.toString),
"height" -> JsString(car.height.toString),
"weight" -> JsString(car.weight.toString),
"date" -> JsString(car.date.toString)
)
)
}