简体   繁体   中英

JSON Reads Writes for List[(Foo, List[FoodChildren]) ]

As question says

How to best write Writes [List[(Foo, List[FoodChildren]) ] ] where each Foo and FoodChildren itself are case class ?

I am on Scala 2.11, play framework 2.3.1

Can you use a third case class instead of a tuple? JSON doesn't support tuples.

If you use a third case class than you can use Json.format[...] to build the Reads/Writes.

Just write your own Writer instead of using the macro:

implicit val myTupleWrites = new Writes[(Int,List[FoodChildren])] {
  def writes(myTuple: (Int,List[FoodChildren])) = Json.obj(
    "i" -> myTuple._1,
    "lst" -> myTuple._2
  )
}

https://www.playframework.com/documentation/2.3.x/ScalaJson

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM