summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMathias <mathias@spray.cc>2011-12-07 10:38:29 +0100
committerMathias <mathias@spray.cc>2011-12-07 10:38:29 +0100
commit6a5ccb2e6c37f7922db34f29c1f9cfd61a2397e8 (patch)
tree5b58cf9463bf300e92f6eedb5042f5e4c5b6e93a /src/main
parentfd80c9099d4d474a6080df11a5786bd7e9b5cb7f (diff)
downloadspray-json-6a5ccb2e6c37f7922db34f29c1f9cfd61a2397e8.tar.gz
spray-json-6a5ccb2e6c37f7922db34f29c1f9cfd61a2397e8.tar.bz2
spray-json-6a5ccb2e6c37f7922db34f29c1f9cfd61a2397e8.zip
Move exception into package.scala, simplify throwing of (De)SerializationExceptions
Diffstat (limited to 'src/main')
-rw-r--r--src/main/scala/cc/spray/json/BasicFormats.scala24
-rw-r--r--src/main/scala/cc/spray/json/CollectionFormats.scala13
-rw-r--r--src/main/scala/cc/spray/json/DeserializationException.scala19
-rw-r--r--src/main/scala/cc/spray/json/ProductFormats.scala6
-rw-r--r--src/main/scala/cc/spray/json/SerializationException.scala19
-rw-r--r--src/main/scala/cc/spray/json/StandardFormats.scala16
-rw-r--r--src/main/scala/cc/spray/json/package.scala11
7 files changed, 38 insertions, 70 deletions
diff --git a/src/main/scala/cc/spray/json/BasicFormats.scala b/src/main/scala/cc/spray/json/BasicFormats.scala
index ebb0b31..0389e3a 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 x => throw new DeserializationException("Expected Int as JsNumber, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Long as JsNumber, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Float as JsNumber, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Double as JsNumber, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Byte as JsNumber, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Short as JsNumber, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected BigDecimal as JsNumber, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected BigInt as JsNumber, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected JsBoolean, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Char as single-character JsString, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected String as JsString, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Symbol as JsString, but got " + x)
+ case x => deserializationError("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 c6c3403..93e41ab 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 x => throw new DeserializationException("Expected List as JsArray, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Array as JsArray, but got " + x)
+ case x => deserializationError("Expected Array as JsArray, but got " + x)
}
}
@@ -55,9 +55,10 @@ trait CollectionFormats {
}
}
def read(value: JsValue) = value match {
- case JsObject(fields) =>
- fields.map(field => (JsString(field._1).convertTo[K], field._2.convertTo[V])) (collection.breakOut)
- case x => throw new DeserializationException("Expected Map as JsObject, but got " + x)
+ case x: JsObject => x.fields.map { field =>
+ (JsString(field._1).convertTo[K], field._2.convertTo[V])
+ } (collection.breakOut)
+ case x => deserializationError("Expected Map as JsObject, but got " + x)
}
}
@@ -86,7 +87,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 x => throw new DeserializationException("Expected Collection as JsArray, but got " + x)
+ case x => deserializationError("Expected Collection as JsArray, but got " + x)
}
}
diff --git a/src/main/scala/cc/spray/json/DeserializationException.scala b/src/main/scala/cc/spray/json/DeserializationException.scala
deleted file mode 100644
index b317a14..0000000
--- a/src/main/scala/cc/spray/json/DeserializationException.scala
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (C) 2011 Mathias Doenitz
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package cc.spray.json
-
-class DeserializationException(msg: String, cause: Throwable = null) extends RuntimeException(msg, cause) \ No newline at end of file
diff --git a/src/main/scala/cc/spray/json/ProductFormats.scala b/src/main/scala/cc/spray/json/ProductFormats.scala
index d82cbf9..6a79f5f 100644
--- a/src/main/scala/cc/spray/json/ProductFormats.scala
+++ b/src/main/scala/cc/spray/json/ProductFormats.scala
@@ -16,8 +16,6 @@
package cc.spray.json
-import annotation.tailrec
-
/**
* Provides the helpers for constructing custom JsonFormat implementations for types implementing the Product trait
* (especially case classes)
@@ -416,9 +414,9 @@ trait ProductFormats {
catch {
case e: NoSuchElementException if !fieldFound =>
if (reader.isInstanceOf[OptionFormat[_]]) None.asInstanceOf[T]
- else throw new DeserializationException("Object is missing required member '" + fieldName + "'", e)
+ else deserializationError("Object is missing required member '" + fieldName + "'", e)
}
- case _ => throw new DeserializationException("Object expected")
+ case _ => deserializationError("Object expected")
}
}
}
diff --git a/src/main/scala/cc/spray/json/SerializationException.scala b/src/main/scala/cc/spray/json/SerializationException.scala
deleted file mode 100644
index f8cbc00..0000000
--- a/src/main/scala/cc/spray/json/SerializationException.scala
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (C) 2011 Mathias Doenitz
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package cc.spray.json
-
-class SerializationException(msg: String) extends RuntimeException(msg) \ No newline at end of file
diff --git a/src/main/scala/cc/spray/json/StandardFormats.scala b/src/main/scala/cc/spray/json/StandardFormats.scala
index 277dfc9..78bc539 100644
--- a/src/main/scala/cc/spray/json/StandardFormats.scala
+++ b/src/main/scala/cc/spray/json/StandardFormats.scala
@@ -48,8 +48,8 @@ trait StandardFormats {
def read(value: JsValue) = (value.convertTo(safeReader[A]), value.convertTo(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)
+ case (_: Right[_, _], _: Right[_, _]) => deserializationError("Ambiguous Either value: can be read as both, Left and Right, values")
+ case (Left(ea), Left(eb)) => deserializationError("Could not read Either value:\n" + ea + "---------- and ----------\n" + eb)
}
}
@@ -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 x => throw new DeserializationException("Expected Tuple2 as JsArray, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Tuple3 as JsArray, but got " + x)
+ case x => deserializationError("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 x => throw new DeserializationException("Expected Tuple4 as JsArray, but got " + x)
+ case x => deserializationError("Expected Tuple4 as JsArray, but got " + x)
}
}
@@ -88,7 +88,7 @@ trait StandardFormats {
def read(value: JsValue) = value match {
case JsArray(a :: b :: c :: d :: e :: Nil) =>
(a.convertTo[A], b.convertTo[B], c.convertTo[C], d.convertTo[D], e.convertTo[E])
- case x => throw new DeserializationException("Expected Tuple5 as JsArray, but got " + x)
+ case x => deserializationError("Expected Tuple5 as JsArray, but got " + x)
}
}
}
@@ -99,7 +99,7 @@ trait StandardFormats {
def read(value: JsValue) = value match {
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 x => throw new DeserializationException("Expected Tuple6 as JsArray, but got " + x)
+ case x => deserializationError("Expected Tuple6 as JsArray, but got " + x)
}
}
}
@@ -110,7 +110,7 @@ trait StandardFormats {
def read(value: JsValue) = value match {
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 x => throw new DeserializationException("Expected Tuple7 as JsArray, but got " + x)
+ case x => deserializationError("Expected Tuple7 as JsArray, but got " + x)
}
}
}
diff --git a/src/main/scala/cc/spray/json/package.scala b/src/main/scala/cc/spray/json/package.scala
index f080312..1bc592f 100644
--- a/src/main/scala/cc/spray/json/package.scala
+++ b/src/main/scala/cc/spray/json/package.scala
@@ -19,8 +19,11 @@ package cc.spray
package object json {
type JsField = (String, JsValue)
-
- def jsonReader[T](implicit reader: JsonReader[T]) = reader
+
+ def deserializationError(msg: String, cause: Throwable = null) = throw new DeserializationException(msg, cause)
+ def serializationError(msg: String) = throw new SerializationException(msg)
+
+ def jsonReader[T](implicit reader: JsonReader[T]) = reader
def jsonWriter[T](implicit writer: JsonWriter[T]) = writer
implicit def pimpAny[T](any: T): PimpedAny[T] = new PimpedAny(any)
@@ -28,6 +31,10 @@ package object json {
}
package json {
+
+ class DeserializationException(msg: String, cause: Throwable = null) extends RuntimeException(msg, cause)
+ class SerializationException(msg: String) extends RuntimeException(msg)
+
private[json] class PimpedAny[T](any: T) {
def toJson(implicit writer: JsonWriter[T]): JsValue = writer.write(any)
}