我正在使用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)
}当我尝试放入像这样的路径时:
get {
complete {
List(new ElementResponse(...), new ElementResponse(...))
}
}我收到一个错误,说:
could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[List[pl.ftang.scala.polka.rest.ElementResponse]]也许你知道问题出在哪里?
我使用的是Scala 2.10.1和Scala 1.1-M7和Scala json 1.2.5
发布于 2013-07-19 11:06:55
您还需要导入您在路由范围上定义的格式:
import JollyJsonProtocol._
get {
complete {
List(new ElementResponse(...), new ElementResponse(...))
}
}发布于 2015-06-30 22:23:24
这是一个老问题,但我想尽我最大的努力。今天也在研究类似的问题。
Marcin,看起来你的问题实际上并没有得到解决(据我所知)--为什么你接受了一个答案?
你有没有尝试在某些地方添加import spray.json.DefaultJsonProtocol._?它们负责让Seqs、Maps、Options和Tuples正常工作。我认为这可能是您的问题的原因,因为这是List没有转换的原因。
发布于 2013-07-19 15:40:11
要做到这一点,最简单的方法是从列表中生成一个字符串,否则您将不得不处理ChunckedMessages:
implicit def ListMarshaller[T](implicit m: Marshaller[T]) =
Marshaller[List[T]]{ (value, ctx) =>
value match {
case Nil => ctx.marshalTo(EmptyEntity)
case v => v.map(m(_, ctx)).mkString(",")
}
}第二种方法是将你的列表转换成Stream[ElementResponse],然后让spray为你chunck它。
get {
complete {
List(new ElementResponse(...), new ElementResponse(...)).toStream
}
}https://stackoverflow.com/questions/17734457
复制相似问题