Created
July 26, 2010 18:54
-
-
Save debasishg/491035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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