I am trying to refactor some models I have that currently look like this:
case class Person(name: String, age: Int)
object Person {
implicit val reads: Reads[Person] = (
(JsPath \ "name").read[String] and
(JsPath \ "age").read[Int]
)(Person.apply _)
}
To something that looks like this:
abstract class BaseModel {
val pk: String // Some stuff here that will be common
}
object BaseModel {
implicit val reads: Reads[BaseModel] // Not sure what to do here
}
So that I can do this:
trait MyTrait[Model <: BaseModel] {
// More code here
val body: JsObject = ...
val parsed = body.validate[Model] // Error: There is no implicit value defined for Model
}
case class NewPerson extends BaseModel {...}
object NewPerson {...} // Maybe need to extend something here
class MyController extends MyTrait[NewPerson]
I want every model to define an implicit reads value, but I am unsure about how to indicate this in the abstract class's companion object.