2016-10-15 1 views
1

Je suis en train d'essayer unmarshal json string. Mais se retrouver avec une erreur. Pas capable de comprendre l'erreur correctement.unmarshalling json dans spray-json

Voici le 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() 
} 

Erreur:

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] 

Répondre

1

La seule chose qui manque est unmarshaller implicite pour le type JsValue à votre type de cible BoxList:

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

Le tout exemple de travail à partir de votre code d'origine:

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() 
} 

espère que cela aide, monsieur!