From 7146a4af52a888f0cdf1a806cf640ef243e82a6c Mon Sep 17 00:00:00 2001 From: Mathias Date: Mon, 9 May 2011 09:57:13 +0200 Subject: Squash sub package 'formats' --- src/main/scala/cc/spray/json/BasicFormats.scala | 127 ++++++++++ .../scala/cc/spray/json/CollectionFormats.scala | 89 +++++++ .../scala/cc/spray/json/DefaultJsonProtocol.scala | 25 ++ src/main/scala/cc/spray/json/GenericFormats.scala | 262 ++++++++++++++++++++ src/main/scala/cc/spray/json/JsValue.scala | 1 - src/main/scala/cc/spray/json/JsonFormat.scala | 37 +++ src/main/scala/cc/spray/json/PimpedAny.scala | 25 -- src/main/scala/cc/spray/json/StandardFormats.scala | 103 ++++++++ .../scala/cc/spray/json/formats/BasicFormats.scala | 128 ---------- .../cc/spray/json/formats/CollectionFormats.scala | 90 ------- .../cc/spray/json/formats/DefaultJsonFormats.scala | 26 -- .../cc/spray/json/formats/GenericFormats.scala | 263 --------------------- .../scala/cc/spray/json/formats/JsonFormat.scala | 38 --- .../cc/spray/json/formats/StandardFormats.scala | 104 -------- src/main/scala/cc/spray/json/package.scala | 8 +- 15 files changed, 649 insertions(+), 677 deletions(-) create mode 100644 src/main/scala/cc/spray/json/BasicFormats.scala create mode 100644 src/main/scala/cc/spray/json/CollectionFormats.scala create mode 100644 src/main/scala/cc/spray/json/DefaultJsonProtocol.scala create mode 100644 src/main/scala/cc/spray/json/GenericFormats.scala create mode 100644 src/main/scala/cc/spray/json/JsonFormat.scala delete mode 100644 src/main/scala/cc/spray/json/PimpedAny.scala create mode 100644 src/main/scala/cc/spray/json/StandardFormats.scala delete mode 100644 src/main/scala/cc/spray/json/formats/BasicFormats.scala delete mode 100644 src/main/scala/cc/spray/json/formats/CollectionFormats.scala delete mode 100644 src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala delete mode 100644 src/main/scala/cc/spray/json/formats/GenericFormats.scala delete mode 100644 src/main/scala/cc/spray/json/formats/JsonFormat.scala delete mode 100644 src/main/scala/cc/spray/json/formats/StandardFormats.scala (limited to 'src/main') diff --git a/src/main/scala/cc/spray/json/BasicFormats.scala b/src/main/scala/cc/spray/json/BasicFormats.scala new file mode 100644 index 0000000..e16b4ca --- /dev/null +++ b/src/main/scala/cc/spray/json/BasicFormats.scala @@ -0,0 +1,127 @@ +/* + * Original implementation (C) 2009-2011 Debasish Ghosh + * Adapted and extended in 2011 by 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 + +/** + * Provides the JsonFormats for the most important Scala types. + */ +trait BasicFormats { + + implicit object IntJsonFormat extends JsonFormat[Int] { + def write(x: Int) = JsNumber(x) + def read(value: JsValue) = value match { + case JsNumber(x) => x.intValue + case _ => throw new DeserializationException("Int expected") + } + } + + implicit object LongJsonFormat extends JsonFormat[Long] { + def write(x: Long) = JsNumber(x) + def read(value: JsValue) = value match { + case JsNumber(x) => x.longValue + case _ => throw new DeserializationException("Long expected") + } + } + + implicit object FloatJsonFormat extends JsonFormat[Float] { + def write(x: Float) = JsNumber(x) + def read(value: JsValue) = value match { + case JsNumber(x) => x.floatValue + case _ => throw new DeserializationException("Float expected") + } + } + + implicit object DoubleJsonFormat extends JsonFormat[Double] { + def write(x: Double) = JsNumber(x) + def read(value: JsValue) = value match { + case JsNumber(x) => x.doubleValue + case _ => throw new DeserializationException("Double expected") + } + } + + implicit object ByteJsonFormat extends JsonFormat[Byte] { + def write(x: Byte) = JsNumber(x) + def read(value: JsValue) = value match { + case JsNumber(x) => x.byteValue + case _ => throw new DeserializationException("Byte expected") + } + } + + implicit object ShortJsonFormat extends JsonFormat[Short] { + def write(x: Short) = JsNumber(x) + def read(value: JsValue) = value match { + case JsNumber(x) => x.shortValue + case _ => throw new DeserializationException("Short expected") + } + } + + implicit object BigDecimalJsonFormat extends JsonFormat[BigDecimal] { + def write(x: BigDecimal) = JsNumber(x) + def read(value: JsValue) = value match { + case JsNumber(x) => x + case _ => throw new DeserializationException("String expected") + } + } + + implicit object BigIntJsonFormat extends JsonFormat[BigInt] { + def write(x: BigInt) = JsNumber(x) + def read(value: JsValue) = value match { + case JsNumber(x) => x.toBigInt + case _ => throw new DeserializationException("BigInt expected") + } + } + + implicit object UnitJsonFormat extends JsonFormat[Unit] { + def write(x: Unit) = JsNumber(1) + def read(value: JsValue) {} + } + + implicit object BooleanJsonFormat extends JsonFormat[Boolean] { + def write(x: Boolean) = JsBoolean(x) + def read(value: JsValue) = value match { + case JsTrue => true + case JsFalse => false + case _ => throw new DeserializationException("Boolean expected") + } + } + + implicit object CharJsonFormat extends JsonFormat[Char] { + 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") + } + } + + implicit object StringJsonFormat extends JsonFormat[String] { + def write(x: String) = JsString(x) + def read(value: JsValue) = value match { + case JsString(x) => x + case _ => throw new DeserializationException("String expected") + } + } + + implicit object SymbolJsonFormat extends JsonFormat[Symbol] { + def write(x: Symbol) = JsString(x.name) + def read(value: JsValue) = value match { + case JsString(x) => Symbol(x) + case _ => throw new DeserializationException("Symbol expected") + } + } + +} \ No newline at end of file diff --git a/src/main/scala/cc/spray/json/CollectionFormats.scala b/src/main/scala/cc/spray/json/CollectionFormats.scala new file mode 100644 index 0000000..2bce527 --- /dev/null +++ b/src/main/scala/cc/spray/json/CollectionFormats.scala @@ -0,0 +1,89 @@ +/* + * Original implementation (C) 2009-2011 Debasish Ghosh + * Adapted and extended in 2011 by 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 + +import reflect.Manifest + +trait CollectionFormats { + + /** + * Supplies the JsonFormat for Lists. + */ + implicit def listFormat[T :JsonFormat] = new JsonFormat[List[T]] { + def write(list: List[T]) = JsArray(list.map(_.toJson)) + def read(value: JsValue) = value match { + case JsArray(elements) => elements.map(_.fromJson) + case _ => throw new DeserializationException("List expected") + } + } + + /** + * Supplies the JsonFormat for Arrays. + */ + implicit def arrayFormat[T :JsonFormat :Manifest] = new JsonFormat[Array[T]] { + def write(array: Array[T]) = JsArray(array.map(_.toJson).toList) + def read(value: JsValue) = value match { + case JsArray(elements) => elements.map(_.fromJson[T]).toArray + case _ => throw new DeserializationException("Array expected") + } + } + + /** + * 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]] { + def write(m: Map[K, V]) = JsObject { + m.toList.map { t => + t._1.toJson match { + case JsString(x) => JsField(x, t._2.toJson) + case x => throw new SerializationException("Map key must be formatted as JsString, not '" + x + "'") + } + } + } + def read(value: JsValue) = value match { + case JsObject(fields) => fields.map(field => (JsString(field.name).fromJson[K], field.value.fromJson[V])).toMap + case _ => throw new DeserializationException("Map expected") + } + } + + /** + * Supplies the JsonFormat for immutable Sets. + */ + implicit def immutableSetFormat[T :JsonFormat] = viaList[Set[T], T](list => Set(list :_*)) + + import collection.mutable.Set + + /** + * Supplies the JsonFormat for mutable Sets. + */ + implicit def mutableSetFormat[T :JsonFormat] = viaList[Set[T], T](list => Set.empty ++ list) + + /** + * 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 write(iterable: I) = JsArray(iterable.map(_.toJson).toList) + def read(value: JsValue) = value match { + case JsArray(elements) => f(elements.map(_.fromJson[T])) + case _ => throw new DeserializationException("Collection expected") + } + } + +} \ No newline at end of file diff --git a/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala b/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala new file mode 100644 index 0000000..a8201c2 --- /dev/null +++ b/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala @@ -0,0 +1,25 @@ +/* + * Original implementation (C) 2009-2011 Debasish Ghosh + * Adapted and extended in 2011 by 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 + +/** + * Provides all the predefined JsonFormats. + */ +trait DefaultJsonProtocol extends BasicFormats with StandardFormats with CollectionFormats with GenericFormats + +object DefaultJsonProtocol extends DefaultJsonProtocol diff --git a/src/main/scala/cc/spray/json/GenericFormats.scala b/src/main/scala/cc/spray/json/GenericFormats.scala new file mode 100644 index 0000000..aa1da08 --- /dev/null +++ b/src/main/scala/cc/spray/json/GenericFormats.scala @@ -0,0 +1,262 @@ +/* + * Original implementation (C) 2009-2011 Debasish Ghosh + * Adapted and extended in 2011 by 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 + +/** + * Provides the helpers for constructing custom JsonFormat implementations. + */ +trait GenericFormats { + + private type JF[T] = JsonFormat[T] // simple alias for reduced verbosity + + /** + * Lazy wrapper around serialization. Useful when you want to serialize mutually recursive structures. + */ + def lazyFormat[T](format: => JF[T]) = new JF[T]{ + lazy val delegate = format; + def write(x: T) = delegate.write(x); + def read(value: JsValue) = delegate.read(value); + } + + def jsonFormat[A :JF, B :JF, T <: Product](construct: (A, B) => T, a: String, b: String) = new JF[T]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B] + ) + } + + def jsonFormat[A :JF, B :JF, C :JF, T <: Product](construct: (A, B, C) => T, + a: String, b: String, c: String) = new JF[T]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson), + JsField(c, element[C](p, 2).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B], + field(value, c).fromJson[C] + ) + } + + 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]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson), + JsField(c, element[C](p, 2).toJson), + JsField(d, element[D](p, 3).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B], + field(value, c).fromJson[C], + field(value, d).fromJson[D] + ) + } + + 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]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson), + JsField(c, element[C](p, 2).toJson), + JsField(d, element[D](p, 3).toJson), + JsField(e, element[E](p, 4).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B], + field(value, c).fromJson[C], + field(value, d).fromJson[D], + field(value, e).fromJson[E] + ) + } + + 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]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson), + JsField(c, element[C](p, 2).toJson), + JsField(d, element[D](p, 3).toJson), + JsField(e, element[E](p, 4).toJson), + JsField(f, element[F](p, 5).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B], + field(value, c).fromJson[C], + field(value, d).fromJson[D], + field(value, e).fromJson[E], + field(value, f).fromJson[F] + ) + } + + 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]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson), + JsField(c, element[C](p, 2).toJson), + JsField(d, element[D](p, 3).toJson), + JsField(e, element[E](p, 4).toJson), + JsField(f, element[F](p, 5).toJson), + JsField(g, element[G](p, 6).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B], + field(value, c).fromJson[C], + field(value, d).fromJson[D], + field(value, e).fromJson[E], + field(value, f).fromJson[F], + field(value, g).fromJson[G] + ) + } + + 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]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson), + JsField(c, element[C](p, 2).toJson), + JsField(d, element[D](p, 3).toJson), + JsField(e, element[E](p, 4).toJson), + JsField(f, element[F](p, 5).toJson), + JsField(g, element[G](p, 6).toJson), + JsField(h, element[H](p, 7).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B], + field(value, c).fromJson[C], + field(value, d).fromJson[D], + field(value, e).fromJson[E], + field(value, f).fromJson[F], + field(value, g).fromJson[G], + field(value, h).fromJson[H] + ) + } + + 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]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson), + JsField(c, element[C](p, 2).toJson), + JsField(d, element[D](p, 3).toJson), + JsField(e, element[E](p, 4).toJson), + JsField(f, element[F](p, 5).toJson), + JsField(g, element[G](p, 6).toJson), + JsField(h, element[H](p, 7).toJson), + JsField(i, element[I](p, 8).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B], + field(value, c).fromJson[C], + field(value, d).fromJson[D], + field(value, e).fromJson[E], + field(value, f).fromJson[F], + field(value, g).fromJson[G], + field(value, h).fromJson[H], + field(value, i).fromJson[I] + ) + } + + 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]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson), + JsField(c, element[C](p, 2).toJson), + JsField(d, element[D](p, 3).toJson), + JsField(e, element[E](p, 4).toJson), + JsField(f, element[F](p, 5).toJson), + JsField(g, element[G](p, 6).toJson), + JsField(h, element[H](p, 7).toJson), + JsField(i, element[I](p, 8).toJson), + JsField(j, element[J](p, 9).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B], + field(value, c).fromJson[C], + field(value, d).fromJson[D], + field(value, e).fromJson[E], + field(value, f).fromJson[F], + field(value, g).fromJson[G], + field(value, h).fromJson[H], + field(value, i).fromJson[I], + field(value, j).fromJson[J] + ) + } + + 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]{ + def write(p: T) = JsObject( + JsField(a, element[A](p, 0).toJson), + JsField(b, element[B](p, 1).toJson), + JsField(c, element[C](p, 2).toJson), + JsField(d, element[D](p, 3).toJson), + JsField(e, element[E](p, 4).toJson), + JsField(f, element[F](p, 5).toJson), + JsField(g, element[G](p, 6).toJson), + JsField(h, element[H](p, 7).toJson), + JsField(i, element[I](p, 8).toJson), + JsField(j, element[J](p, 9).toJson), + JsField(k, element[K](p, 10).toJson) + ) + def read(value: JsValue) = construct( + field(value, a).fromJson[A], + field(value, b).fromJson[B], + field(value, c).fromJson[C], + field(value, d).fromJson[D], + field(value, e).fromJson[E], + field(value, f).fromJson[F], + field(value, g).fromJson[G], + field(value, h).fromJson[H], + field(value, i).fromJson[I], + field(value, j).fromJson[J], + field(value, k).fromJson[K] + ) + } + + // helpers + + private def element[T](p: Product, ix: Int) = p.productElement(ix).asInstanceOf[T] + + private def field(value: JsValue, fieldName: String) = value match { + case jso: JsObject => { + jso.fields + .find(_.name == fieldName) + .getOrElse(throw new DeserializationException("Object is missing required member '" + fieldName + "'")) + .value + } + case _ => throw new DeserializationException("Object expected") + } +} diff --git a/src/main/scala/cc/spray/json/JsValue.scala b/src/main/scala/cc/spray/json/JsValue.scala index f98103c..18e302e 100644 --- a/src/main/scala/cc/spray/json/JsValue.scala +++ b/src/main/scala/cc/spray/json/JsValue.scala @@ -18,7 +18,6 @@ package cc.spray.json -import formats._ import collection.mutable.ListBuffer /** diff --git a/src/main/scala/cc/spray/json/JsonFormat.scala b/src/main/scala/cc/spray/json/JsonFormat.scala new file mode 100644 index 0000000..94a2576 --- /dev/null +++ b/src/main/scala/cc/spray/json/JsonFormat.scala @@ -0,0 +1,37 @@ +/* + * Original implementation (C) 2009-2011 Debasish Ghosh + * Adapted and extended in 2011 by 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 + +/** + * Provides the JSON deserialization for type T. + */ +trait JsonReader[T] { + def read(json: JsValue): T +} + +/** + * Provides the JSON serialization for type T. + */ +trait JsonWriter[T] { + def write(obj: T): JsValue +} + +/** + * Provides the JSON deserialization and serialization for type T. + */ +trait JsonFormat[T] extends JsonReader[T] with JsonWriter[T] diff --git a/src/main/scala/cc/spray/json/PimpedAny.scala b/src/main/scala/cc/spray/json/PimpedAny.scala deleted file mode 100644 index 4c10771..0000000 --- a/src/main/scala/cc/spray/json/PimpedAny.scala +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2009-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 - -import formats.JsonWriter - -private[json] class PimpedAny[T](any: T, writer: JsonWriter[T]) { - - def toJson: JsValue = writer.write(any) - -} \ 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 new file mode 100644 index 0000000..2a07d6c --- /dev/null +++ b/src/main/scala/cc/spray/json/StandardFormats.scala @@ -0,0 +1,103 @@ +/* + * Original implementation (C) 2009-2011 Debasish Ghosh + * Adapted and extended in 2011 by 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 + +/** + * Provides the JsonFormats for the non-collection standard types. + */ +trait StandardFormats { + + private type JF[T] = JsonFormat[T] // simple alias for reduced verbosity + + implicit def optionFormat[T :JF] = new JF[Option[T]] { + def write(option: Option[T]) = option match { + case Some(x) => x.toJson + case None => JsNull + } + def read(value: JsValue) = value match { + case JsNull => None + case x => Some(x.fromJson) + } + } + + implicit def tuple1Format[A :JF] = new JF[Tuple1[A]] { + def write(t: Tuple1[A]) = t._1.toJson + def read(value: JsValue) = Tuple1(value.fromJson[A]) + } + + implicit def tuple2Format[A :JF, B :JF] = new JF[(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.fromJson[A], b.fromJson[B]) + case _ => throw new DeserializationException("Tuple2 expected") + } + } + + implicit def tuple3Format[A :JF, B :JF, C :JF] = new JF[(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.fromJson[A], b.fromJson[B], c.fromJson[C]) + case _ => throw new DeserializationException("Tuple3 expected") + } + } + + implicit def tuple4Format[A :JF, B :JF, C :JF, D :JF] = new JF[(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.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D]) + case _ => throw new DeserializationException("Tuple4 expected") + } + } + + implicit def tuple5Format[A :JF, B :JF, C :JF, D :JF, E :JF] = { + 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) => { + (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D], e.fromJson[E]) + } + case _ => throw new DeserializationException("Tuple5 expected") + } + } + } + + implicit def tuple6Format[A :JF, B :JF, C :JF, D :JF, E :JF, F: JF] = { + 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) => { + (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D], e.fromJson[E], f.fromJson[F]) + } + case _ => throw new DeserializationException("Tuple6 expected") + } + } + } + + 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)] { + 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) => { + (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D], e.fromJson[E], f.fromJson[F], g.fromJson[G]) + } + case _ => throw new DeserializationException("Tuple7 expected") + } + } + } + +} \ No newline at end of file diff --git a/src/main/scala/cc/spray/json/formats/BasicFormats.scala b/src/main/scala/cc/spray/json/formats/BasicFormats.scala deleted file mode 100644 index 4be7ef6..0000000 --- a/src/main/scala/cc/spray/json/formats/BasicFormats.scala +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Original implementation (C) 2009-2011 Debasish Ghosh - * Adapted and extended in 2011 by 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 -package formats - -/** - * Provides the JsonFormats for the most important Scala types. - */ -trait BasicFormats { - - implicit object IntJsonFormat extends JsonFormat[Int] { - def write(x: Int) = JsNumber(x) - def read(value: JsValue) = value match { - case JsNumber(x) => x.intValue - case _ => throw new DeserializationException("Int expected") - } - } - - implicit object LongJsonFormat extends JsonFormat[Long] { - def write(x: Long) = JsNumber(x) - def read(value: JsValue) = value match { - case JsNumber(x) => x.longValue - case _ => throw new DeserializationException("Long expected") - } - } - - implicit object FloatJsonFormat extends JsonFormat[Float] { - def write(x: Float) = JsNumber(x) - def read(value: JsValue) = value match { - case JsNumber(x) => x.floatValue - case _ => throw new DeserializationException("Float expected") - } - } - - implicit object DoubleJsonFormat extends JsonFormat[Double] { - def write(x: Double) = JsNumber(x) - def read(value: JsValue) = value match { - case JsNumber(x) => x.doubleValue - case _ => throw new DeserializationException("Double expected") - } - } - - implicit object ByteJsonFormat extends JsonFormat[Byte] { - def write(x: Byte) = JsNumber(x) - def read(value: JsValue) = value match { - case JsNumber(x) => x.byteValue - case _ => throw new DeserializationException("Byte expected") - } - } - - implicit object ShortJsonFormat extends JsonFormat[Short] { - def write(x: Short) = JsNumber(x) - def read(value: JsValue) = value match { - case JsNumber(x) => x.shortValue - case _ => throw new DeserializationException("Short expected") - } - } - - implicit object BigDecimalJsonFormat extends JsonFormat[BigDecimal] { - def write(x: BigDecimal) = JsNumber(x) - def read(value: JsValue) = value match { - case JsNumber(x) => x - case _ => throw new DeserializationException("String expected") - } - } - - implicit object BigIntJsonFormat extends JsonFormat[BigInt] { - def write(x: BigInt) = JsNumber(x) - def read(value: JsValue) = value match { - case JsNumber(x) => x.toBigInt - case _ => throw new DeserializationException("BigInt expected") - } - } - - implicit object UnitJsonFormat extends JsonFormat[Unit] { - def write(x: Unit) = JsNumber(1) - def read(value: JsValue) {} - } - - implicit object BooleanJsonFormat extends JsonFormat[Boolean] { - def write(x: Boolean) = JsBoolean(x) - def read(value: JsValue) = value match { - case JsTrue => true - case JsFalse => false - case _ => throw new DeserializationException("Boolean expected") - } - } - - implicit object CharJsonFormat extends JsonFormat[Char] { - 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") - } - } - - implicit object StringJsonFormat extends JsonFormat[String] { - def write(x: String) = JsString(x) - def read(value: JsValue) = value match { - case JsString(x) => x - case _ => throw new DeserializationException("String expected") - } - } - - implicit object SymbolJsonFormat extends JsonFormat[Symbol] { - def write(x: Symbol) = JsString(x.name) - def read(value: JsValue) = value match { - case JsString(x) => Symbol(x) - case _ => throw new DeserializationException("Symbol expected") - } - } - -} \ No newline at end of file diff --git a/src/main/scala/cc/spray/json/formats/CollectionFormats.scala b/src/main/scala/cc/spray/json/formats/CollectionFormats.scala deleted file mode 100644 index 02a9999..0000000 --- a/src/main/scala/cc/spray/json/formats/CollectionFormats.scala +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Original implementation (C) 2009-2011 Debasish Ghosh - * Adapted and extended in 2011 by 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 -package formats - -import reflect.Manifest - -trait CollectionFormats { - - /** - * Supplies the JsonFormat for Lists. - */ - implicit def listFormat[T :JsonFormat] = new JsonFormat[List[T]] { - def write(list: List[T]) = JsArray(list.map(_.toJson)) - def read(value: JsValue) = value match { - case JsArray(elements) => elements.map(_.fromJson) - case _ => throw new DeserializationException("List expected") - } - } - - /** - * Supplies the JsonFormat for Arrays. - */ - implicit def arrayFormat[T :JsonFormat :Manifest] = new JsonFormat[Array[T]] { - def write(array: Array[T]) = JsArray(array.map(_.toJson).toList) - def read(value: JsValue) = value match { - case JsArray(elements) => elements.map(_.fromJson[T]).toArray - case _ => throw new DeserializationException("Array expected") - } - } - - /** - * 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]] { - def write(m: Map[K, V]) = JsObject { - m.toList.map { t => - t._1.toJson match { - case JsString(x) => JsField(x, t._2.toJson) - case x => throw new SerializationException("Map key must be formatted as JsString, not '" + x + "'") - } - } - } - def read(value: JsValue) = value match { - case JsObject(fields) => fields.map(field => (JsString(field.name).fromJson[K], field.value.fromJson[V])).toMap - case _ => throw new DeserializationException("Map expected") - } - } - - /** - * Supplies the JsonFormat for immutable Sets. - */ - implicit def immutableSetFormat[T :JsonFormat] = viaList[Set[T], T](list => Set(list :_*)) - - import collection.mutable.Set - - /** - * Supplies the JsonFormat for mutable Sets. - */ - implicit def mutableSetFormat[T :JsonFormat] = viaList[Set[T], T](list => Set.empty ++ list) - - /** - * 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 write(iterable: I) = JsArray(iterable.map(_.toJson).toList) - def read(value: JsValue) = value match { - case JsArray(elements) => f(elements.map(_.fromJson[T])) - case _ => throw new DeserializationException("Collection expected") - } - } - -} \ No newline at end of file diff --git a/src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala b/src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala deleted file mode 100644 index 15f2abb..0000000 --- a/src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Original implementation (C) 2009-2011 Debasish Ghosh - * Adapted and extended in 2011 by 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 -package formats - -/** - * Provides all the predefined JsonFormats. - */ -trait DefaultJsonFormats extends BasicFormats with StandardFormats with CollectionFormats with GenericFormats - -object DefaultJsonFormats extends DefaultJsonFormats diff --git a/src/main/scala/cc/spray/json/formats/GenericFormats.scala b/src/main/scala/cc/spray/json/formats/GenericFormats.scala deleted file mode 100644 index dbfb264..0000000 --- a/src/main/scala/cc/spray/json/formats/GenericFormats.scala +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Original implementation (C) 2009-2011 Debasish Ghosh - * Adapted and extended in 2011 by 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 -package formats - -/** - * Provides the helpers for constructing custom JsonFormat implementations. - */ -trait GenericFormats { - - private type JF[T] = JsonFormat[T] // simple alias for reduced verbosity - - /** - * Lazy wrapper around serialization. Useful when you want to serialize mutually recursive structures. - */ - def lazyFormat[T](format: => JF[T]) = new JF[T]{ - lazy val delegate = format; - def write(x: T) = delegate.write(x); - def read(value: JsValue) = delegate.read(value); - } - - def format[A :JF, B :JF, T <: Product](construct: (A, B) => T, a: String, b: String) = new JF[T]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B] - ) - } - - def format[A :JF, B :JF, C :JF, T <: Product](construct: (A, B, C) => T, - a: String, b: String, c: String) = new JF[T]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson), - JsField(c, element[C](p, 2).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B], - field(value, c).fromJson[C] - ) - } - - def format[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]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson), - JsField(c, element[C](p, 2).toJson), - JsField(d, element[D](p, 3).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B], - field(value, c).fromJson[C], - field(value, d).fromJson[D] - ) - } - - def format[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]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson), - JsField(c, element[C](p, 2).toJson), - JsField(d, element[D](p, 3).toJson), - JsField(e, element[E](p, 4).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B], - field(value, c).fromJson[C], - field(value, d).fromJson[D], - field(value, e).fromJson[E] - ) - } - - def format[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]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson), - JsField(c, element[C](p, 2).toJson), - JsField(d, element[D](p, 3).toJson), - JsField(e, element[E](p, 4).toJson), - JsField(f, element[F](p, 5).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B], - field(value, c).fromJson[C], - field(value, d).fromJson[D], - field(value, e).fromJson[E], - field(value, f).fromJson[F] - ) - } - - def format[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]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson), - JsField(c, element[C](p, 2).toJson), - JsField(d, element[D](p, 3).toJson), - JsField(e, element[E](p, 4).toJson), - JsField(f, element[F](p, 5).toJson), - JsField(g, element[G](p, 6).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B], - field(value, c).fromJson[C], - field(value, d).fromJson[D], - field(value, e).fromJson[E], - field(value, f).fromJson[F], - field(value, g).fromJson[G] - ) - } - - def format[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]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson), - JsField(c, element[C](p, 2).toJson), - JsField(d, element[D](p, 3).toJson), - JsField(e, element[E](p, 4).toJson), - JsField(f, element[F](p, 5).toJson), - JsField(g, element[G](p, 6).toJson), - JsField(h, element[H](p, 7).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B], - field(value, c).fromJson[C], - field(value, d).fromJson[D], - field(value, e).fromJson[E], - field(value, f).fromJson[F], - field(value, g).fromJson[G], - field(value, h).fromJson[H] - ) - } - - def format[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]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson), - JsField(c, element[C](p, 2).toJson), - JsField(d, element[D](p, 3).toJson), - JsField(e, element[E](p, 4).toJson), - JsField(f, element[F](p, 5).toJson), - JsField(g, element[G](p, 6).toJson), - JsField(h, element[H](p, 7).toJson), - JsField(i, element[I](p, 8).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B], - field(value, c).fromJson[C], - field(value, d).fromJson[D], - field(value, e).fromJson[E], - field(value, f).fromJson[F], - field(value, g).fromJson[G], - field(value, h).fromJson[H], - field(value, i).fromJson[I] - ) - } - - def format[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]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson), - JsField(c, element[C](p, 2).toJson), - JsField(d, element[D](p, 3).toJson), - JsField(e, element[E](p, 4).toJson), - JsField(f, element[F](p, 5).toJson), - JsField(g, element[G](p, 6).toJson), - JsField(h, element[H](p, 7).toJson), - JsField(i, element[I](p, 8).toJson), - JsField(j, element[J](p, 9).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B], - field(value, c).fromJson[C], - field(value, d).fromJson[D], - field(value, e).fromJson[E], - field(value, f).fromJson[F], - field(value, g).fromJson[G], - field(value, h).fromJson[H], - field(value, i).fromJson[I], - field(value, j).fromJson[J] - ) - } - - def format[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]{ - def write(p: T) = JsObject( - JsField(a, element[A](p, 0).toJson), - JsField(b, element[B](p, 1).toJson), - JsField(c, element[C](p, 2).toJson), - JsField(d, element[D](p, 3).toJson), - JsField(e, element[E](p, 4).toJson), - JsField(f, element[F](p, 5).toJson), - JsField(g, element[G](p, 6).toJson), - JsField(h, element[H](p, 7).toJson), - JsField(i, element[I](p, 8).toJson), - JsField(j, element[J](p, 9).toJson), - JsField(k, element[K](p, 10).toJson) - ) - def read(value: JsValue) = construct( - field(value, a).fromJson[A], - field(value, b).fromJson[B], - field(value, c).fromJson[C], - field(value, d).fromJson[D], - field(value, e).fromJson[E], - field(value, f).fromJson[F], - field(value, g).fromJson[G], - field(value, h).fromJson[H], - field(value, i).fromJson[I], - field(value, j).fromJson[J], - field(value, k).fromJson[K] - ) - } - - // helpers - - private def element[T](p: Product, ix: Int) = p.productElement(ix).asInstanceOf[T] - - private def field(value: JsValue, fieldName: String) = value match { - case jso: JsObject => { - jso.fields - .find(_.name == fieldName) - .getOrElse(throw new DeserializationException("Object is missing required member '" + fieldName + "'")) - .value - } - case _ => throw new DeserializationException("Object expected") - } -} diff --git a/src/main/scala/cc/spray/json/formats/JsonFormat.scala b/src/main/scala/cc/spray/json/formats/JsonFormat.scala deleted file mode 100644 index 0951ef8..0000000 --- a/src/main/scala/cc/spray/json/formats/JsonFormat.scala +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Original implementation (C) 2009-2011 Debasish Ghosh - * Adapted and extended in 2011 by 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 -package formats - -/** - * Provides the JSON deserialization for type T. - */ -trait JsonReader[T] { - def read(json: JsValue): T -} - -/** - * Provides the JSON serialization for type T. - */ -trait JsonWriter[T] { - def write(obj: T): JsValue -} - -/** - * Provides the JSON deserialization and serialization for type T. - */ -trait JsonFormat[T] extends JsonReader[T] with JsonWriter[T] diff --git a/src/main/scala/cc/spray/json/formats/StandardFormats.scala b/src/main/scala/cc/spray/json/formats/StandardFormats.scala deleted file mode 100644 index 78bbe53..0000000 --- a/src/main/scala/cc/spray/json/formats/StandardFormats.scala +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Original implementation (C) 2009-2011 Debasish Ghosh - * Adapted and extended in 2011 by 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 -package formats - -/** - * Provides the JsonFormats for the non-collection standard types. - */ -trait StandardFormats { - - private type JF[T] = JsonFormat[T] // simple alias for reduced verbosity - - implicit def optionFormat[T :JF] = new JF[Option[T]] { - def write(option: Option[T]) = option match { - case Some(x) => x.toJson - case None => JsNull - } - def read(value: JsValue) = value match { - case JsNull => None - case x => Some(x.fromJson) - } - } - - implicit def tuple1Format[A :JF] = new JF[Tuple1[A]] { - def write(t: Tuple1[A]) = t._1.toJson - def read(value: JsValue) = Tuple1(value.fromJson[A]) - } - - implicit def tuple2Format[A :JF, B :JF] = new JF[(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.fromJson[A], b.fromJson[B]) - case _ => throw new DeserializationException("Tuple2 expected") - } - } - - implicit def tuple3Format[A :JF, B :JF, C :JF] = new JF[(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.fromJson[A], b.fromJson[B], c.fromJson[C]) - case _ => throw new DeserializationException("Tuple3 expected") - } - } - - implicit def tuple4Format[A :JF, B :JF, C :JF, D :JF] = new JF[(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.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D]) - case _ => throw new DeserializationException("Tuple4 expected") - } - } - - implicit def tuple5Format[A :JF, B :JF, C :JF, D :JF, E :JF] = { - 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) => { - (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D], e.fromJson[E]) - } - case _ => throw new DeserializationException("Tuple5 expected") - } - } - } - - implicit def tuple6Format[A :JF, B :JF, C :JF, D :JF, E :JF, F: JF] = { - 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) => { - (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D], e.fromJson[E], f.fromJson[F]) - } - case _ => throw new DeserializationException("Tuple6 expected") - } - } - } - - 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)] { - 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) => { - (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D], e.fromJson[E], f.fromJson[F], g.fromJson[G]) - } - case _ => throw new DeserializationException("Tuple7 expected") - } - } - } - -} \ No newline at end of file diff --git a/src/main/scala/cc/spray/json/package.scala b/src/main/scala/cc/spray/json/package.scala index 362fb7b..a5daedb 100644 --- a/src/main/scala/cc/spray/json/package.scala +++ b/src/main/scala/cc/spray/json/package.scala @@ -16,8 +16,6 @@ package cc.spray -import json.formats.{JsonReader, JsonWriter} - package object json { def jsonReader[T](implicit reader: JsonReader[T]) = reader @@ -25,4 +23,10 @@ package object json { implicit def pimpAny[T :JsonWriter](any: T): PimpedAny[T] = new PimpedAny(any, jsonWriter) +} + +package json { + private[json] class PimpedAny[T](any: T, writer: JsonWriter[T]) { + def toJson: JsValue = writer.write(any) + } } \ No newline at end of file -- cgit v1.2.3