spray-json and list marshalling
Asked Answered
R

3

10

I'm using spray-json to marshal lists of custom objects into JSON. I have the following case class and its 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)
}

When I try to put in in a route like this one:

get {
      complete {
        List(new ElementResponse(...), new ElementResponse(...))
      }
    }

I get an error saying that:

 could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[List[pl.ftang.scala.polka.rest.ElementResponse]]

Perhaps you know what is the problem?

I'm using Scala 2.10.1 with spray 1.1-M7 and spray-json 1.2.5

Rackrent answered 18/7, 2013 at 21:42 Comment(1)
See this example, which uses a List.Belloc
T
2

You also need to import the format you defined on the route scope:

import JollyJsonProtocol._
get {
      complete {
        List(new ElementResponse(...), new ElementResponse(...))
      }
    }
Terrigenous answered 19/7, 2013 at 3:6 Comment(1)
I have that import. Marshalling objects of type ElementResponse works fine. What does not work is marshalling Lists of those objects.Rackrent
L
5

This is an old issue, but I feel like giving my 2c. Was looking at similar issues today.

Marcin, it seems your issue was not actually solved (as far as I can read) - why did you accept one answer?

Did you try adding import spray.json.DefaultJsonProtocol._ in places? Those are in charge of making things such as Seqs, Maps, Options and Tuples to work. I would assume this might be the cause of your problem, since it's the List that is not getting converted.

Laminated answered 30/6, 2015 at 14:23 Comment(0)
C
3

The easiest way to do this, is to make a String from your list or you'll have to deal with 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(",")
      }
    }

The seconds way is to convert your list into the Stream[ElementResponse] and let spray chunck it for you.

get {
  complete {
    List(new ElementResponse(...), new ElementResponse(...)).toStream
  }
}
Comment answered 19/7, 2013 at 7:40 Comment(2)
That is a nice idea, but how should I use marshaller in my json protocol? (JollyJsonProtocol in my case) - adding this implicit method to the protocol class does not help.Rackrent
I would recommend you to rename your JollyJsonProtocol and make it as a companion object for [import tax][2]. List marshaller should work by importing it into the scope. As for Stream just call toStream on your listComment
T
2

You also need to import the format you defined on the route scope:

import JollyJsonProtocol._
get {
      complete {
        List(new ElementResponse(...), new ElementResponse(...))
      }
    }
Terrigenous answered 19/7, 2013 at 3:6 Comment(1)
I have that import. Marshalling objects of type ElementResponse works fine. What does not work is marshalling Lists of those objects.Rackrent

© 2022 - 2024 — McMap. All rights reserved.