Play Framework 2.3 Scala - 使用隐式Writes转换器将嵌套对象序列化为JSon

时间:2014-08-27 23:11:09

标签: json scala playframework jsonserializer playframework-json

我需要一个前端jquery-component这样一个特定的json对象(ajax响应):

[
    {"division":"IT", "contacts":[
        {“firstname”:”Carl”, “surname”:”Smith”, “empID”:1},
        {“firstname”:”Henry”, “surname”:”Miller”, “empID”:2}
]}, 
    {"division":"Sales", "contacts":[
        {“firstname”:”Nancy”, “surname”:”McDonald”, “empID”:3},
        {“firstname”:”Susan”, “surname”:”McBright”, “empID”:4}
]}
]

在Backend中,数据通过anorm(MySQL)读取并转换为以下对象:

List(Map("division" -> "IT", "contacts" -> List(c3,c4)), Map("division" -> "Sales",          "contacts" -> List(c3,c4)))

然后我尝试将Object序列化为JSon但没有成功(包括隐式Writes转换器)。下面我以同样的方式制作了一个简化的测试用例Idea-Worksheet:

import play.api.libs.json.{JsValue, Json, Writes}

case class Contact(firstname: String, surname: String, empID: Option[Int])

case class ContactDivisionList(division: String, contacts: Seq[Contact])

implicit val ctWrites = new Writes[Contact] {
  def writes(ct: Contact) = Json.obj(
    "firstname" -> ct.firstname,
    "surname" -> ct.surname,
    "empid" -> ct.empID
  )
}

implicit val sdlWrites = new Writes[ContactDivisionList] {
  def writes(dlist: ContactDivisionList) = Json.obj(
    "division" -> dlist.division,
    "contacts" -> Json.toJson(dlist.contacts)
  )
}


/*
Example
*/
val c1 = Contact("Carl","Smith",Option(1))
val c2 = Contact("Henry","Miller",Option(2))
val c3 = Contact("Nancy","McDonald",Option(3))
val c4 = Contact("Susan","McBright",Option(4))

//Test case 1 ->OK
Json.toJson(List(c1,c2,c3,c4))

//Test case 2 ->OK
val c_comp1=List(Map("contacts" -> List(c1,c2)),Map("contacts" -> List(c3,c4)))
//RESULT --> c_comp1: List[scala.collection.immutable.Map[String,List[Contact]]] =     List(Map(contacts -> List(Contact(Carl,Smith,Some(1)), Contact(Henry,Miller,Some(2)))),   Map(contacts -> List(Contact(Nancy,McDonald,Some(3)), Contact(Susan,McBright,Some(4)))))
Json.toJson(c_comp1)
//res1: play.api.libs.json.JsValue = [{"contacts":    [{"firstname":"Carl","surname":"Smith","empid":1},{"firstname":"Henry","surname":"Miller","empid":2}]},{"contacts":[{"firstname":"Nancy","surname":"McDonald","empid":3},{"firstname":"Susan","surname":"McBright","empid":4}]}]


//Test case 3 ->Fail!!!
val c_comp2 = List(Map("division" -> "IT", "contacts" -> List(c1,c2)),Map("division" ->  "Sales", "contacts" -> List(c3,c4)))
//sdlWrites: play.api.libs.json.Writes[ContactDivisionList]{def writes(dlist:    ContactDivisionList): play.api.libs.json.JsObject} = $anon$2@3738baec

Json.toJson(c_comp2)
//!!!!!Error messages
/*Error:(39, 13) No Json serializer found for type     List[scala.collection.immutable.Map[String,java.io.Serializable]]. Try to implement an    implicit Writes or Format for this type.
Json.toJson(c_comp2)
^

Error:(39, 13) not enough arguments for method toJson: (implicit tjs:    play.api.libs.json.Writes[List[scala.collection.immutable.Map[String,java.io.Serializable]]    ])play.api.libs.json.JsValue.
Unspecified value parameter tjs.
Json.toJson(c_comp2)
^
*/

在脚本结束时,您可以看到“测试用例3”,当我执行Json.toJson(c_comp2)时出现错误 - >“找不到类型的Json序列化程序..”。我尝试了很多东西,但我做不到。成功的“测试用例2”的唯一区别是我使用String-Tuppel扩展Map。

我希望sombody可以帮助我解决这个问题,Thx

祝你好运 卡斯滕

1 个答案:

答案 0 :(得分:2)

您的问题是,您所拥有的MapString映射到String(您的部门名称)和List[Contact]的最小上限,恰好是java.io.Serializable

scala> case class Contact(firstname: String, surname: String, empID: Option[Int])
defined class Contact

scala> case class ContactDivisionList(division: String, contacts: Seq[Contact])
defined class ContactDivisionList

scala> val c1 = Contact("Carl","Smith",Option(1))
c1: Contact = Contact(Carl,Smith,Some(1))

scala> val c2 = Contact("Henry","Miller",Option(2))
c2: Contact = Contact(Henry,Miller,Some(2))

scala> val c3 = Contact("Nancy","McDonald",Option(3))
c3: Contact = Contact(Nancy,McDonald,Some(3))

scala> val c4 = Contact("Susan","McBright",Option(4))
c4: Contact = Contact(Susan,McBright,Some(4))

scala> Map("division" -> "IT", "contacts" -> List(c1,c2))
res10: scala.collection.immutable.Map[String,java.io.Serializable] = Map(division -> IT, contacts -> List(Contact(Carl,Smith,Some(1)), Contact(Henry,Miller,Some(2))))

我不完全确定您问题的性质,但如果您已经拥有List[ContactDivisionList],那么将其序列化为JSON非常简单:

scala> implicit val contactWrites = Json.writes[Contact]
contactWrites: play.api.libs.json.OWrites[Contact] = play.api.libs.json.OWrites$$anon$2@3676af92

scala> implicit val contactDivisionListWrites = Json.writes[ContactDivisionList]
contactDivisionListWrites: play.api.libs.json.OWrites[ContactDivisionList] = play.api.libs.json.OWrites$$anon$2@2999d17d

scala> Json.toJson(List(ContactDivisionList("IT", List(c1,c2)), ContactDivisionList("Sales", List(c3, c4))))
res2: play.api.libs.json.JsValue = [{"division":"IT","contacts":[{"firstname":"Carl","surname":"Smith","empID":1},{"firstname":"Henry","surname":"Miller","empID":2}]},{"division":"Sales","contacts":[{"firstname":"Nancy","surname":"McDonald","empID":3},{"firstname":"Susan","surname":"McBright","empID":4}]}]

在我看来,你应该首先避免使用Map。我之前从未使用过anorm,但我认为你需要关注的是数据结构的代码,因为那时你已经失去了类型安全。理想情况下,您应该使用ContactDivisionList或直接使用JsValue个案例构建对象。