Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No implicit format for MyClass available using Json.format

I'm getting an error when using a complex object as attribute of another object on Json.format.

I have two classes: RoleDTO and EmailInvitationDTO. EmailInvitationDTO has a RoleDTO. So, I declared:

case class RoleDTO(id:Option[Long] = None, roleType:Int, userID:Long, fromHousingUnitID:Option[Long] = None, isAdmin:Option[Boolean] = None, fromResidentUserID:Option[Long] = None, documentNumber:Option[String] = None, fromCondoID:Option[Long] = None)
object RoleDTO { val roleFormat = Json.format[RoleDTO] }

case class EmailInvitationDTO(firstName:String, lastName:String, email:String, role:RoleDTO)
object EmailInvitationDTO{ val emailInvitationFormat = Json.format[EmailInvitationDTO] }

I'm getting the error: No implicit format for RoleDTO available. Even if I declare the roleFormat variable in a line before emailInvitationFormat:

object EmailInvitationDTO {
    val roleFormat = Json.format[RoleDTO]
    val emailInvitationFormat = Json.format[EmailInvitationDTO]
}

Anyone knows what is missing? Thanks.

like image 615
adheus Avatar asked Feb 23 '15 14:02

adheus


1 Answers

You need to include an implicit roleFormat in your EmailInvitationDTO object declaration. The Json.format macro looks for implicit Json formats at compile time, otherwise it will have no idea how to read/write the RoleDTO in your EmailInvitationDTO.

So you'll need the following line in scope before creating an emailInvitationFormat:

implicit val roleFormat = Json.format[RoleDTO]
like image 53
josephpconley Avatar answered Oct 23 '22 21:10

josephpconley