Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created July 26, 2010 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debasishg/491035 to your computer and use it in GitHub Desktop.
Save debasishg/491035 to your computer and use it in GitHub Desktop.
// a simple POJO
case class Address(street: String, city: String, zip: String)
object AddressProtocol extends DefaultProtocol {
implicit val AddressFormat: Format[Address] =
asProduct3("street", "city", "zip")(Address)(Address.unapply(_).get)
}
// aggregate containing list
case class Contact(name: String, addresses: List[Address])
import AddressProtocol._
object ContactProtocol extends DefaultProtocol {
implicit val ContactFormat: Format[Contact] =
asProduct2("name", "addresses")(Contact)(Contact.unapply(_).get)
}
// test case
import ContactProtocol._
// instantiate
val contact = Contact("Debasish Ghosh",
List(Address("monroe st", "denver", "80231"), Address("pine drive", "santa clara", "95054")))
// serialize & de-serialize
fromjson[Contact](tojson(contact)) should equal(contact)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment