From 98e26a2db23ebf0933d7cb5ce138069e41271fde Mon Sep 17 00:00:00 2001 From: Mathias Date: Thu, 17 Nov 2011 14:34:46 +0100 Subject: Improve DeserializationException error messages --- src/main/scala/cc/spray/json/BasicFormats.scala | 24 +++++++++++----------- .../scala/cc/spray/json/CollectionFormats.scala | 8 ++++---- src/main/scala/cc/spray/json/StandardFormats.scala | 21 ++++++++----------- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/main/scala/cc/spray/json/BasicFormats.scala b/src/main/scala/cc/spray/json/BasicFormats.scala index 32ae91e..ebb0b31 100644 --- a/src/main/scala/cc/spray/json/BasicFormats.scala +++ b/src/main/scala/cc/spray/json/BasicFormats.scala @@ -26,7 +26,7 @@ trait BasicFormats { def write(x: Int) = JsNumber(x) def read(value: JsValue) = value match { case JsNumber(x) => x.intValue - case _ => throw new DeserializationException("Int expected") + case x => throw new DeserializationException("Expected Int as JsNumber, but got " + x) } } @@ -34,7 +34,7 @@ trait BasicFormats { def write(x: Long) = JsNumber(x) def read(value: JsValue) = value match { case JsNumber(x) => x.longValue - case _ => throw new DeserializationException("Long expected") + case x => throw new DeserializationException("Expected Long as JsNumber, but got " + x) } } @@ -43,7 +43,7 @@ trait BasicFormats { def read(value: JsValue) = value match { case JsNumber(x) => x.floatValue case JsNull => Float.NaN - case _ => throw new DeserializationException("Float expected") + case x => throw new DeserializationException("Expected Float as JsNumber, but got " + x) } } @@ -52,7 +52,7 @@ trait BasicFormats { def read(value: JsValue) = value match { case JsNumber(x) => x.doubleValue case JsNull => Double.NaN - case _ => throw new DeserializationException("Double expected") + case x => throw new DeserializationException("Expected Double as JsNumber, but got " + x) } } @@ -60,7 +60,7 @@ trait BasicFormats { def write(x: Byte) = JsNumber(x) def read(value: JsValue) = value match { case JsNumber(x) => x.byteValue - case _ => throw new DeserializationException("Byte expected") + case x => throw new DeserializationException("Expected Byte as JsNumber, but got " + x) } } @@ -68,7 +68,7 @@ trait BasicFormats { def write(x: Short) = JsNumber(x) def read(value: JsValue) = value match { case JsNumber(x) => x.shortValue - case _ => throw new DeserializationException("Short expected") + case x => throw new DeserializationException("Expected Short as JsNumber, but got " + x) } } @@ -76,7 +76,7 @@ trait BasicFormats { def write(x: BigDecimal) = JsNumber(x) def read(value: JsValue) = value match { case JsNumber(x) => x - case _ => throw new DeserializationException("String expected") + case x => throw new DeserializationException("Expected BigDecimal as JsNumber, but got " + x) } } @@ -84,7 +84,7 @@ trait BasicFormats { def write(x: BigInt) = JsNumber(x) def read(value: JsValue) = value match { case JsNumber(x) => x.toBigInt - case _ => throw new DeserializationException("BigInt expected") + case x => throw new DeserializationException("Expected BigInt as JsNumber, but got " + x) } } @@ -98,7 +98,7 @@ trait BasicFormats { def read(value: JsValue) = value match { case JsTrue => true case JsFalse => false - case _ => throw new DeserializationException("Boolean expected") + case x => throw new DeserializationException("Expected JsBoolean, but got " + x) } } @@ -106,7 +106,7 @@ trait BasicFormats { def write(x: Char) = JsString(String.valueOf(x)) def read(value: JsValue) = value match { case JsString(x) if x.length == 1 => x.charAt(0) - case _ => throw new DeserializationException("Char expected") + case x => throw new DeserializationException("Expected Char as single-character JsString, but got " + x) } } @@ -114,7 +114,7 @@ trait BasicFormats { def write(x: String) = JsString(x) def read(value: JsValue) = value match { case JsString(x) => x - case _ => throw new DeserializationException("String expected") + case x => throw new DeserializationException("Expected String as JsString, but got " + x) } } @@ -122,7 +122,7 @@ trait BasicFormats { def write(x: Symbol) = JsString(x.name) def read(value: JsValue) = value match { case JsString(x) => Symbol(x) - case _ => throw new DeserializationException("Symbol expected") + case x => throw new DeserializationException("Expected Symbol as JsString, but got " + x) } } diff --git a/src/main/scala/cc/spray/json/CollectionFormats.scala b/src/main/scala/cc/spray/json/CollectionFormats.scala index bcfa198..5fdee83 100644 --- a/src/main/scala/cc/spray/json/CollectionFormats.scala +++ b/src/main/scala/cc/spray/json/CollectionFormats.scala @@ -26,7 +26,7 @@ trait CollectionFormats { def write(list: List[T]) = JsArray(list.map(_.toJson)) def read(value: JsValue) = value match { case JsArray(elements) => elements.map(_.convertTo[T]) - case _ => throw new DeserializationException("List expected") + case x => throw new DeserializationException("Expected List as JsArray, but got " + x) } } @@ -37,7 +37,7 @@ trait CollectionFormats { def write(array: Array[T]) = JsArray(array.map(_.toJson).toList) def read(value: JsValue) = value match { case JsArray(elements) => elements.map(_.convertTo[T]).toArray[T] - case _ => throw new DeserializationException("Array expected") + case x => throw new DeserializationException("Expected Array as JsArray, but got " + x) } } @@ -56,7 +56,7 @@ trait CollectionFormats { } def read(value: JsValue) = value match { case JsObject(fields) => fields.map(field => (JsString(field.name).convertTo[K], field.value.convertTo[V])).toMap - case _ => throw new DeserializationException("Map expected") + case x => throw new DeserializationException("Expected Map as JsObject, but got " + x) } } @@ -85,7 +85,7 @@ trait CollectionFormats { def write(iterable: I) = JsArray(iterable.map(_.toJson).toList) def read(value: JsValue) = value match { case JsArray(elements) => f(elements.map(_.convertTo[T])) - case _ => throw new DeserializationException("Collection expected") + case x => throw new DeserializationException("Expected Collection as JsArray, but got " + x) } } diff --git a/src/main/scala/cc/spray/json/StandardFormats.scala b/src/main/scala/cc/spray/json/StandardFormats.scala index 56fb658..6319d16 100644 --- a/src/main/scala/cc/spray/json/StandardFormats.scala +++ b/src/main/scala/cc/spray/json/StandardFormats.scala @@ -62,7 +62,7 @@ trait StandardFormats { def write(t: (A, B)) = JsArray(t._1.toJson, t._2.toJson) def read(value: JsValue) = value match { case JsArray(a :: b :: Nil) => (a.convertTo[A], b.convertTo[B]) - case _ => throw new DeserializationException("Tuple2 expected") + case x => throw new DeserializationException("Expected Tuple2 as JsArray, but got " + x) } } @@ -70,7 +70,7 @@ trait StandardFormats { def write(t: (A, B, C)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson) def read(value: JsValue) = value match { case JsArray(a :: b :: c :: Nil) => (a.convertTo[A], b.convertTo[B], c.convertTo[C]) - case _ => throw new DeserializationException("Tuple3 expected") + case x => throw new DeserializationException("Expected Tuple3 as JsArray, but got " + x) } } @@ -78,7 +78,7 @@ trait StandardFormats { def write(t: (A, B, C, D)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson, t._4.toJson) def read(value: JsValue) = value match { case JsArray(a :: b :: c :: d :: Nil) => (a.convertTo[A], b.convertTo[B], c.convertTo[C], d.convertTo[D]) - case _ => throw new DeserializationException("Tuple4 expected") + case x => throw new DeserializationException("Expected Tuple4 as JsArray, but got " + x) } } @@ -86,10 +86,9 @@ trait StandardFormats { new JF[(A, B, C, D, E)] { def write(t: (A, B, C, D, E)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson, t._4.toJson, t._5.toJson) def read(value: JsValue) = value match { - case JsArray(a :: b :: c :: d :: e :: Nil) => { + case JsArray(a :: b :: c :: d :: e :: Nil) => (a.convertTo[A], b.convertTo[B], c.convertTo[C], d.convertTo[D], e.convertTo[E]) - } - case _ => throw new DeserializationException("Tuple5 expected") + case x => throw new DeserializationException("Expected Tuple5 as JsArray, but got " + x) } } } @@ -98,10 +97,9 @@ trait StandardFormats { new JF[(A, B, C, D, E, F)] { def write(t: (A, B, C, D, E, F)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson, t._4.toJson, t._5.toJson, t._6.toJson) def read(value: JsValue) = value match { - case JsArray(a :: b :: c :: d :: e :: f :: Nil) => { + case JsArray(a :: b :: c :: d :: e :: f :: Nil) => (a.convertTo[A], b.convertTo[B], c.convertTo[C], d.convertTo[D], e.convertTo[E], f.convertTo[F]) - } - case _ => throw new DeserializationException("Tuple6 expected") + case x => throw new DeserializationException("Expected Tuple6 as JsArray, but got " + x) } } } @@ -110,10 +108,9 @@ trait StandardFormats { new JF[(A, B, C, D, E, F, G)] { def write(t: (A, B, C, D, E, F, G)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson, t._4.toJson, t._5.toJson, t._6.toJson, t._6.toJson) def read(value: JsValue) = value match { - case JsArray(a :: b :: c :: d :: e :: f :: g :: Nil) => { + case JsArray(a :: b :: c :: d :: e :: f :: g :: Nil) => (a.convertTo[A], b.convertTo[B], c.convertTo[C], d.convertTo[D], e.convertTo[E], f.convertTo[F], g.convertTo[G]) - } - case _ => throw new DeserializationException("Tuple7 expected") + case x => throw new DeserializationException("Expected Tuple7 as JsArray, but got " + x) } } } -- cgit v1.2.3