totem3
1/14/2014 - 11:50 PM

gistfile1.txt

scala> import org.json4s._
import org.json4s._

scala> import org.json4s.native.JsonMethods._
import org.json4s.native.JsonMethods._

scala> import org.json4s.JsonDSL._
import org.json4s.JsonDSL._

scala> case class Organization(name:String)
defined class Organization

scala> case class Person(name: String, organization: Organization)
defined class Person

scala> implicit def Organization2Json(o:Organization): JValue = JObject(JField("name", JString(o.name)))
warning: there were 1 feature warning(s); re-run with -feature for details
Organization2Json: (o: Organization)org.json4s.JValue

scala> implicit def Person2Json(p:Person): JValue = JObject(JField("name", JString(p.name)), JField("organization", p.organization))
warning: there were 1 feature warning(s); re-run with -feature for details
Person2Json: (p: Person)org.json4s.JValue

scala> val o = Organization("A team")
o: Organization = Organization(A team)

scala> val p = Person("tak", o)
p: Person = Person(tak,Organization(A team))

scala> compact(render(p))
res0: String = {"name":"tak","organization":{"name":"A team"}}