Rajeev Rajeev - 1 month ago 41
Scala Question

unmarshalling json in spray-json

I am trying to unmarshal json string. But end up with error. Not able to understand error properly.

Below is the code :

import akka.actor.ActorSystem
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import spray.json._
import scala.concurrent.Future
import org.json4s.{Serialization, jackson}

object Test extends App{
val json = """{
| "entries": [
| {
| ".tag": "folder",
| "name": "Camera Uploads",
| "path_lower": "/camera uploads",
| "path_display": "/Camera Uploads",
| "id": "id:up-mXElWkuAAAAAAAAAHsw"
| },
| {
| ".tag": "file",
| "name": "roboQuestions.mov",
| "path_lower": "/roboquestions.mov",
| "path_display": "/roboQuestions.mov",
| "id": "id:up-mXElWkuAAAAAAAAAUUg",
| "client_modified": "2016-08-23T16:24:53Z",
| "server_modified": "2016-08-23T18:13:34Z",
| "rev": "1a9d06ecb0f6",
| "size": 110339964
| }
| ],
| "cursor": "AAFA2NDm5UrAaaG6s2gwGbyh1Fq5Na6EE-UMpccSv2PrvAmfh-54645-6f4S2cYmnj0G0r1aMMy0KlP9VWllp3oWIaYtAKDQfj3zSCiWo_lB98MnRlEKDA0sRexYsTI68dlaDM_Yimiy",
| "has_more": false
|}""".stripMargin.parseJson

implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val jacksonSerialization: Serialization = jackson.Serialization
import concurrent.ExecutionContext.Implicits.global

final case class BoxFolder(id:String, `.tag`:Option[String], name:Option[String], path_lower:Option[String], path_display:Option[String], client_modified:Option[String], server_modified:Option[String], rev:Option[String], size:Option[Long])
object BoxFolder extends DefaultJsonProtocol with SprayJsonSupport
{
implicit val folderFormat = jsonFormat9(BoxFolder.apply)
}

final case class BoxList(entries:List[BoxFolder], cursor:String, has_more:Boolean)
object BoxList extends DefaultJsonProtocol with SprayJsonSupport
{
implicit val folderFormat = jsonFormat3(BoxList)
}
val resFuture:Future[BoxList] = Unmarshal(json).to[BoxList]
resFuture.map(println)
system.terminate()
}


Error :

Error:(279, 53) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[spray.json.JsValue,Test.BoxList]
val resFuture:Future[BoxList] = Unmarshal(json).to[BoxList]

Error:(279, 53) not enough arguments for method to: (implicit um: akka.http.scaladsl.unmarshalling.Unmarshaller[spray.json.JsValue,Test.BoxList], implicit ec: scala.concurrent.ExecutionContext, implicit mat: akka.stream.Materializer)scala.concurrent.Future[Test.BoxList].
Unspecified value parameters um, ec, mat.
val resFuture:Future[BoxList] = Unmarshal(json).to[BoxList]

Answer

The only thing missing is implicit unmarshaller for type JsValue to your target type BoxList:

  implicit val boxListUnmarshaller: Unmarshaller[JsValue, BoxList] =
    Unmarshaller.strict(jsValue => boxListFormat.read(jsValue))

The whole working example from your original code:

import akka.actor.ActorSystem
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller}
import akka.stream.ActorMaterializer
import spray.json._

import scala.concurrent.Future
import org.json4s.{Serialization, jackson}

object Test extends App {
  val json =
    """{
      |    "entries": [
      |        {
      |            ".tag": "folder",
      |            "name": "Camera Uploads",
      |            "path_lower": "/camera uploads",
      |            "path_display": "/Camera Uploads",
      |            "id": "id:up-mXElWkuAAAAAAAAAHsw"
      |        },
      |        {
      |            ".tag": "file",
      |            "name": "roboQuestions.mov",
      |            "path_lower": "/roboquestions.mov",
      |            "path_display": "/roboQuestions.mov",
      |            "id": "id:up-mXElWkuAAAAAAAAAUUg",
      |            "client_modified": "2016-08-23T16:24:53Z",
      |            "server_modified": "2016-08-23T18:13:34Z",
      |            "rev": "1a9d06ecb0f6",
      |            "size": 110339964
      |        }
      |    ],
      |    "cursor": "AAFA2NDm5UrAaaG6s2gwGbyh1Fq5Na6EE-UMpccSv2PrvAmfh-54645-6f4S2cYmnj0G0r1aMMy0KlP9VWllp3oWIaYtAKDQfj3zSCiWo_lB98MnRlEKDA0sRexYsTI68dlaDM_Yimiy",
      |    "has_more": false
      |}""".stripMargin.parseJson

  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()
  implicit val jacksonSerialization: Serialization = jackson.Serialization

  import concurrent.ExecutionContext.Implicits.global

  case class BoxFolder(id: String,
                       `.tag`: Option[String],
                       name: Option[String],
                       path_lower: Option[String],
                       path_display: Option[String],
                       client_modified: Option[String],
                       server_modified: Option[String],
                       rev: Option[String],
                       size: Option[Long])

  object BoxFolder extends DefaultJsonProtocol with SprayJsonSupport {
    implicit val folderFormat = jsonFormat9(BoxFolder.apply)
  }

  case class BoxList(entries: List[BoxFolder], cursor: String, has_more: Boolean)

  object BoxListFormat extends DefaultJsonProtocol with SprayJsonSupport {
    implicit val boxListFormat = jsonFormat3(BoxList)
  }

  import BoxListFormat._

  implicit val boxListUnmarshaller: Unmarshaller[JsValue, BoxList] =
    Unmarshaller.strict(jsValue => boxListFormat.read(jsValue))

  val resFuture: Future[BoxList] = Unmarshal(json).to[BoxList]
  resFuture.map(println)
  system.terminate()
}

Hope it helps, sir!