crockpotveggies
crockpotveggies

Reputation: 13320

Play! 2.4 - Implicit Reads for ScalaJson w/ Generic Types

Using Play 2.4 ScalaWS. I've defined a method that takes a type manifest T and performs a GET request to an external API. The problem is that it won't compile because there isn't an implicit Reads for parsing JSON.

Here's the code:

def myGet[T](path: String)(implicit m: Manifest[T]): Future[Either[model.MyError,T]]  = {
    val url = MY_HOST+"/"+path
    ws
      .url(url)
      .withHeaders(myHeaders: _*)
      .get()
      .map { response =>
        try {
          Right(response.json.as[T])
        } catch {
          // check if this response was an error
          Left(response.json.as[model.MyError])
        }
      }

  }

The compilation error is specifically:

Compilation error[No Json deserializer found for type T. Try to implement an implicit Reads or Format for this type.]

I'm not sure of the simplest way to do this. Thanks for your help.

Edit

I also tried (implicit m: Manifest[T], reads: Reads[T]) with no luck.

Upvotes: 2

Views: 627

Answers (1)

crockpotveggies
crockpotveggies

Reputation: 13320

It turns out using (implicit m: Manifest[T], readsT: Reads[T]) and having the Reads be an implicit parameter was the correct way of doing this. I had to run sbt clean since something was improperly cached in the incremental compiler.

It now works just fine.

Upvotes: 4

Related Questions