Last active
August 17, 2016 15:56
-
-
Save mcveat/74568bfc664a1d5edebd42d952867347 to your computer and use it in GitHub Desktop.
This file contains 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 def eitherFormat[A, B](implicit af: Format[A], bf: Format[B]) = new Format[Either[A, B]] { | |
override def writes(o: Either[A, B]): JsValue = o match { | |
case Left(a) => af.writes(a) | |
case Right(b) => bf.writes(b) | |
} | |
override def reads(json: JsValue): JsResult[Either[A, B]] = | |
json.validate[B].map(Right.apply).orElse(json.validate[A].map(Left.apply)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment