Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryanpbrewster/d0a48f00d396c1a86e6029c0ad4f2b8d to your computer and use it in GitHub Desktop.
Save ryanpbrewster/d0a48f00d396c1a86e6029c0ad4f2b8d to your computer and use it in GitHub Desktop.
Complex Writes[Person] for Working with JSON in Play 2.1 by Greg Methvin
def idFormat[T]: Format[Id[T]] = Format(
__.read[Long].map(Id(_)),
new Writes[Id[T]]{ def writes(o: Id[T]) = JsNumber(o.id) }
)
implicit val personIdFormat = idFormat[Person]
implicit val nameFormat = Json.format[Name]
implicit val personFormat = Json.format[Person]
Json.toJson(Person(Some(Id(1)), Name("Arthur","Dent"), "[email protected]", new LocalDate("1952-03-11")))
// => {"id":1,"name":{"firstName":"Arthur","lastName":"Dent"},"email":"[email protected]","birthDate":"1952-03-11"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment