There is no implicit format for MyClass available using Json.format

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

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

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 get an error: Implicit format for RoleDTO is not available. Even if I declare the roleFormat variable in the line before emailInvitationFormat :

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

Does anyone know what is missing? Thanks.

+5
source share
1 answer

You need to include the implicit roleFormat in the declaration of the EmailInvitationDTO object. The Json.format macro Json.format for implicit Json formats at compile time, otherwise it does not know how to read / write RoleDTO in EmailInvitationDTO .

So, before creating emailInvitationFormat :

you will need the following line in the scope:
 implicit val roleFormat = Json.format[RoleDTO] 
+5
source

Source: https://habr.com/ru/post/1213996/


All Articles