在 Json.format 上使用复杂对象作为另一个对象的属性时出现错误。
我有两个类(class):角色DTO 和 EmailInvitationDTO . EmailInvitationDTO 有一个 角色DTO .于是,我宣布:
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] }
我收到错误: 没有隐式格式角色DTO 可用的。即使我声明 角色格式变量在 之前的一行中电子邮件邀请格式 :
object EmailInvitationDTO {
val roleFormat = Json.format[RoleDTO]
val emailInvitationFormat = Json.format[EmailInvitationDTO]
}
有谁知道缺少什么?谢谢。
最佳答案
您需要包含一个隐式 roleFormat
在您的 EmailInvitationDTO
对象声明。 Json.format
宏在编译时寻找隐含的 Json 格式,否则它不知道如何读/写 RoleDTO
在您的 EmailInvitationDTO
.
因此,在创建 emailInvitationFormat
之前,您需要在范围内添加以下行:
implicit val roleFormat = Json.format[RoleDTO]
关于json - 使用 Json.format 没有可用的 MyClass 隐式格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28676421/