summaryrefslogtreecommitdiff
path: root/shared/src/test/scala/spray/json/AdditionalFormatsSpec.scala
diff options
context:
space:
mode:
authorJakob Odersky <jakob@odersky.com>2018-03-07 14:07:25 -0800
committerJakob Odersky <jakob@odersky.com>2019-06-10 23:22:15 +0200
commita43a10a12fd5653e6050c652024764416b71ab54 (patch)
tree19824c4e4eb31f849d51520d57a484e4f4ca0640 /shared/src/test/scala/spray/json/AdditionalFormatsSpec.scala
parentda8ed26cfed5e958eeb29a3444ff882a46090459 (diff)
downloadspray-json-a43a10a12fd5653e6050c652024764416b71ab54.tar.gz
spray-json-a43a10a12fd5653e6050c652024764416b71ab54.tar.bz2
spray-json-a43a10a12fd5653e6050c652024764416b71ab54.zip
Add support for ScalaJS and Scala Native
Binary compatibility with previous versions is maintained.
Diffstat (limited to 'shared/src/test/scala/spray/json/AdditionalFormatsSpec.scala')
-rw-r--r--shared/src/test/scala/spray/json/AdditionalFormatsSpec.scala73
1 files changed, 73 insertions, 0 deletions
diff --git a/shared/src/test/scala/spray/json/AdditionalFormatsSpec.scala b/shared/src/test/scala/spray/json/AdditionalFormatsSpec.scala
new file mode 100644
index 0000000..01127e6
--- /dev/null
+++ b/shared/src/test/scala/spray/json/AdditionalFormatsSpec.scala
@@ -0,0 +1,73 @@
+/*
+ * 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 spray.json
+
+import org.specs2.mutable._
+
+class AdditionalFormatsSpec extends Specification {
+
+ case class Container[A](inner: Option[A])
+
+ object ReaderProtocol extends DefaultJsonProtocol {
+ implicit def containerReader[T :JsonFormat] = lift {
+ new JsonReader[Container[T]] {
+ def read(value: JsValue) = value match {
+ case JsObject(fields) if fields.contains("content") => Container(Some(jsonReader[T].read(fields("content"))))
+ case _ => deserializationError("Unexpected format: " + value.toString)
+ }
+ }
+ }
+ }
+
+ object WriterProtocol extends DefaultJsonProtocol {
+ implicit def containerWriter[T :JsonFormat] = lift {
+ new JsonWriter[Container[T]] {
+ def write(obj: Container[T]) = JsObject("content" -> obj.inner.toJson)
+ }
+ }
+ }
+
+ "The liftJsonWriter" should {
+ val obj = Container(Some(Container(Some(List(1, 2, 3)))))
+
+ "properly write a Container[Container[List[Int]]] to JSON" in {
+ import WriterProtocol._
+ obj.toJson.toString mustEqual """{"content":{"content":[1,2,3]}}"""
+ }
+
+ "properly read a Container[Container[List[Int]]] from JSON" in {
+ import ReaderProtocol._
+ """{"content":{"content":[1,2,3]}}""".parseJson.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._
+ val json = Foo(1, "a", Some(Foo(2, "b", Some(Foo(3, "c") :: Nil)) :: Foo(4, "d") :: Nil)).toJson
+
+ json mustEqual
+ """{"id":1,"name":"a","foos":[{"id":2,"name":"b","foos":[{"id":3,"name":"c"}]},{"id":4,"name":"d"}]}""".parseJson
+ }
+ }
+} \ No newline at end of file