I would like to create a JSON reads converter with type parameter like this:
case class AClass(name: String, age: Int)
case class AClass(name: String)
object GeneralReadsType extends Enumeration {
type GeneralReadsType = Value
val AReadType = Value("atype")
val BReadType = Value("btype")
}
implicit val repoReads: Reads[GeneralReadsType.GeneralReadsType] = {
GeneralReadsType match {
case GeneralReadsType.AReadType => (
(JsPath \ "name").read[String] and
(JsPath \ "age").read[Int]
) (GeneralReadsType.AReadType.apply _)
case GeneralReadsType.BReadType => (
(JsPath \ "name").read[String]
) (GeneralReadsType.BReadType.apply _)
}
}
But when I try to use it in
def getResponse[GeneralReadsType](request: WSRequest): Future[List[GeneralReadsType]] = {
request.get().map {
case response if response.status == 200 => {
(response.json).validate[List[GeneralReadsType]] match {
case JsSuccess(items: List[GeneralReadsType], _) => items
case _: JsError => List[GeneralReadsType]()
}
}
case _ => List[GeneralReadsType]()
}
}
getResponse[AReadType](request)
I get a compile error
No Json deserializer found for type List[GeneralReadsType]. Try to implement an implicit Reads or Format for this type.
So, how should this be achieved?
Thanks!
GeneralReadsType.GeneralReadsType
but getResponse needs a Reads forGeneralReadsType
. - Dylan