1
votes

I had an Main app with the following structure:

implicit val orgs: RootJsonFormat[GHOrg]                = jsonFormat3(GHOrg)
implicit val users: RootJsonFormat[GHUser]              = jsonFormat2(GHUser)
implicit val repos: RootJsonFormat[GHRepo]              = jsonFormat3(GHRepo)
case class GHUser(login: String, contributions: Option[Int] = None)
case class GHRepo(name: String, owner: GHUser, contributors_url: String)
case class GHOrg(name: String, repos_url: String, members_url: String)

At some point I do

Unmarshal(e).to[List[GHRepo]]

And everything works, however, I have been doing a little bit of code cleaning and I have moved the three case classes above to a different package, and now I am getting title's error. I have tried this suggestion (spray-json error: could not find implicit value for parameter um) but does not work.

Here is how the current code (Not compiling):

import akka.actor.ActorSystem
import akka.http.javadsl.model.StatusCodes
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import com.elbauldelprogramador.domain.{GHOrg, GHRepo, GHUser}
import org.slf4j.LoggerFactory
import spray.json.DefaultJsonProtocol._
import spray.json.RootJsonFormat

object Main extends App {

  implicit val system: ActorSystem                        = ActorSystem()
  implicit val materializer: ActorMaterializer            = ActorMaterializer()
  implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  implicit val orgs: RootJsonFormat[GHOrg]                = jsonFormat3(GHOrg)
  implicit val users: RootJsonFormat[GHUser]              = jsonFormat2(GHUser)
  implicit val repos: RootJsonFormat[GHRepo]              = jsonFormat3(GHRepo)
// ....
Unmarshal(e).to[List[GHRepo]] // Error
// ....
}

Here is the full error:

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.scaladsl.model.ResponseEntity,List[com.elbauldelprogramador.domain.GHUser]] [error] val users = Unmarshal(e).to[List[GHUser]] [error] ^

1
What is error you got? Try don't use akka to localize the error, e.g., use jsonBody.parseJson.convertTo[List[GHRepo]].Mikhail Ionkin
I don't have a jsonBody, I have a ResponseEntityElBaulP
You can copy string from response and pass it to Main for test. And what is um? PS: I found it is implicit um: FromRequestUnmarshaller[T]Mikhail Ionkin
I can convert List[GHRepo] with use different packages for case classes. I use """[{"owner":{"login": "sd"}}]""".parseJson.convertTo[List[GHRepo]] and got List(GHRepo(GHUser(sd))) (I remove 2 fields from case classes to simplify).Mikhail Ionkin

1 Answers

3
votes

I finally was able to solve it reading spray's docs:

Basically, I created a separated file for each case class and one object called MyJsonProtocol with the following code:

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val users = jsonFormat2(GHUser)
  // ...
}

Then in my main app I just import both things:

import com.elbauldelprogramador.api.MyJsonProtocol._
import com.elbauldelprogramador.domain.{GHOrg, GHRepo, GHUser}