Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ryanpbrewster/4cb0841b32a75861d2ef2e14f0b0cdb3 to your computer and use it in GitHub Desktop.

Select an option

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
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