From 03e9e1a6d50f6afb7848033ad4ca1f5239943a2f Mon Sep 17 00:00:00 2001 From: Mathias Date: Mon, 23 May 2011 14:12:53 +0200 Subject: Add predefined JsonFormat for Either type --- src/main/scala/cc/spray/json/StandardFormats.scala | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/main') diff --git a/src/main/scala/cc/spray/json/StandardFormats.scala b/src/main/scala/cc/spray/json/StandardFormats.scala index 2a07d6c..20012b4 100644 --- a/src/main/scala/cc/spray/json/StandardFormats.scala +++ b/src/main/scala/cc/spray/json/StandardFormats.scala @@ -17,12 +17,24 @@ package cc.spray.json +import scala.{Left, Right} + /** * Provides the JsonFormats for the non-collection standard types. */ trait StandardFormats { private type JF[T] = JsonFormat[T] // simple alias for reduced verbosity + + def safeReader[A :JsonFormat] = new JsonReader[Either[Exception, A]] { + def read(json: JsValue) = { + try { + Right(json.fromJson) + } catch { + case e: Exception => Left(e) + } + } + } implicit def optionFormat[T :JF] = new JF[Option[T]] { def write(option: Option[T]) = option match { @@ -34,6 +46,19 @@ trait StandardFormats { case x => Some(x.fromJson) } } + + implicit def eitherFormat[A :JF, B :JF] = new JF[Either[A, B]] { + def write(either: Either[A, B]) = either match { + case Right(a) => a.toJson + case Left(b) => b.toJson + } + def read(value: JsValue) = (value.fromJson(safeReader[A]), value.fromJson(safeReader[B])) match { + case (Right(a), _: Left[_, _]) => Left(a) + case (_: Left[_, _], Right(b)) => Right(b) + case (_: Right[_, _], _: Right[_, _]) => throw new DeserializationException("Ambiguous Either value: can be read as both, Left and Right, values") + case (Left(ea), Left(eb)) => throw new DeserializationException("Could not read Either value:\n" + ea + "---------- and ----------\n" + eb) + } + } implicit def tuple1Format[A :JF] = new JF[Tuple1[A]] { def write(t: Tuple1[A]) = t._1.toJson -- cgit v1.2.3