I have a class that looks somewhat like that
import java.time.OffsetDateTime
import spray.json._
import DefaultJsonProtocol._
sealed trait Person {
def firstName: String
def country: String
def lastName: String
def salary: Option[BigDecimal]
}
case class InternalPerson(
firstName: String,
country: String,
lastName: Option[BigDecimal],
salary: Option[BigDecimal]
) extends Person
object Person {
def fromName(name: Name, country: String, salary: Option[BigDecimal]): Person = {
InternalPerson(
firstName = name.firstName,
lastName = name.lastName,
country = country,
salary = salary
)
}
}
object PersonJsonProtocol extends DefaultJsonProtocol {
implicit val personFormat = jsonFormat4(Person.apply)
}
I am just trying to add a json support to my class. whenever I import the protocol and spray.json._
from another classes I get:
Note: implicit value personFormat is not applicable here because it comes after the application point and it lacks an explicit result type
and
value apply is not a member of object of Person
any idea on how to have Json support for companion objects that extends a trait in scala?
If you defined your implicit in terms of the case class, InternalPerson, json formatting should be enabled: implicit val personFormat = jsonFormat4(InternalPerson)
And you won't have to define an apply() method, which you will have to do in either the Person trait or any implementation thereof otherwise.