In play 2.1 reads are used to marshall Json to objects. But how can I do this when the case class has only one field. The ideom that works for more fields does not work, as
As Julien answered, you can read single field case classes using this:
case class Person(name: String)
val personReads: Reads[Person] =
(__ \ "name").read[String].map { name => Person(name) }
Just a complement, if you want to write:
val personWrites: Writes[Person] =
(__ \ "name").write[String].contramap { (person: Person) => person.name }
Or format (read and write):
val personFormat: Format[Person] =
(__ \ "name").format[String].inmap(name => Person(name), (person: Person) => person.name)
For write and format you have to import this:
import play.api.libs.functional.syntax._