Last active
May 2, 2016 21:17
-
-
Save ryanpbrewster/4cb0841b32a75861d2ef2e14f0b0cdb3 to your computer and use it in GitHub Desktop.
Format[Person] using defaults for Working with JSON in Play 2.1 by Greg Methvin.scala
This file contains hidden or 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
| implicit val personFormat: Format[Person] = ( | |
| (__ "id").formatNullable[Id[Person]] and | |
| (__ "name").format[Name] and | |
| (__ "email").formatNullable( | |
| Format(Reads.email, Writes.StringWrites)) | |
| .inmap[String](_.getOrElse(""), Some(_).filterNot(_.isEmpty) | |
| ) and | |
| (__ "birthDate").format[LocalDate] | |
| )(Person.apply, unlift(Person.unapply)) | |
| Json.fromJson[Person](Json.parse("""{ | |
| "id":0, | |
| "name":{"firstName":"Arthur","lastName":"Dent"}, | |
| "birthDate":"1952-03-11" | |
| }""")) | |
| // => JsSuccess(Person(Some(0),Name(Arthur,Dent),,1952-03-11),) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment