Last active
May 6, 2016 20:45
-
-
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
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
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