2013-04-15 117 views
4

我一直試圖讓我的頭在整個下午都無濟於事,Play 2.1.1修改了Json如何讀寫工作。Play Framework 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斑點。

因爲它使用了應用的東西,所以我沒有看到如何在映射之前創建子類。

任何幫助或指導,非常感謝。

感謝

湯姆

回答

7

你可以這樣來做:

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) 

但是它更能營造分離Reads

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) 

編輯:簡單的case類, its possible to do

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

Here是相當全面的介紹JSON閱讀和更多。

部分物體怎麼樣?如果我不想填寫構造函數 的每個字段,我是否可以傳遞容器或者是否超載構造函數 或類似的函數?

使用Option

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),) 
+0

感謝您的回答無窮大,它的起點,使更多的意義了。部分對象如何?如果我不想填寫構造函數中的每個字段,是否可以傳遞容器或重載構造函數或類似函數? – magicaltrout

+0

感謝無限,您的例子對於像我這樣的新手來說比文檔更容易理解。 – magicaltrout

+0

我非常愛你!謝謝 –