我有一個Scala-Play網絡服務應用程序,它執行一些計算,生成Saddle frames並需要將這些Saddle幀轉儲到Json中。所以我定義了一個frameWrites
這樣的:Scala-Play Json:如何定義任何鞍形框架的寫入?
object JsonUtils {
implicit def frameWrites[RX, CX, E] = new Writes[Frame[RX, CX, E]] {
override def writes(frame: Frame[RX, CX, E]): JsValue = {
val json: JsArray = Json.arr(
(0 until frame.numRows).map { i =>
Json.obj(
frame.rowIx.at(i).toString ->
(0 until frame.numCols).map { j =>
Json.obj(
frame.colIx.at(j).toString -> frame.at(i, j).toString
)
}
)
})
json
}
}
}
,然後嘗試使用這樣的:
import utils.JsonUtils._
def computingAction = Action {
val pnlStatistics: Frame[String, String, Double] = ???
Ok(pnlStatistics)
}
但隨後總是錯誤Cannot write an instance of org.saddle.Frame[String,String,Double] to HTTP response. Try to define a Writeable[org.saddle.Frame[String,String,Double]]
要真正明確我自己也嘗試定義爲JsonUtils
的一部分:
implicit def frameSSDWrites = frameWrites[String, String, Double]
但是這其中也沒有得到回升...
UPDATE明確調用作家作品:
Ok(frameWrites.writes(results("PnlStatistics")))
但是你需要實現Writeable not a Writes,對嗎? – Mysterion