简体   繁体   English

Scala-Play Json:如何为任何鞍架定义写入?

[英]Scala-Play Json: How to define a Writes for any Saddle Frame?

I have a Scala-Play web services application that does a bit of computations that generate Saddle frames and need to dump those Saddle frames into Json. 我有一个Scala-Play Web服务应用程序,该应用程序进行了一些计算,生成了Saddle框架,并且需要将这些Saddle框架转储到Json中。 So I define a frameWrites like this: 所以我定义了一个frameWrites像这样:

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
      }
  }
}

and then attempt to use it like this: 然后尝试像这样使用它:

import utils.JsonUtils._

def computingAction = Action {
   val pnlStatistics: Frame[String, String, Double] = ???
   Ok(pnlStatistics) 
}

but then always get the error 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]] 但总是收到错误消息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]] 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]]

To be really explicit I have also tried defining as part of JsonUtils : 为了明确JsonUtils我也尝试过将其定义为JsonUtils一部分:

implicit def frameSSDWrites = frameWrites[String, String, Double]

but this one also doesn't get picked up ... 但是这个也不会被捡起来...

UPDATE invoking the writer explicitly works: 调用作者的UPDATE显式起作用:

Ok(frameWrites.writes(results("PnlStatistics")))

A Play result input must be some content that can be put in a HTTP response. 播放结果输入必须是可以放入HTTP响应中的某些内容。

Your action does not know that you want to write it specifically as JSON, so you need to return Ok(Json.toJson(pnlStatistics)) . 您的操作不知道您要专门将其编写为JSON,因此需要返回Ok(Json.toJson(pnlStatistics))

Let us see how this work: 让我们看看它是如何工作的:

  • The type of pnlStatistics is a Frame , so you can serialize it to JSON, using your implicit Writes[Frame] , so Json.toJson accepts it as parameter. pnlStatistics的类型为Frame ,因此您可以使用隐式Writes[Frame]其序列化为JSON,因此Json.toJson接受它作为参数。 It then returns a JsValue . 然后,它返回一个JsValue

  • Since there is also an implicit Writeable[JsValue] in scope, Ok then accepts this value as content for your response (and will also add the Content-Type header). 由于范围内还存在一个隐式Writeable[JsValue] ,因此Ok将接受此值作为您的响应的内容(并将添加Content-Type标头)。

If you forget the explicit JSON conversion, Play has no way to determine how you want the frame handled in the response. 如果您忘记了显式的JSON转换,Play将无法确定您希望如何在响应中处理帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM