I have a class Person and class Employee. Class Employee extends class Person.
My current implicit writes to convert classes to JSON looks like this:
implicit val implicitPersonWrites = new Writes[Person] {
def writes(v: Person): JsValue = {
Json.obj("label" -> v.label, "age" -> v.age)
}
}
implicit val implicitEmployeeWrites = new Writes[Employee] {
def writes(v: Employee): JsValue = {
Json.obj("label" -> v.label, "age" -> v.age, "company" -> v.company)
}
}
The problem is that even my object is of type Employee, implicit write for Person superclass is always used. So at the end the fields specific to Employee class are not present in the output. How to make implicit writes properly in case of inheritance?
PersonthePersoninstance will be used, even if it's "really" anEmployee. - Travis Brown