How to deserialize nested objects correctly in spray-json?
import spray.json._
case class Person(name: String)
case class Color(n: String, r: I
To your remaining question - how to reuse JSON conversions within a wrapping type:
"person-field" -> JsObject("p-name" -> JsString(c.p.name))
I would change this to:
"person-field" -> p.toJson
This way, you are letting the Person
case class JSONify itself, instead of introducing another way in the wrapping object. DRY and simpler.
And:
case Seq(JsString(name), JsNumber(red), JsNumber(green), JsNumber(blue), JsObject(person)) =>
Color(name, red.toInt, green.toInt, blue.toInt, null)
Use the .convertTo[Person]
here:
case Seq(JsString(name), JsNumber(red), JsNumber(green), JsNumber(blue), jsv) =>
Color(name, red.toInt, green.toInt, blue.toInt, jsv.convertTo[Person])
If there are problems, please ask for more help. I have similar structure in my own project but didn't try to run them in this context.