I'm a bit confused why the code below doesn't work:
implicit val connectReads: Reads[ConnectCommand] = (
(JsPath \ "uuid").read[String]
)(ConnectCommand.apply _)
private def generateMessage[T](json: JsValue) = json.validate[T] match {
case s: JsSuccess[T] => s.asOpt
case e: JsError => None
}
The function would be called as follows:
generateMessage[ConnectCommand](json)
I'm getting the following errors:
Error:(59, 64) No Json deserializer found for type T. Try to implement an implicit Reads or Format for this type.
private def generateMessage[T](json: JsValue) = json.validate[T] match {
^
Error:(59, 64) not enough arguments for method validate: (implicit rds: play.api.libs.json.Reads[T])play.api.libs.json.JsResult[T].
Unspecified value parameter rds.
private def generateMessage[T](json: JsValue) = json.validate[T] match {
^
I'm fairly new to Scala generics... is there any way to do what I'm trying to do here?
According to the documentation
JsValue.validate
requires an implicitReads
for your type to be available:Assuming you have it available at the place you are calling
generateMessage
from, you have to pass it intogenerateMessage
, so thatvalidate
would see it as well:or the shorter form: