Play JSON Reads/Writes with single-parameter case classes
Like Travis said:
- Transforming an existing Reads: use the map method
- Transforming an existing Writes: use contramap
However, contramap only works on Writes that produce JsObject. Your writes will fail at runtime:
val w = JsPath.write[String].contramap[B](_.a)
scala> w.writes(B("Hello"))
java.lang.RuntimeException: when empty JsPath, expecting JsObject
You can create a Writes "from scratch" using Writes.apply
:
Writes[B](b => JsString(b.a))
Similarly you can create a Reads using Reads.apply.