json - 使用 Json.format 没有可用的 MyClass 隐式格式

标签 json scala playframework-2.0 implicit

在 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/

相关文章:

iphone - iOS JSON 解析第二次调用时返回 null

scala - 为什么 Scala 2.11 编译器重命名我的私有(private)方法?

json - Spark对json的异常处理

scala - 在playframework 2/scala中上传多个大文件和其他表单数据

Scala Play 和多项目 SBT 构建

javascript - 如何处理项目点击从 ng-repeat 生成的列表并加载下一页

json - 记录的简单 JSON 反序列化不正确(Delphi Sydney [10.4.1])

json - 使用 Typescript 处理 JSON 中数组的匿名元素

scala - Maven 构建错误(Scala + Spark):object apache is not a member of package org

forms - Play framework 2.0 scala 表单错误处理