1
我有以下類(簡化的比特存在),這將延長JSON格式,對於表示數據庫級與所述ID字段的特定對象:斯卡拉/播放2.4 JSON格式問題
import play.api.libs.json._
import play.api.libs.functional.syntax._
class EntityFormat[T <: Entity](entityFormatter: Format[T]) extends Format[T] {
val extendedFormat: Format[T] = (
__.format[T](entityFormatter) ~
(__ \ "id").format[Option[Long]]
)(tupleToEntity, entityToTuple)
private def tupleToEntity(e: T, id: Option[Long]) = {
e.id = id
e
}
private def entityToTuple(e: T) = (e, e.id)
def writes(o: T): JsValue = extendedFormat.writes(o)
def reads(json: JsValue): JsResult[T] = extendedFormat.reads(json)
}
abstract class Entity {
var id: Option[Long] = None
}
隨着播放2.3,然後我可以寫
implicit val userFormat: Format[User] = new EntityFormat(Json.format[User])
然後,這將與生成的JSON中的ID字段一起使用。然而,隨着播放2.4我得到以下編譯時間問題:
No Json formatter found for type Option[Long]. Try to implement an implicit Format for this type. (__ \ "id").format[Option[Long]]
missing arguments for method tupleToEntity in class DomainEntityFormat; follow this method with `_' if you want to treat it as a partially applied function)(tupleToEntity, entityToTuple)
^
missing arguments for method tupleToEntity in class DomainEntityFormat; follow this method with `_' if you want to treat it as a partially applied function)(tupleToEntity, entityToTuple)
^
,你應該怎麼做與遊戲2.4延伸到做出那種JSON格式的工作?
歡呼聲,似乎這樣的伎倆。 – mpartan