summaryrefslogtreecommitdiff
path: root/src/main/scala/cc/spray/json/formats
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/cc/spray/json/formats')
-rw-r--r--src/main/scala/cc/spray/json/formats/BasicFormats.scala9
-rw-r--r--src/main/scala/cc/spray/json/formats/CollectionFormats.scala29
-rw-r--r--src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala9
-rw-r--r--src/main/scala/cc/spray/json/formats/GenericFormats.scala9
-rw-r--r--src/main/scala/cc/spray/json/formats/JsonFormat.scala15
-rw-r--r--src/main/scala/cc/spray/json/formats/StandardFormats.scala9
6 files changed, 61 insertions, 19 deletions
diff --git a/src/main/scala/cc/spray/json/formats/BasicFormats.scala b/src/main/scala/cc/spray/json/formats/BasicFormats.scala
index 378d67d..4be7ef6 100644
--- a/src/main/scala/cc/spray/json/formats/BasicFormats.scala
+++ b/src/main/scala/cc/spray/json/formats/BasicFormats.scala
@@ -1,6 +1,3 @@
-package cc.spray.json
-package formats
-
/*
* Original implementation (C) 2009-2011 Debasish Ghosh
* Adapted and extended in 2011 by Mathias Doenitz
@@ -18,6 +15,12 @@ package formats
* 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] {
diff --git a/src/main/scala/cc/spray/json/formats/CollectionFormats.scala b/src/main/scala/cc/spray/json/formats/CollectionFormats.scala
index 53be0d5..02a9999 100644
--- a/src/main/scala/cc/spray/json/formats/CollectionFormats.scala
+++ b/src/main/scala/cc/spray/json/formats/CollectionFormats.scala
@@ -1,6 +1,3 @@
-package cc.spray.json
-package formats
-
/*
* Original implementation (C) 2009-2011 Debasish Ghosh
* Adapted and extended in 2011 by Mathias Doenitz
@@ -18,10 +15,16 @@ package formats
* 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 {
@@ -30,6 +33,9 @@ trait CollectionFormats {
}
}
+ /**
+ * 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 {
@@ -38,6 +44,10 @@ 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]] {
def write(m: Map[K, V]) = JsObject {
m.toList.map { t =>
@@ -53,11 +63,22 @@ trait CollectionFormats {
}
}
+ /**
+ * 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 {
diff --git a/src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala b/src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala
index 868ebb7..15f2abb 100644
--- a/src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala
+++ b/src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala
@@ -1,6 +1,3 @@
-package cc.spray.json
-package formats
-
/*
* Original implementation (C) 2009-2011 Debasish Ghosh
* Adapted and extended in 2011 by Mathias Doenitz
@@ -18,6 +15,12 @@ package formats
* 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
index 52aa837..dbfb264 100644
--- a/src/main/scala/cc/spray/json/formats/GenericFormats.scala
+++ b/src/main/scala/cc/spray/json/formats/GenericFormats.scala
@@ -1,6 +1,3 @@
-package cc.spray.json
-package formats
-
/*
* Original implementation (C) 2009-2011 Debasish Ghosh
* Adapted and extended in 2011 by Mathias Doenitz
@@ -18,6 +15,12 @@ package formats
* 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
diff --git a/src/main/scala/cc/spray/json/formats/JsonFormat.scala b/src/main/scala/cc/spray/json/formats/JsonFormat.scala
index 1edac19..0951ef8 100644
--- a/src/main/scala/cc/spray/json/formats/JsonFormat.scala
+++ b/src/main/scala/cc/spray/json/formats/JsonFormat.scala
@@ -1,6 +1,3 @@
-package cc.spray.json
-package formats
-
/*
* Original implementation (C) 2009-2011 Debasish Ghosh
* Adapted and extended in 2011 by Mathias Doenitz
@@ -18,12 +15,24 @@ package formats
* 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
index 91e21d3..78bbe53 100644
--- a/src/main/scala/cc/spray/json/formats/StandardFormats.scala
+++ b/src/main/scala/cc/spray/json/formats/StandardFormats.scala
@@ -1,6 +1,3 @@
-package cc.spray.json
-package formats
-
/*
* Original implementation (C) 2009-2011 Debasish Ghosh
* Adapted and extended in 2011 by Mathias Doenitz
@@ -18,6 +15,12 @@ package formats
* 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