diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/scala/cc/spray/json/CollectionFormats.scala | 8 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/JsonFormat.scala | 6 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/ProductFormats.scala | 38 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/StandardFormats.scala | 12 |
4 files changed, 36 insertions, 28 deletions
diff --git a/src/main/scala/cc/spray/json/CollectionFormats.scala b/src/main/scala/cc/spray/json/CollectionFormats.scala index 5fdee83..a17c43f 100644 --- a/src/main/scala/cc/spray/json/CollectionFormats.scala +++ b/src/main/scala/cc/spray/json/CollectionFormats.scala @@ -22,7 +22,7 @@ trait CollectionFormats { /** * Supplies the JsonFormat for Lists. */ - implicit def listFormat[T :JsonFormat] = new JsonFormat[List[T]] { + implicit def listFormat[T :JsonFormat] = new RootJsonFormat[List[T]] { def write(list: List[T]) = JsArray(list.map(_.toJson)) def read(value: JsValue) = value match { case JsArray(elements) => elements.map(_.convertTo[T]) @@ -33,7 +33,7 @@ trait CollectionFormats { /** * Supplies the JsonFormat for Arrays. */ - implicit def arrayFormat[T :JsonFormat :ClassManifest] = new JsonFormat[Array[T]] { + implicit def arrayFormat[T :JsonFormat :ClassManifest] = new RootJsonFormat[Array[T]] { 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] @@ -45,7 +45,7 @@ trait CollectionFormats { * Supplies the JsonFormat for Maps. The implicitly available JsonFormat for the key type K must * always write JsStrings, otherwise a [[cc.spray.json.SerializationException]] will be thrown. */ - implicit def mapFormat[K :JsonFormat, V :JsonFormat] = new JsonFormat[Map[K, V]] { + implicit def mapFormat[K :JsonFormat, V :JsonFormat] = new RootJsonFormat[Map[K, V]] { def write(m: Map[K, V]) = JsObject { m.toList.map { t => t._1.toJson match { @@ -81,7 +81,7 @@ trait CollectionFormats { * A JsonFormat construction helper that creates a JsonFormat for an Iterable type I from a builder function * List => I. */ - def viaList[I <: Iterable[T], T :JsonFormat](f: List[T] => I): JsonFormat[I] = new JsonFormat[I] { + def viaList[I <: Iterable[T], T :JsonFormat](f: List[T] => I): RootJsonFormat[I] = new RootJsonFormat[I] { def write(iterable: I) = JsArray(iterable.map(_.toJson).toList) def read(value: JsValue) = value match { case JsArray(elements) => f(elements.map(_.convertTo[T])) diff --git a/src/main/scala/cc/spray/json/JsonFormat.scala b/src/main/scala/cc/spray/json/JsonFormat.scala index c5bc53e..01b9f80 100644 --- a/src/main/scala/cc/spray/json/JsonFormat.scala +++ b/src/main/scala/cc/spray/json/JsonFormat.scala @@ -51,3 +51,9 @@ object JsonWriter { * Provides the JSON deserialization and serialization for type T. */ trait JsonFormat[T] extends JsonReader[T] with JsonWriter[T] + +/** + * A special JsonFormat signaling that the format produces a legal JSON root object, i.e. either a JSON array + * or a JSON object. + */ +trait RootJsonFormat[T] extends JsonFormat[T]
\ 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 d0263c8..90c3349 100644 --- a/src/main/scala/cc/spray/json/ProductFormats.scala +++ b/src/main/scala/cc/spray/json/ProductFormats.scala @@ -26,7 +26,7 @@ import annotation.tailrec trait ProductFormats { this: StandardFormats => - def jsonFormat[A :JF, T <: Product](construct: A => T, a: String) = new JF[T]{ + def jsonFormat[A :JF, T <: Product](construct: A => T, a: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0) ) @@ -35,7 +35,7 @@ trait ProductFormats { ) } - def jsonFormat[A :JF, B :JF, T <: Product](construct: (A, B) => T, a: String, b: String) = new JF[T]{ + def jsonFormat[A :JF, B :JF, T <: Product](construct: (A, B) => T, a: String, b: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1)) @@ -47,7 +47,7 @@ trait ProductFormats { } def jsonFormat[A :JF, B :JF, C :JF, T <: Product](construct: (A, B, C) => T, - a: String, b: String, c: String) = new JF[T]{ + a: String, b: String, c: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -61,7 +61,7 @@ trait ProductFormats { } def jsonFormat[A :JF, B :JF, C :JF, D :JF, T <: Product](construct: (A, B, C, D) => T, - a: String, b: String, c: String, d: String) = new JF[T]{ + a: String, b: String, c: String, d: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -77,7 +77,7 @@ trait ProductFormats { } def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, T <: Product](construct: (A, B, C, D, E) => T, - a: String, b: String, c: String, d: String, e: String) = new JF[T]{ + a: String, b: String, c: String, d: String, e: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -95,7 +95,7 @@ trait ProductFormats { } def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, T <: Product](construct: (A, B, C, D, E, F) => T, - a: String, b: String, c: String, d: String, e: String, f: String) = new JF[T]{ + a: String, b: String, c: String, d: String, e: String, f: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -115,7 +115,7 @@ trait ProductFormats { } def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, T <: Product](construct: (A, B, C, D, E, F, G) => T, - a: String, b: String, c: String, d: String, e: String, f: String, g: String) = new JF[T]{ + a: String, b: String, c: String, d: String, e: String, f: String, g: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -138,7 +138,7 @@ trait ProductFormats { def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, T <: Product] (construct: (A, B, C, D, E, F, G, H) => T, - a: String, b: String, c: String, d: String, e: String, f: String, g: String, h: String) = new JF[T]{ + a: String, b: String, c: String, d: String, e: String, f: String, g: String, h: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -162,8 +162,8 @@ trait ProductFormats { } def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I) => T, - a: String, b: String, c: String, d: String, e: String, f: String, g: String, h: String, i: String) = new JF[T]{ + (construct: (A, B, C, D, E, F, G, H, I) => T, a: String, b: String, c: String, d: String, e: String, f: String, + g: String, h: String, i: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -190,7 +190,7 @@ trait ProductFormats { def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, T <: Product] (construct: (A, B, C, D, E, F, G, H, I, J) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String) = new JF[T]{ + f: String, g: String, h: String, i: String, j: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -219,7 +219,7 @@ trait ProductFormats { def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, T <: Product] (construct: (A, B, C, D, E, F, G, H, I, J, K) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String, k: String) = new JF[T]{ + f: String, g: String, h: String, i: String, j: String, k: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -250,7 +250,7 @@ trait ProductFormats { def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L :JF, T <: Product] (construct: (A, B, C, D, E, F, G, H, I, J, K, L) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String, k: String, l: String) = new JF[T]{ + f: String, g: String, h: String, i: String, j: String, k: String, l: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -283,7 +283,7 @@ trait ProductFormats { def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L :JF, M :JF, T <: Product] (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String) = new JF[T]{ + f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -317,8 +317,9 @@ trait ProductFormats { } def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L :JF, M :JF, N :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M, N) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String, n: String) = new JF[T]{ + (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M, N) => T, a: String, b: String, c: String, d: String, + e: String, f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String, + n: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, @@ -354,8 +355,9 @@ trait ProductFormats { } def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L :JF, M :JF, N :JF, O :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String, n: String, o: String) = new JF[T]{ + (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) => T, a: String, b: String, c: String, d: String, + e: String, f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String, n: String, + o: String) = new RootJsonFormat[T]{ def write(p: T) = JsObject( productElement2Field[A](a, p, 0, productElement2Field[B](b, p, 1, diff --git a/src/main/scala/cc/spray/json/StandardFormats.scala b/src/main/scala/cc/spray/json/StandardFormats.scala index 6319d16..277dfc9 100644 --- a/src/main/scala/cc/spray/json/StandardFormats.scala +++ b/src/main/scala/cc/spray/json/StandardFormats.scala @@ -58,7 +58,7 @@ trait StandardFormats { def read(value: JsValue) = Tuple1(value.convertTo[A]) } - implicit def tuple2Format[A :JF, B :JF] = new JF[(A, B)] { + implicit def tuple2Format[A :JF, B :JF] = new RootJsonFormat[(A, B)] { 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]) @@ -66,7 +66,7 @@ trait StandardFormats { } } - implicit def tuple3Format[A :JF, B :JF, C :JF] = new JF[(A, B, C)] { + implicit def tuple3Format[A :JF, B :JF, C :JF] = new RootJsonFormat[(A, B, C)] { 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]) @@ -74,7 +74,7 @@ trait StandardFormats { } } - implicit def tuple4Format[A :JF, B :JF, C :JF, D :JF] = new JF[(A, B, C, D)] { + implicit def tuple4Format[A :JF, B :JF, C :JF, D :JF] = new RootJsonFormat[(A, B, C, D)] { 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]) @@ -83,7 +83,7 @@ trait StandardFormats { } implicit def tuple5Format[A :JF, B :JF, C :JF, D :JF, E :JF] = { - new JF[(A, B, C, D, E)] { + new RootJsonFormat[(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) => @@ -94,7 +94,7 @@ trait StandardFormats { } implicit def tuple6Format[A :JF, B :JF, C :JF, D :JF, E :JF, F: JF] = { - new JF[(A, B, C, D, E, F)] { + new RootJsonFormat[(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) => @@ -105,7 +105,7 @@ trait StandardFormats { } implicit def tuple7Format[A :JF, B :JF, C :JF, D :JF, E :JF, F: JF, G: JF] = { - new JF[(A, B, C, D, E, F, G)] { + new RootJsonFormat[(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) => |