In this answer, I give a brief explanation of why Writesit is not a functor, i.e. why, if we have a Writes[A]function and A => Bwe can not create the Writes[B]same as with Reads.
, Writes () , , , a Writes[A] B => A, a Writes[B].
Format Reads, Writes, , , - ( , Play).
, , , :
case class Foo(i: Int, s: String)
case class Bar(s: String, i: Int)
, Format Foo:
import play.api.libs.json._
import play.api.libs.functional.syntax._
implicit val fooFormat = Json.format[Foo]
- Bar - Foo. , Bar Foo, , , Format:
implicit val barFormat = fooFormat.inmap[Bar](
foo => Bar(foo.s, foo.i),
bar => Foo(bar.i, bar.s)
)
, Format , JsValue A, A JsValue. Format[A] Format[B], (.. A => B, B => A).