Question

I've been struggling with this for a few hours. I'm hoping someone could assist me in understanding where the problem is.

Spray-JSON has a test case here

https://github.com/spray/spray-json/blob/master/src/test/scala/spray/json/AdditionalFormatsSpec.scala

case class Container[A](inner: Option[A])

object ReaderProtocol extends DefaultJsonProtocol {
  implicit def containerReader[T :JsonFormat] = 
    new JsonReader[Container[T]] {
      def read(value: JsValue) = value match {
        case JsObject(fields) if fields.contains("content") => 
          Container(Some(jsonReader[T].read(fields("content"))))
        case _ => deserializationError("Unexpected format: " + value.toString)
      }
    }
  }
}

that shows how you can serialize a container type. I've attempted to adapt this to my situaiton.

case class ListResponseObject[A](url : String, data : Seq[A])

object ListResponseWriterProtocol extends DefaultJsonProtocol {
  implicit def containerWriter[T: JsonFormat] = lift {
    new JsonWriter[ListResponseObject[T]] {
      def write(obj: ListResponseObject[T]) = JsObject(
        "object" -> JsString("object"),
        "url" -> JsString(obj.url),
        "count" -> JsNumber(obj.data.length),
        "data" -> obj.data.toJson
      )
    }
  }
}

Unfortunately, when I attempt to use this here

{ ctx : RequestContext =>
        ask(cardTokenActor, ListMessage(account))
            .mapTo[ListResponse]
            .onComplete {
              case Success(ListResponse(list: ListResponseObject[CardToken])) => 
                 ctx.complete(list)
              case Success(_) => ctx.complete(NotFound)
              case Failure(e: Throwable ) => logAndFail(ctx, e)
          }
 }

I run into this error

161: could not find implicit value for evidence parameter of type 
spray.httpx.marshalling.Marshaller[com.smoothpay.services.ListResponseObject
[com.smoothpay.services.cardtoken.entity.CardToken]]
[error] case Success(ListResponse(list: ListResponseObject[CardToken])) =>    
ctx.complete(list)

I've also got all the right imports in place.

import spray.httpx.SprayJsonSupport._
import spray.httpx.marshalling._
import spray.http._
import spray.json._

I'm curious where the problem could be. Appreciate the help in advance.

Was it helpful?

Solution

You can make it a bit easier, i think that should help:

case class ListResponseObject[A](url : String, data : Seq[A])
object ListResponseWriterProtocol extends DefaultJsonProtocol {
  implicit def containerWriter[A: JsonFormat] = jsonFormat2(ListResponseObject.apply[A])
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top