Providing a JsonFormat for a Sequence of Objects

余生颓废 提交于 2020-01-03 15:57:48

问题


i`m trying here to find some help to apply an JsonFormat extended of the DefaultJsonProtocol to an class containing a Sequence of Objects.

So for the classes:

class Person(val name: String, [......], val adresses: Seq[Adress])
class Adress(val streetname: String, val plz: BigDecimal, val city: String)

now i would like to apply my JsonFormat:

object PersonJsonProtocol extends DefaultJsonProtocol {
  implicit object PersonJsonFormat extends RootJsonFormat[Person] {
    def write(pers: Person) = JsObject(
    "name" -> JsString(pers.name),
    [......],
    "adresses" -> JsArray(pers.adresses)
)
def read(value: JsValue) = {...}
}

But actually i am not sure how to do it. I searched through the spray-json documentation and throug google, stackoverflow & Co. I am totally new to Scala/Spray and perhaps i am just missing the point. So perhaps somebody here is so kind to help me. Without the Adress sequence i will work.

With the JsArray as provided in the example i get an type mismatch. It is exspecting an List[JsValue] but also with converting to list the mismatch still stands.

I also tried to inserts an seperate AdressJsonProtocol and include it via: "addresses" -> AdressJsonFormat.write(pers.adresses) but yet again it is an Sequence...


回答1:


Look at the source of spray.json.CollectionFormats.

Here is a runnable implementation:

import spray.json._

class Adress(val streetname: String, val plz: BigDecimal, val city: String)

class Person(val name: String, val adresses: Seq[Adress])

object PersonJsonProtocol extends DefaultJsonProtocol {
  implicit object AdressJsonFormat extends RootJsonFormat[Adress] {
    def write(addr: Adress) = JsObject(Map(
      "streetname" -> JsString(addr.streetname),
      "plz" -> JsNumber(addr.plz),
      "city" -> JsString(addr.city)
    ))
    def read(value: JsValue): Adress = ???
  }
  implicit object PersonJsonFormat extends RootJsonFormat[Person] {
    def write(pers: Person) = JsObject(Map(
      "name" -> JsString(pers.name),
      "adresses" -> JsArray(pers.adresses.map(_.toJson).toList)
    ))
    def read(value: JsValue): Person = ???
  }
}

object Main extends App {
  import PersonJsonProtocol._
  val person = new Person("joe", Seq(new Adress("street", 123, "city")))
  println("poso's default toString: %s".format(person))
  val personJVal = person.toJson
  println("JValue's toString: %s".format(personJVal))
  val personJson = personJVal.prettyPrint
  println("pretty-printing: %s".format(personJson))
}

which yields:

poso's default toString: Person@680ccad
JValue's toString: {"name":"joe","adresses":[{"streetname":"street","plz":123,"city":"city"}]}
pretty-printing: {
  "name": "joe",
  "adresses": [{
    "streetname": "street",
    "plz": 123,
    "city": "city"
  }]
}



回答2:


You don't need to write a DefaultJsonProtocol for each case class, except if you want some special logic (formatting, filtering ...)

Have you tried to simply use the default case class serialization?

implicit val formatPerson = jsonFormat6(Adress)
implicit val formatAddress = jsonFormat3(Adress)

The number in jsonFormat'number' stands for the number of members in your case class.

Then spray-json will take care of your nested Address collection when serializing a Person.



来源:https://stackoverflow.com/questions/21756836/providing-a-jsonformat-for-a-sequence-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!