4

If I have nested case classes, how can they be converted to JSON using spray-json?

For example my structure looks something like:

case class MyObj1(name: String, myObject2: MyObject2)
case class MyObj2(myObjec3s: Seq[MyObj3])
case class MyObj3(name1: String, name2: String)

The json would look something like:
{
  "name": "",
  "myObject2": [ { "name1": "", "name2": "" }, { "name1": "", "name2": "" }, .... ]
}
Roy Shmuli
  • 4,979
  • 1
  • 24
  • 38
bashan
  • 3,572
  • 6
  • 41
  • 58
  • 2
    Did you try declaring implicit json formats for `MyObj3`, `MyObj2` then `MyObj1`? – Dimitri Dec 07 '14 at 22:39
  • 2
    See "Providing JsonFormats for Case Classes" on [the Spray-json README](https://github.com/spray/spray-json). You are going to want `implicit val obj1Format = jsonFormat2(MyObj1)` and so on inside a class that extends `DefaultJsonProtocol`. – AmigoNico Dec 08 '14 at 02:15
  • Possible duplicate of [Spray-json deserializing nested object](http://stackoverflow.com/questions/21691525/spray-json-deserializing-nested-object) – Matthias Braun Jan 15 '17 at 20:17

0 Answers0