summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/scala/cc/spray/json/AdditionalFormats.scala62
-rw-r--r--src/main/scala/cc/spray/json/DefaultJsonProtocol.scala7
-rw-r--r--src/main/scala/cc/spray/json/ProductFormats.scala (renamed from src/main/scala/cc/spray/json/GenericFormats.scala)16
-rw-r--r--src/main/scala/cc/spray/json/StandardFormats.scala11
-rw-r--r--src/test/scala/cc/spray/json/BasicFormatsSpec.scala2
-rw-r--r--src/test/scala/cc/spray/json/CollectionFormatsSpec.scala2
-rw-r--r--src/test/scala/cc/spray/json/ProductFormatsSpec.scala (renamed from src/test/scala/cc/spray/json/GenericFormatsSpec.scala)2
-rw-r--r--src/test/scala/cc/spray/json/StandardFormatsSpec.scala2
8 files changed, 77 insertions, 27 deletions
diff --git a/src/main/scala/cc/spray/json/AdditionalFormats.scala b/src/main/scala/cc/spray/json/AdditionalFormats.scala
new file mode 100644
index 0000000..62a30e7
--- /dev/null
+++ b/src/main/scala/cc/spray/json/AdditionalFormats.scala
@@ -0,0 +1,62 @@
+/*
+ * 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 additional JsonFormats and helpers
+ */
+trait AdditionalFormats {
+
+ implicit object JsValueFormat extends JsonFormat[JsValue] {
+ def write(value: JsValue) = value
+ def read(value: JsValue) = value
+ }
+
+ def formatFromWriter[T :JsonWriter] = new JsonFormat[T] {
+ def write(obj: T) = obj.toJson
+ def read(value: JsValue) = throw new RuntimeException("JsonFormat constructed from JsonWriter can't read from JSON")
+ }
+
+ def formatFromReader[T :JsonReader] = new JsonFormat[T] {
+ def write(obj: T) = throw new RuntimeException("JsonFormat constructed from JsonReader can't write JSON")
+ def read(value: JsValue) = value.fromJson[T]
+ }
+
+ /**
+ * Lazy wrapper around serialization. Useful when you want to serialize (mutually) recursive structures.
+ */
+ 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);
+ }
+
+ /**
+ * Wraps an existing JsonReader with Exception protection.
+ */
+ def safeReader[A :JsonReader] = new JsonReader[Either[Exception, A]] {
+ def read(json: JsValue) = {
+ try {
+ Right(json.fromJson)
+ } catch {
+ case e: Exception => Left(e)
+ }
+ }
+ }
+
+} \ 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
index a8201c2..6f8daf5 100644
--- a/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala
+++ b/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala
@@ -20,6 +20,11 @@ package cc.spray.json
/**
* Provides all the predefined JsonFormats.
*/
-trait DefaultJsonProtocol extends BasicFormats with StandardFormats with CollectionFormats with GenericFormats
+trait DefaultJsonProtocol
+ extends BasicFormats
+ with StandardFormats
+ with CollectionFormats
+ with ProductFormats
+ with AdditionalFormats
object DefaultJsonProtocol extends DefaultJsonProtocol
diff --git a/src/main/scala/cc/spray/json/GenericFormats.scala b/src/main/scala/cc/spray/json/ProductFormats.scala
index 8856e02..39b44fb 100644
--- a/src/main/scala/cc/spray/json/GenericFormats.scala
+++ b/src/main/scala/cc/spray/json/ProductFormats.scala
@@ -18,21 +18,13 @@
package cc.spray.json
/**
- * Provides the helpers for constructing custom JsonFormat implementations.
+ * Provides the helpers for constructing custom JsonFormat implementations for types implementing the Product trait
+ * (especially case classes)
*/
-trait GenericFormats {
+trait ProductFormats {
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, T <: Product](construct: A => T, a: String) = new JF[T]{
def write(p: T) = JsObject(
JsField(a, element[A](p, 0).toJson)
diff --git a/src/main/scala/cc/spray/json/StandardFormats.scala b/src/main/scala/cc/spray/json/StandardFormats.scala
index 20012b4..8b0dc3a 100644
--- a/src/main/scala/cc/spray/json/StandardFormats.scala
+++ b/src/main/scala/cc/spray/json/StandardFormats.scala
@@ -23,19 +23,10 @@ import scala.{Left, Right}
* Provides the JsonFormats for the non-collection standard types.
*/
trait StandardFormats {
+ this: AdditionalFormats =>
private type JF[T] = JsonFormat[T] // simple alias for reduced verbosity
- def safeReader[A :JsonFormat] = new JsonReader[Either[Exception, A]] {
- def read(json: JsValue) = {
- try {
- Right(json.fromJson)
- } catch {
- case e: Exception => Left(e)
- }
- }
- }
-
implicit def optionFormat[T :JF] = new JF[Option[T]] {
def write(option: Option[T]) = option match {
case Some(x) => x.toJson
diff --git a/src/test/scala/cc/spray/json/BasicFormatsSpec.scala b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala
index 7207db5..c26d302 100644
--- a/src/test/scala/cc/spray/json/BasicFormatsSpec.scala
+++ b/src/test/scala/cc/spray/json/BasicFormatsSpec.scala
@@ -2,7 +2,7 @@ package cc.spray.json
import org.specs.Specification
-class BasicFormatsSpec extends Specification with BasicFormats {
+class BasicFormatsSpec extends Specification with DefaultJsonProtocol {
"The IntJsonFormat" should {
"convert an Int to a JsNumber" in {
diff --git a/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala b/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala
index 7a47fb7..ba792a3 100644
--- a/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala
+++ b/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala
@@ -3,7 +3,7 @@ package cc.spray.json
import org.specs.Specification
import java.util.Arrays
-class CollectionFormatsSpec extends Specification with CollectionFormats with BasicFormats {
+class CollectionFormatsSpec extends Specification with DefaultJsonProtocol {
"The listFormat" should {
val list = List(1, 2, 3)
diff --git a/src/test/scala/cc/spray/json/GenericFormatsSpec.scala b/src/test/scala/cc/spray/json/ProductFormatsSpec.scala
index b284d42..5f253c2 100644
--- a/src/test/scala/cc/spray/json/GenericFormatsSpec.scala
+++ b/src/test/scala/cc/spray/json/ProductFormatsSpec.scala
@@ -2,7 +2,7 @@ package cc.spray.json
import org.specs.Specification
-class GenericFormatsSpec extends Specification with GenericFormats with BasicFormats {
+class ProductFormatsSpec extends Specification with DefaultJsonProtocol {
case class Test2(a: Int, b: Double)
implicit val test2Format = jsonFormat(Test2, "a", "b")
diff --git a/src/test/scala/cc/spray/json/StandardFormatsSpec.scala b/src/test/scala/cc/spray/json/StandardFormatsSpec.scala
index 7c454ac..8005c9f 100644
--- a/src/test/scala/cc/spray/json/StandardFormatsSpec.scala
+++ b/src/test/scala/cc/spray/json/StandardFormatsSpec.scala
@@ -3,7 +3,7 @@ package cc.spray.json
import org.specs.Specification
import scala.Right
-class StandardFormatsSpec extends Specification with StandardFormats with BasicFormats {
+class StandardFormatsSpec extends Specification with DefaultJsonProtocol {
"The optionFormat" should {
"convert None to JsNull" in {