summaryrefslogtreecommitdiff
path: root/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala')
-rw-r--r--src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala38
1 files changed, 34 insertions, 4 deletions
diff --git a/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala b/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala
index 7368128..f25c9d8 100644
--- a/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala
+++ b/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 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 org.specs2.mutable._
@@ -10,8 +26,8 @@ class AdditionalFormatsSpec extends Specification {
implicit def containerReader[T :JsonFormat] = lift {
new JsonReader[Container[T]] {
def read(value: JsValue) = value match {
- case JsObject(JsField("content", obj: JsValue) :: Nil) => Container(Some(jsonReader[T].read(obj)))
- case _ => throw new DeserializationException("Unexpected format: " + value.toString)
+ case JsObject(fields) if fields.contains("content") => Container(Some(jsonReader[T].read(fields("content"))))
+ case _ => deserializationError("Unexpected format: " + value.toString)
}
}
}
@@ -20,7 +36,7 @@ class AdditionalFormatsSpec extends Specification {
object WriterProtocol extends DefaultJsonProtocol {
implicit def containerWriter[T :JsonFormat] = lift {
new JsonWriter[Container[T]] {
- def write(obj: Container[T]) = JsObject(JsField("content", obj.inner.toJson))
+ def write(obj: Container[T]) = JsObject("content" -> obj.inner.toJson)
}
}
}
@@ -35,7 +51,21 @@ class AdditionalFormatsSpec extends Specification {
"properly read a Container[Container[List[Int]]] from JSON" in {
import ReaderProtocol._
- JsonParser("""{"content":{"content":[1,2,3]}}""").fromJson[Container[Container[List[Int]]]] mustEqual obj
+ """{"content":{"content":[1,2,3]}}""".asJson.convertTo[Container[Container[List[Int]]]] mustEqual obj
+ }
+ }
+
+ case class Foo(id: Long, name: String, foos: Option[List[Foo]] = None)
+
+ object FooProtocol extends DefaultJsonProtocol {
+ implicit val FooProtocol: JsonFormat[Foo] = lazyFormat(jsonFormat(Foo, "id", "name", "foos"))
+ }
+
+ "The lazyFormat wrapper" should {
+ "enable recursive format definitions" in {
+ import FooProtocol._
+ Foo(1, "a", Some(Foo(2, "b", Some(Foo(3, "c") :: Nil)) :: Foo(4, "d") :: Nil)).toJson.toString mustEqual
+ """{"id":1,"name":"a","foos":[{"id":2,"name":"b","foos":[{"id":3,"name":"c"}]},{"id":4,"name":"d"}]}"""
}
}
} \ No newline at end of file