I am struggling with a request to JSON conversion.
Error : No apply function found for CommonRequest
ClassA.scala
trait CommonRequest{
//def someFunc()
//val a: String
}
object CommonRequest {
implicit val CommonRequestRequestFormat: OFormat[CommonRequest] = Json.format[CommonRequest] // Error: No apply function found for CommonRequest
}
ClassB.scala
case class DerivedRequestA(One: Int) extends CommonRequest{
//Overridden def and vals
}
case class DerivedRequestB(Two: Int) extends CommonRequest{
//Overridden def and vals
}
object DerivedRequestA {
implicit val RequestAFormat: OFormat[DerivedRequestA] = Json.format[DerivedRequestA]
}
object DerivedRequestB {
implicit val RequestAFormat: OFormat[DerivedRequestB] = Json.format[DerivedRequestB]
}
ClassC.scala
def CallAPI(request: CommonRequest): Future[CommonResponse] = {
//call api with JSON request body
request_body = toJsonStringValue(request) // This call needs write implicit
}
def toJsonStringValue[T](o: T)(implicit t: Writes[T]): String = {
Json.toJson(o).toString()
}
Looks like, only companion object is not enough. What else I need to do here ?