gpt4 book ai didi

json - Play Framework 2.1.1 Json 写入嵌套对象

转载 作者:行者123 更新时间:2023-12-02 09:08:40 25 4
gpt4 key购买 nike

整个下午我一直在努力解决这个问题,但没有成功,Play 2.1.1 彻底改变了 Json 读写工作的方式。

基本上我有一个包装对象,如下所示:

case class CombinedUser(user: SocialUser, userdetails: UserDetails)

如您所见,它包含 2 个我想要序列化和反序列化为 json 的类。

但我不明白如何使用新设计获取和设置子类中的字段。

例如

implicit val combinedUser2Reads = (
(__ \ "email").read[String] and
(__ \ "providerid").read[String] and
(__ \ "firstname").read[String] and
(__ \ "lastname").read[String] and
(__ \ "fullname").read[String] and
(__ \ "avatarurl").read[String] and
(__ \ "address1").read[String] and
(__ \ "address2").read[String] and
(__ \ "address3").read[String] and
(__ \ "city").read[String] and
(__ \ "country").read[String] and
(__ \ "phone").read[String] and
(__ \ "publickey").as[String]
)(CombinedUser2.apply _)

我想要一个包含大多数子类字段(所有字符串)的 json blob。

因为它使用了 apply 东西,所以我不知道如何在映射之前创建子类。

非常感谢任何帮助或指导。

谢谢

汤姆

最佳答案

你可以这样做:

case class SocialUser(firstName: String, lastName: String)
case class UserDetails(avatarUrl: String, phone: String)
case class CombinedUser(user: SocialUser, userDetails: UserDetails)
implicit val combinedUserReads: Reads[CombinedUser] = (
(__ \ "user").read((
(__ \ "firstName").read[String] and
(__ \ "lastName").read[String]
)(SocialUser)) and
(__ \ "userDetails").read((
(__ \ "avatarUrl").read[String] and
(__ \ "phone").read[String]
)(UserDetails))
)(CombinedUser)

但是,最好创建单独的读取:

implicit val socialUserReads = (
(__ \ "firstName").read[String] and
(__ \ "lastName").read[String]
)(SocialUser)
implicit val userDetailsReads = (
(__ \ "avatarUrl").read[String] and
(__ \ "phone").read[String]
)(UserDetails)
implicit val combinedUserReads: Reads[CombinedUser] = (
(__ \ "user").read[SocialUser] and
(__ \ "userDetails").read[UserDetails]
)(CombinedUser)

编辑:对于简单的案例类,its possible to do :

implicit val socialUserReads = Json.format[SocialUser]
implicit val userDetailsReads = Json.format[UserDetails]
implicit val combinedUserReads = Json.format[CombinedUser]

Here对 JSON Reads 等内容进行了相当全面的介绍。

How about partial objects? If I don't want to fill in every field in the constructor, can I pass empties or do I overload the constructor or similar?

使用选项:

case class CombinedUser(user: SocialUser, userDetails: Option[UserDetails])
//reads
implicit val combinedUserReads: Reads[CombinedUser] = (
(__ \ "user").read[SocialUser] and
(__ \ "userDetails").readOpt[UserDetails]
)(CombinedUser)
//writes
implicit val combinedUserWrites: Writes[CombinedUser] = (
//socialUserWrites and userDetailsWrites must be in scope
(__ \ "user").write[SocialUser] and
(__ \ "userDetails").write[Option[UserDetails]]
)(unlift(CombinedUser.unapply))

val json = Json.obj(
"user" -> Json.obj(
"firstName" -> "Homer",
"lastName" -> "Simpson"
)
)
Json.fromJson[CombinedUser](json)
//JsSuccess(CombinedUser(SocialUser(Homer,Simpson),None),)

关于json - Play Framework 2.1.1 Json 写入嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16021437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com