I am working on the following lines of code.
val list = Car.getNames() Ok(Json.toJson(list))
I got the following errors ....
[error] my_app / app / models / Car.scala: 51: There is no Json desancializer for type java.util.Date. Try to implement implicit reads or a format for this type.
Car has a java.util.date object as one of the parameters, and I implemented Reads and Writes to support the java.util.date object since import play.api.libs.json.* Does not support it.
Could you indicate my mistakes?
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) ) ) }
scala playframework
Masashi
source share