Chris Stewart
Chris Stewart

Reputation: 1679

Cannot find JsonReader or JsonFormat type class for List[...]

I'm trying to use spray's implicit List[T] marshallers. Here is a json formatter I have defined

object ImportMultiResponseMarshaller extends DefaultJsonProtocol {

  val successKey = "success"
  val errorKey = "error"

  implicit object ImportMultiResponseFormatter extends RootJsonFormat[ImportMultiResponse] {
    override def read(value: JsValue): ImportMultiResponse = {
      val f = value.asJsObject.fields
      val success = f(successKey).convertTo[Boolean]
      val error = f(errorKey).convertTo[String]
      ImportMultiResponse(success,error)
    }

    override def write(response: ImportMultiResponse): JsValue = {
      val m = Map(
        successKey -> JsBoolean(response.success),
        errorKey -> JsString(response.error)
      )
      JsObject(m)
    }
  }
}

and here is where I am trying to use the code:

  def importMulti(request: ImportMultiRequest): Future[ImportMultiResponse] = {
    import spray.json._
    import DefaultJsonProtocol._
    import org.bitcoins.rpc.marshallers.wallet.ImportMultiRequestMarshaller._
    import org.bitcoins.rpc.marshallers.wallet.ImportMultiResponseMarshaller._
    val json = request.toJson
    val array = JsArray(json)
    val cmd = "importmulti"
    sendCommand(cmd,array).map { json =>
      val result = json.convertTo[List[ImportMultiResponse]]
      result.head
    }
  }

and here is the error I am getting:

[error] /home/chris/dev/bitcoin-s-rpc-client/src/main/scala/org/bitcoins/rpc/RPCClient.scala:280: Cannot find JsonReader or JsonFormat type class for List[org.bitcoins.rpc.bitcoincore.wallet.ImportMultiResponse]
[error]       val result = json.convertTo[List[ImportMultiResponse]]

I've looked at other questions with respect to this, but it seems like they were forgetting to import spray.json._ or DefaultJsonProtocol._ which I have done. Does anyone else see what I am doing wrong?

I am using the akka-http-spray-json library instead of only spray, if that matters.

Upvotes: 2

Views: 2631

Answers (1)

Tolga Celik
Tolga Celik

Reputation: 1

trait JsonProtocol extends SprayJsonSupport with DefaultJsonProtocol{
  implicit val applicationInfoFormat = jsonFormat3(ApplicationInfo)
...
}

In a class i have some jsonFormats like above , and I had to extend JsonProtocol. I imported related protocol classes where i needed.

Upvotes: 0

Related Questions