Skip to content

Instantly share code, notes, and snippets.

@mcveat
Last active August 17, 2016 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcveat/74568bfc664a1d5edebd42d952867347 to your computer and use it in GitHub Desktop.
Save mcveat/74568bfc664a1d5edebd42d952867347 to your computer and use it in GitHub Desktop.
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