spray-json和list marshalling

Mar*_*lke 10 scala scala-2.10 spray spray-json

我正在使用spray-json将自定义对象列表编组到JSON中.我有以下case类和它的JsonProtocol.

case class ElementResponse(name: String, symbol: String, code: String, pkwiu: String, remarks: String, priceNetto: BigDecimal, priceBrutto: BigDecimal, vat: Int, minInStock:Int,                        maxInStock: Int)

object JollyJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport  {
 implicit val elementFormat = jsonFormat10(ElementResponse)
}
Run Code Online (Sandbox Code Playgroud)

当我试图像这样的路线投入时:

get {
      complete {
        List(new ElementResponse(...), new ElementResponse(...))
      }
    }
Run Code Online (Sandbox Code Playgroud)

我得到一个错误说:

 could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[List[pl.ftang.scala.polka.rest.ElementResponse]]
Run Code Online (Sandbox Code Playgroud)

也许你知道这是什么问题?

我正在使用Scala 2.10.1喷雾1.1-M7和喷雾-json 1.2.5

aka*_*ppi 5

这是一个老问题,但我想给我的2c.今天看着类似的问题.

Marcin,看来你的问题实际上并未解决(据我所知) - 你为什么接受一个答案?

你有没有尝试添加import spray.json.DefaultJsonProtocol._地方?那些负责使诸如Seqs,Maps,Options和Tuples之类的东西工作.我认为这可能是你的问题的原因,因为它List是没有得到转换.


Joh*_*son 2

您还需要导入在路由范围中定义的格式:

import JollyJsonProtocol._
get {
      complete {
        List(new ElementResponse(...), new ElementResponse(...))
      }
    }
Run Code Online (Sandbox Code Playgroud)

  • 我有那个进口。编组 ElementResponse 类型的对象效果很好。不起作用的是编组这些对象的列表。 (4认同)