Skip to content

Instantly share code, notes, and snippets.

@josephpconley
Created February 12, 2014 17:29
Show Gist options
  • Save josephpconley/8960352 to your computer and use it in GitHub Desktop.
Save josephpconley/8960352 to your computer and use it in GitHub Desktop.
Play controller serving nested case class as Json
package controllers
case class Root(id: Int, name: String, children: Seq[Child])
case class Child(id: Int, name: String)
object Application extends Controller with Secured{
def test = Action { implicit req =>
//childFmt must go first, otherwise the rootFmt will not compile
implicit val childFmt = Json.writes[Child]
implicit val rootFmt = Json.writes[Root]
val children = Seq(
Child(1, "child1"),
Child(2, "child2"),
Child(3, "child3")
)
val root = Root(0, "root", children)
Ok(Json.toJson(root))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment