我正在撰写JSON
Writes
。在models/Users.scala,
中,我定义了一个implicit
对象,其中包含implicit
个定义。
object UserImplicits {
/*Writes (write to JsValue) are used by toJson method of Json object to convert data (say the model) to JsValue*/
implicit val profileWrites:Writes[UserProfile] = (
(JsPath \ "confirmed").write[Boolean] and
(JsPath \ "email").write[String] and
(JsPath \ "firstname").write[String] and
(JsPath \ "lastname").write[String]
) (unlift(UserProfile.unapply))
implicit val userWrites: Writes[User] = (
(JsPath \ "id").write[UUID] and
(JsPath \ "user-profile").write[UserProfile]
) (unlift(User.unapply))
implicit val usersResourceWrites:Writes[UsersResource] = (
(JsPath \ "id").write[String] and
(JsPath \ "url").write[String] and
(JsPath \ "user").write[User]
) (unlift(UsersResource.unapply))
/*Reads (read from JsValue) is used by Json object's as or asOpt methods to convert JsValue to some other data, eg your model*/
implicit val profileReads:Reads[UserProfile] = (
(JsPath \ "confirmed").read[Boolean] and
(JsPath \ "email").read[String] and
(JsPath \ "firstname").read[String] and
(JsPath \ "lastname").read[String]
) (UserProfile.apply _)
implicit val userReads: Reads[User] = (
(JsPath \ "id").read[UUID] and
(JsPath \ "user-profile").read[UserProfile]
) (User.apply _)
implicit val usersResourceReads: Reads[UsersResource] = (
(JsPath \ "id").read[String] and
(JsPath \ "url").read[String] and
(JsPath \ "user").read[User]
) (UsersResource.apply _)
}
在我的controller
课程中,我导入了models._
,并按如下方式定义了控制器:
import models._
import scala.concurrent.{ExecutionContext, Future}
class UserController @Inject()(cc: ControllerComponents)(implicit exec: ExecutionContext) extends AbstractController(cc){
//TODOM - remove hard coded response
def addUser = Action.async{ implicit request => {
println("addUser controller called")
val user = User(UUID.randomUUID(),UserProfile(true,"m@m.com","m","c"))
val userResource = UsersResource(user.id.toString(),"/ws/users",user)
val json = Json.toJson(userResource); //converts the model to JsValue using Writes defined in Users model class
println("returning json:",Json.prettyPrint(json))
Future{Ok(json)}}
}
我收到以下编译错误。
代码No Json serializer found for type models.UsersResource. Try to implement an implicit Writes or Format for this type.
的 val json = Json.toJson(userResource);
问题似乎是Play无法找到隐式写入。如果我在控制器中移动隐式定义而不是在模型中定义,则代码可以工作。如何在控制器类中看到model / user.scala中定义的含义?
答案 0 :(得分:0)
如果将implicits移动到您要(序列化)的类的伴随对象中,编译器将自动拾取它们。所以,如果这是你的UserProfile
案例类:
case class UserProfile(confirmed: Boolean,
email: String,
firstname: String,
lastname: String)
...然后你可以在下面写下这个(关键点是它的名字相同):
object UserProfile {
implicit val profileWrites: Writes[UserProfile] = //...
implicit val profileReads: Reads[UserProfile] = //...
}
或者,只使用一个Format
(一个Reads
和一个Writes
汇总到一个),如果JSON结构与您的案例类字段完全对应,则可以轻松实现名称:
object UserProfile {
implicit val profileFormat: Format[UserProfile] = Json.format[UserProfile]
或者,您可以import UserImplicits._
。
答案 1 :(得分:0)
我必须创建另一个对象(不是伴随对象)并在那里添加隐式定义。
要使用这些含义,请在需要隐含的文件中使用import models.UserImplicits._。
object UserImplicits {
/*Writes (write to JsValue) are used by toJson method of Json object to convert data (say the model) to JsValue*/
implicit val profileWrites:Writes[UserProfile] = (
(JsPath \ "confirmed").write[Boolean] and
(JsPath \ "email").writeNullable[String] and
(JsPath \ "firstname").writeNullable[String] and
(JsPath \ "lastname").writeNullable[String]
) (unlift(UserProfile.unapply))
implicit val userWrites: Writes[User] = (
(JsPath \ "id").write[UUID] and
(JsPath \ "user-profile").write[UserProfile]
) (unlift(User.unapply))
implicit val usersResourceWrites:Writes[UsersResource] = (
(JsPath \ "id").write[String] and
(JsPath \ "url").write[String] and
(JsPath \ "user").write[User]
) (unlift(UsersResource.unapply))
/*Reads (read from JsValue) is used by Json object's as or asOpt methods to convert JsValue to some other data, eg your model*/
implicit val profileReads:Reads[UserProfile] = (
(JsPath \ "confirmed").read[Boolean] and
(JsPath \ "email").readNullable[String] and
(JsPath \ "firstname").readNullable[String] and
(JsPath \ "lastname").readNullable[String]
) (UserProfile.apply _)
implicit val userReads: Reads[User] = (
(JsPath \ "id").read[UUID] and
(JsPath \ "user-profile").read[UserProfile]
) (User.apply _)
implicit val usersResourceReads: Reads[UsersResource] = (
(JsPath \ "id").read[String] and
(JsPath \ "url").read[String] and
(JsPath \ "user").read[User]
) (UsersResource.apply _)
}