summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMathias <mathias@spray.cc>2011-11-30 16:07:43 +0100
committerMathias <mathias@spray.cc>2011-11-30 16:15:12 +0100
commit938576d2859f3749a357c96ac7a9be6c5fd4ec7e (patch)
tree9da1dfc0ae49831927acb57cfb66bc66f10cc85c /src
parentcf840bcbe341a2976ea8ea9afefa157bf426e9f8 (diff)
downloadspray-json-938576d2859f3749a357c96ac7a9be6c5fd4ec7e.tar.gz
spray-json-938576d2859f3749a357c96ac7a9be6c5fd4ec7e.tar.bz2
spray-json-938576d2859f3749a357c96ac7a9be6c5fd4ec7e.zip
Add RootJsonReader and RootJsonWriter as well as some helper methods, closes #10
Diffstat (limited to 'src')
-rw-r--r--src/main/scala/cc/spray/json/AdditionalFormats.scala28
-rw-r--r--src/main/scala/cc/spray/json/JsonFormat.scala14
2 files changed, 40 insertions, 2 deletions
diff --git a/src/main/scala/cc/spray/json/AdditionalFormats.scala b/src/main/scala/cc/spray/json/AdditionalFormats.scala
index dc78115..889fdaf 100644
--- a/src/main/scala/cc/spray/json/AdditionalFormats.scala
+++ b/src/main/scala/cc/spray/json/AdditionalFormats.scala
@@ -36,6 +36,12 @@ trait AdditionalFormats {
}
/**
+ * Constructs a RootJsonFormat from its two parts, RootJsonReader and RootJsonWriter.
+ */
+ def rootJsonFormat[T](reader: RootJsonReader[T], writer: RootJsonWriter[T]) =
+ rootFormat(jsonFormat(reader, writer))
+
+ /**
* Turns a JsonWriter into a JsonFormat that throws an UnsupportedOperationException for reads.
*/
def lift[T](writer :JsonWriter[T]) = new JsonFormat[T] {
@@ -45,6 +51,12 @@ trait AdditionalFormats {
}
/**
+ * Turns a RootJsonWriter into a RootJsonFormat that throws an UnsupportedOperationException for reads.
+ */
+ def lift[T](writer :RootJsonWriter[T]): RootJsonFormat[T] =
+ rootFormat(lift(writer :JsonWriter[T]))
+
+ /**
* Turns a JsonReader into a JsonFormat that throws an UnsupportedOperationException for writes.
*/
def lift[T <: AnyRef](reader :JsonReader[T]) = new JsonFormat[T] {
@@ -54,15 +66,29 @@ trait AdditionalFormats {
}
/**
+ * Turns a RootJsonReader into a RootJsonFormat that throws an UnsupportedOperationException for writes.
+ */
+ def lift[T <: AnyRef](reader :RootJsonReader[T]): RootJsonFormat[T] =
+ rootFormat(lift(reader :JsonReader[T]))
+
+ /**
* Lazy wrapper around serialization. Useful when you want to serialize (mutually) recursive structures.
*/
- def lazyFormat[T](format: => JsonFormat[T]) = new JsonFormat[T]{
+ def lazyFormat[T](format: => JsonFormat[T]) = new JsonFormat[T] {
lazy val delegate = format;
def write(x: T) = delegate.write(x);
def read(value: JsValue) = delegate.read(value);
}
/**
+ * Explicitly turns a JsonFormat into a RootJsonFormat.
+ */
+ def rootFormat[T](format: JsonFormat[T]) = new RootJsonFormat[T] {
+ def write(obj: T) = format.write(obj)
+ def read(json: JsValue) = format.read(json)
+ }
+
+ /**
* Wraps an existing JsonReader with Exception protection.
*/
def safeReader[A :JsonReader] = new JsonReader[Either[Exception, A]] {
diff --git a/src/main/scala/cc/spray/json/JsonFormat.scala b/src/main/scala/cc/spray/json/JsonFormat.scala
index 01b9f80..683a823 100644
--- a/src/main/scala/cc/spray/json/JsonFormat.scala
+++ b/src/main/scala/cc/spray/json/JsonFormat.scala
@@ -53,7 +53,19 @@ object JsonWriter {
trait JsonFormat[T] extends JsonReader[T] with JsonWriter[T]
/**
+ * A special JsonReader capable of reading a legal JSON root object, i.e. either a JSON array or a JSON object.
+ */
+@implicitNotFound(msg = "Cannot find RootJsonReader or RootJsonFormat type class for ${T}")
+trait RootJsonReader[T] extends JsonReader[T]
+
+/**
+ * A special JsonWriter capable of writing a legal JSON root object, i.e. either a JSON array or a JSON object.
+ */
+@implicitNotFound(msg = "Cannot find RootJsonWriter or RootJsonFormat type class for ${T}")
+trait RootJsonWriter[T] extends 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
+trait RootJsonFormat[T] extends JsonFormat[T] with RootJsonReader[T] with RootJsonWriter[T] \ No newline at end of file