diff options
author | Mathias <mathias@spray.cc> | 2012-10-12 14:15:12 +0200 |
---|---|---|
committer | Mathias <mathias@spray.cc> | 2012-10-12 14:15:12 +0200 |
commit | e5a7de26dfc8a4bb4410b7ee500624e30bf66650 (patch) | |
tree | 70d2133716f04df15b93350a36b8dd1aad28704c /src/test/scala/spray | |
parent | 5354b7b2b1af66049328eed150e036a314878559 (diff) | |
download | spray-json-e5a7de26dfc8a4bb4410b7ee500624e30bf66650.tar.gz spray-json-e5a7de26dfc8a4bb4410b7ee500624e30bf66650.tar.bz2 spray-json-e5a7de26dfc8a4bb4410b7ee500624e30bf66650.zip |
Remove obsolete /cc/ package directories
Diffstat (limited to 'src/test/scala/spray')
-rw-r--r-- | src/test/scala/spray/json/AdditionalFormatsSpec.scala | 71 | ||||
-rw-r--r-- | src/test/scala/spray/json/BasicFormatsSpec.scala | 162 | ||||
-rw-r--r-- | src/test/scala/spray/json/CollectionFormatsSpec.scala | 82 | ||||
-rw-r--r-- | src/test/scala/spray/json/CompactPrinterSpec.scala | 67 | ||||
-rw-r--r-- | src/test/scala/spray/json/CustomFormatSpec.scala | 45 | ||||
-rw-r--r-- | src/test/scala/spray/json/JsonParserSpec.scala | 68 | ||||
-rw-r--r-- | src/test/scala/spray/json/PrettyPrinterSpec.scala | 66 | ||||
-rw-r--r-- | src/test/scala/spray/json/ProductFormatsSpec.scala | 87 | ||||
-rw-r--r-- | src/test/scala/spray/json/ReadmeSpec.scala | 98 | ||||
-rw-r--r-- | src/test/scala/spray/json/StandardFormatsSpec.scala | 76 |
10 files changed, 822 insertions, 0 deletions
diff --git a/src/test/scala/spray/json/AdditionalFormatsSpec.scala b/src/test/scala/spray/json/AdditionalFormatsSpec.scala new file mode 100644 index 0000000..0a28560 --- /dev/null +++ b/src/test/scala/spray/json/AdditionalFormatsSpec.scala @@ -0,0 +1,71 @@ +/* + * 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]}}""".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 diff --git a/src/test/scala/spray/json/BasicFormatsSpec.scala b/src/test/scala/spray/json/BasicFormatsSpec.scala new file mode 100644 index 0000000..82fb127 --- /dev/null +++ b/src/test/scala/spray/json/BasicFormatsSpec.scala @@ -0,0 +1,162 @@ +/* + * 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 BasicFormatsSpec extends Specification with DefaultJsonProtocol { + + "The IntJsonFormat" should { + "convert an Int to a JsNumber" in { + 42.toJson mustEqual JsNumber(42) + } + "convert a JsNumber to an Int" in { + JsNumber(42).convertTo[Int] mustEqual 42 + } + } + + "The LongJsonFormat" should { + "convert a Long to a JsNumber" in { + 42L.toJson mustEqual JsNumber(42L) + } + "convert a JsNumber to a Long" in { + JsNumber(42L).convertTo[Long] mustEqual 42L + } + } + + "The FloatJsonFormat" should { + "convert a Float to a JsNumber" in { + 4.2f.toJson mustEqual JsNumber(4.2f) + } + "convert a Float.NaN to a JsNull" in { + Float.NaN.toJson mustEqual JsNull + } + "convert a Float.PositiveInfinity to a JsNull" in { + Float.PositiveInfinity.toJson mustEqual JsNull + } + "convert a Float.NegativeInfinity to a JsNull" in { + Float.NegativeInfinity.toJson mustEqual JsNull + } + "convert a JsNumber to a Float" in { + JsNumber(4.2f).convertTo[Float] mustEqual 4.2f + } + "convert a JsNull to a Float" in { + JsNull.convertTo[Float].isNaN mustEqual Float.NaN.isNaN + } + } + + "The DoubleJsonFormat" should { + "convert a Double to a JsNumber" in { + 4.2.toJson mustEqual JsNumber(4.2) + } + "convert a Double.NaN to a JsNull" in { + Double.NaN.toJson mustEqual JsNull + } + "convert a Double.PositiveInfinity to a JsNull" in { + Double.PositiveInfinity.toJson mustEqual JsNull + } + "convert a Double.NegativeInfinity to a JsNull" in { + Double.NegativeInfinity.toJson mustEqual JsNull + } + "convert a JsNumber to a Double" in { + JsNumber(4.2).convertTo[Double] mustEqual 4.2 + } + "convert a JsNull to a Double" in { + JsNull.convertTo[Double].isNaN mustEqual Double.NaN.isNaN + } + } + + "The ByteJsonFormat" should { + "convert a Byte to a JsNumber" in { + 42.asInstanceOf[Byte].toJson mustEqual JsNumber(42) + } + "convert a JsNumber to a Byte" in { + JsNumber(42).convertTo[Byte] mustEqual 42 + } + } + + "The ShortJsonFormat" should { + "convert a Short to a JsNumber" in { + 42.asInstanceOf[Short].toJson mustEqual JsNumber(42) + } + "convert a JsNumber to a Short" in { + JsNumber(42).convertTo[Short] mustEqual 42 + } + } + + "The BigDecimalJsonFormat" should { + "convert a BigDecimal to a JsNumber" in { + BigDecimal(42).toJson mustEqual JsNumber(42) + } + "convert a JsNumber to a BigDecimal" in { + JsNumber(42).convertTo[BigDecimal] mustEqual BigDecimal(42) + } + } + + "The BigIntJsonFormat" should { + "convert a BigInt to a JsNumber" in { + BigInt(42).toJson mustEqual JsNumber(42) + } + "convert a JsNumber to a BigInt" in { + JsNumber(42).convertTo[BigInt] mustEqual BigInt(42) + } + } + + "The UnitJsonFormat" should { + "convert Unit to a JsNumber(1)" in { + ().toJson mustEqual JsNumber(1) + } + "convert a JsNumber to Unit" in { + JsNumber(1).convertTo[Unit] mustEqual () + } + } + + "The BooleanJsonFormat" should { + "convert true to a JsTrue" in { true.toJson mustEqual JsTrue } + "convert false to a JsFalse" in { false.toJson mustEqual JsFalse } + "convert a JsTrue to true" in { JsTrue.convertTo[Boolean] mustEqual true } + "convert a JsFalse to false" in { JsFalse.convertTo[Boolean] mustEqual false } + } + + "The CharJsonFormat" should { + "convert a Char to a JsString" in { + 'c'.toJson mustEqual JsString("c") + } + "convert a JsString to a Char" in { + JsString("c").convertTo[Char] mustEqual 'c' + } + } + + "The StringJsonFormat" should { + "convert a String to a JsString" in { + "Hello".toJson mustEqual JsString("Hello") + } + "convert a JsString to a String" in { + JsString("Hello").convertTo[String] mustEqual "Hello" + } + } + + "The SymbolJsonFormat" should { + "convert a Symbol to a JsString" in { + 'Hello.toJson mustEqual JsString("Hello") + } + "convert a JsString to a Symbol" in { + JsString("Hello").convertTo[Symbol] mustEqual 'Hello + } + } + +} diff --git a/src/test/scala/spray/json/CollectionFormatsSpec.scala b/src/test/scala/spray/json/CollectionFormatsSpec.scala new file mode 100644 index 0000000..3d953bb --- /dev/null +++ b/src/test/scala/spray/json/CollectionFormatsSpec.scala @@ -0,0 +1,82 @@ +/* + * 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._ +import java.util.Arrays + +class CollectionFormatsSpec extends Specification with DefaultJsonProtocol { + + "The listFormat" should { + val list = List(1, 2, 3) + val json = JsArray(JsNumber(1), JsNumber(2), JsNumber(3)) + "convert a List[Int] to a JsArray of JsNumbers" in { + list.toJson mustEqual json + } + "convert a JsArray of JsNumbers to a List[Int]" in { + json.convertTo[List[Int]] mustEqual list + } + } + + "The arrayFormat" should { + val array = Array(1, 2, 3) + val json = JsArray(JsNumber(1), JsNumber(2), JsNumber(3)) + "convert an Array[Int] to a JsArray of JsNumbers" in { + array.toJson mustEqual json + } + "convert a JsArray of JsNumbers to an Array[Int]" in { + Arrays.equals(json.convertTo[Array[Int]], array) must beTrue + } + } + + "The mapFormat" should { + val map = Map("a" -> 1, "b" -> 2, "c" -> 3) + val json = JsObject("a" -> JsNumber(1), "b" -> JsNumber(2), "c" -> JsNumber(3)) + "convert a Map[String, Long] to a JsObject" in { + map.toJson mustEqual json + } + "be able to convert a JsObject to a Map[String, Long]" in { + json.convertTo[Map[String, Long]] mustEqual map + } + "throw an Exception when trying to serialize a map whose key are not serialized to JsStrings" in { + Map(1 -> "a").toJson must throwA(new SerializationException("Map key must be formatted as JsString, not '1'")) + } + } + + "The immutableSetFormat" should { + val set = Set(1, 2, 3) + val json = JsArray(JsNumber(1), JsNumber(2), JsNumber(3)) + "convert a Set[Int] to a JsArray of JsNumbers" in { + set.toJson mustEqual json + } + "convert a JsArray of JsNumbers to a Set[Int]" in { + json.convertTo[Set[Int]] mustEqual set + } + } + + "The indexedSeqFormat" should { + val seq = collection.IndexedSeq(1, 2, 3) + val json = JsArray(JsNumber(1), JsNumber(2), JsNumber(3)) + "convert a Set[Int] to a JsArray of JsNumbers" in { + seq.toJson mustEqual json + } + "convert a JsArray of JsNumbers to a IndexedSeq[Int]" in { + json.convertTo[collection.IndexedSeq[Int]] mustEqual seq + } + } + +}
\ No newline at end of file diff --git a/src/test/scala/spray/json/CompactPrinterSpec.scala b/src/test/scala/spray/json/CompactPrinterSpec.scala new file mode 100644 index 0000000..c00512d --- /dev/null +++ b/src/test/scala/spray/json/CompactPrinterSpec.scala @@ -0,0 +1,67 @@ +/* + * 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 CompactPrinterSpec extends Specification { + + "The CompactPrinter" should { + "print JsNull to 'null'" in { + CompactPrinter(JsNull) mustEqual "null" + } + "print JsTrue to 'true'" in { + CompactPrinter(JsTrue) mustEqual "true" + } + "print JsFalse to 'false'" in { + CompactPrinter(JsFalse) mustEqual "false" + } + "print JsNumber(0) to '0'" in { + CompactPrinter(JsNumber(0)) mustEqual "0" + } + "print JsNumber(1.23) to '1.23'" in { + CompactPrinter(JsNumber(1.23)) mustEqual "1.23" + } + "print JsNumber(1.23) to '1.23'" in { + CompactPrinter(JsNumber(1.23)) mustEqual "1.23" + } + "print JsNumber(-1E10) to '-1E10'" in { + CompactPrinter(JsNumber(-1E10)) mustEqual "-1.0E+10" + } + "print JsNumber(12.34e-10) to '12.34e-10'" in { + CompactPrinter(JsNumber(12.34e-10)) mustEqual "1.234E-9" + } + "print JsString(\"xyz\") to \"xyz\"" in { + CompactPrinter(JsString("xyz")) mustEqual "\"xyz\"" + } + "properly escape special chars in JsString" in { + CompactPrinter(JsString("\"\\\b\f\n\r\t\u12AB")) mustEqual """"\"\\\b\f\n\r\t""" + "\\u12ab\"" + } + "properly print a simple JsObject" in ( + CompactPrinter(JsObject("key" -> JsNumber(42), "key2" -> JsString("value"))) + mustEqual """{"key":42,"key2":"value"}""" + ) + "properly print a simple JsArray" in ( + CompactPrinter(JsArray(JsNull, JsNumber(1.23), JsObject("key" -> JsBoolean(true)))) + mustEqual """[null,1.23,{"key":true}]""" + ) + "properly print a JSON padding (JSONP) if requested" in { + CompactPrinter(JsTrue, Some("customCallback")) mustEqual("customCallback(true)") + } + } + +}
\ No newline at end of file diff --git a/src/test/scala/spray/json/CustomFormatSpec.scala b/src/test/scala/spray/json/CustomFormatSpec.scala new file mode 100644 index 0000000..dcea4f2 --- /dev/null +++ b/src/test/scala/spray/json/CustomFormatSpec.scala @@ -0,0 +1,45 @@ +/* + * 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.Specification + +class CustomFormatSpec extends Specification with DefaultJsonProtocol { + + case class MyType(name: String, value: Int) + + implicit val MyTypeProtocol = new RootJsonFormat[MyType] { + def read(json: JsValue) = { + json.asJsObject.getFields("name", "value") match { + case Seq(JsString(name), JsNumber(value)) => MyType(name, value.toInt) + case _ => deserializationError("Expected fields: 'name' (JSON string) and 'value' (JSON number)") + } + } + def write(obj: MyType) = JsObject("name" -> JsString(obj.name), "value" -> JsNumber(obj.value)) + } + + "A custom JsonFormat built with 'asJsonObject'" should { + val value = MyType("bob", 42) + "correctly deserialize valid JSON content" in { + """{ "name": "bob", "value": 42 }""".asJson.convertTo[MyType] mustEqual value + } + "support full round-trip (de)serialization" in { + value.toJson.convertTo[MyType] mustEqual value + } + } + +}
\ No newline at end of file diff --git a/src/test/scala/spray/json/JsonParserSpec.scala b/src/test/scala/spray/json/JsonParserSpec.scala new file mode 100644 index 0000000..af68216 --- /dev/null +++ b/src/test/scala/spray/json/JsonParserSpec.scala @@ -0,0 +1,68 @@ +/* + * 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._ +import org.parboiled.common.FileUtils + +class JsonParserSpec extends Specification { + + "The JsonParser" should { + "parse 'null' to JsNull" in { + JsonParser("null") mustEqual JsNull + } + "parse 'true' to JsTrue" in { + JsonParser("true") mustEqual JsTrue + } + "parse 'false' to JsFalse" in { + JsonParser("false") mustEqual JsFalse + } + "parse '0' to JsNumber" in { + JsonParser("0") mustEqual JsNumber(0) + } + "parse '1.23' to JsNumber" in { + JsonParser("1.23") mustEqual JsNumber(1.23) + } + "parse '-1E10' to JsNumber" in { + JsonParser("-1E10") mustEqual JsNumber("-1E+10") + } + "parse '12.34e-10' to JsNumber" in { + JsonParser("12.34e-10") mustEqual JsNumber("1.234E-9") + } + "parse \"xyz\" to JsString" in { + JsonParser("\"xyz\"") mustEqual JsString("xyz") + } + "parse escapes in a JsString" in { + JsonParser(""""\"\\/\b\f\n\r\t\u12Ab"""") mustEqual JsString("\"\\/\b\f\n\r\t\u12ab") + } + "properly parse a simple JsObject" in ( + JsonParser(""" { "key" :42, "key2": "value" }""") mustEqual + JsObject("key" -> JsNumber(42), "key2" -> JsString("value")) + ) + "properly parse a simple JsArray" in ( + JsonParser("""[null, 1.23 ,{"key":true } ] """) mustEqual + JsArray(JsNull, JsNumber(1.23), JsObject("key" -> JsBoolean(true))) + ) + "be reentrant" in { + val largeJsonSource = FileUtils.readAllCharsFromResource("test.json") + List.fill(20)(largeJsonSource).par.map(JsonParser(_)).toList.map { + _.asInstanceOf[JsObject].fields("questions").asInstanceOf[JsArray].elements.size + } mustEqual List.fill(20)(100) + } + } + +}
\ No newline at end of file diff --git a/src/test/scala/spray/json/PrettyPrinterSpec.scala b/src/test/scala/spray/json/PrettyPrinterSpec.scala new file mode 100644 index 0000000..8b7bc2b --- /dev/null +++ b/src/test/scala/spray/json/PrettyPrinterSpec.scala @@ -0,0 +1,66 @@ +/* + * 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 PrettyPrinterSpec extends Specification { + + "The PrettyPrinter" should { + "print a more complicated JsObject nicely aligned" in { + PrettyPrinter { + JsonParser { + """|{ + | "simpleKey" : "some value", + | "key with spaces": null, + | "zero": 0, + | "number": -1.2323424E-5, + | "Boolean yes":true, + | "Boolean no": false, + | "Unic\u00f8de" : "Long string with newline\nescape", + | "key with \"quotes\"" : "string", + | "sub object" : { + | "sub key": 26.5, + | "a": "b", + | "array": [1, 2, { "yes":1, "no":0 }, ["a", "b", null], false] + | } + |}""".stripMargin + } + } mustEqual { + """|{ + | "simpleKey": "some value", + | "key with spaces": null, + | "zero": 0, + | "number": -0.000012323424, + | "Boolean yes": true, + | "Boolean no": false, + | "Unic\u00f8de": "Long string with newline\nescape", + | "key with \"quotes\"": "string", + | "sub object": { + | "sub key": 26.5, + | "a": "b", + | "array": [1, 2, { + | "yes": 1, + | "no": 0 + | }, ["a", "b", null], false] + | } + |}""".stripMargin.replace("\u00f8", "\\u00f8") + } + } + } + +}
\ No newline at end of file diff --git a/src/test/scala/spray/json/ProductFormatsSpec.scala b/src/test/scala/spray/json/ProductFormatsSpec.scala new file mode 100644 index 0000000..e452639 --- /dev/null +++ b/src/test/scala/spray/json/ProductFormatsSpec.scala @@ -0,0 +1,87 @@ +/* + * 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 ProductFormatsSpec extends Specification { + + case class Test2(a: Int, b: Option[Double]) + case class Test3[A, B](as: List[A], bs: List[B]) + + trait TestProtocol { + this: DefaultJsonProtocol => + implicit val test2Format = jsonFormat2(Test2) + implicit def test3Format[A: JsonFormat, B: JsonFormat] = jsonFormat2(Test3.apply[A, B]) + } + object TestProtocol1 extends DefaultJsonProtocol with TestProtocol + object TestProtocol2 extends DefaultJsonProtocol with TestProtocol with NullOptions + + "A JsonFormat created with `jsonFormat`, for a case class with 2 elements," should { + import TestProtocol1._ + val obj = Test2(42, Some(4.2)) + val json = JsObject("a" -> JsNumber(42), "b" -> JsNumber(4.2)) + "convert to a respective JsObject" in { + obj.toJson mustEqual json + } + "convert a JsObject to the respective case class instance" in { + json.convertTo[Test2] mustEqual obj + } + "throw a DeserializationException if the JsObject does not all required members" in ( + JsObject("b" -> JsNumber(4.2)).convertTo[Test2] must + throwA(new DeserializationException("Object is missing required member 'a'")) + ) + "not require the presence of optional fields for deserialization" in { + JsObject("a" -> JsNumber(42)).convertTo[Test2] mustEqual Test2(42, None) + } + "not render `None` members during serialization" in { + Test2(42, None).toJson mustEqual JsObject("a" -> JsNumber(42)) + } + "ignore additional members during deserialization" in { + JsObject("a" -> JsNumber(42), "b" -> JsNumber(4.2), "c" -> JsString('no)).convertTo[Test2] mustEqual obj + } + "not depend on any specific member order for deserialization" in { + JsObject("b" -> JsNumber(4.2), "a" -> JsNumber(42)).convertTo[Test2] mustEqual obj + } + "throw a DeserializationException if the JsValue is not a JsObject" in ( + JsNull.convertTo[Test2] must throwA(new DeserializationException("Object expected")) + ) + } + + "A JsonProtocol mixing in NullOptions" should { + "render `None` members to `null`" in { + import TestProtocol2._ + Test2(42, None).toJson mustEqual JsObject("a" -> JsNumber(42), "b" -> JsNull) + } + } + + "A JsonFormat for a generic case class and created with `jsonFormat`" should { + import TestProtocol1._ + val obj = Test3(42 :: 43 :: Nil, "x" :: "y" :: "z" :: Nil) + val json = JsObject( + "as" -> JsArray(JsNumber(42), JsNumber(43)), + "bs" -> JsArray(JsString("x"), JsString("y"), JsString("z")) + ) + "convert to a respective JsObject" in { + obj.toJson mustEqual json + } + "convert a JsObject to the respective case class instance" in { + json.convertTo[Test3[Int, String]] mustEqual obj + } + } + +} diff --git a/src/test/scala/spray/json/ReadmeSpec.scala b/src/test/scala/spray/json/ReadmeSpec.scala new file mode 100644 index 0000000..89d3dbc --- /dev/null +++ b/src/test/scala/spray/json/ReadmeSpec.scala @@ -0,0 +1,98 @@ +/* + * 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 ReadmeSpec extends Specification { + + "The Usage snippets" should { + "behave as expected" in { + import DefaultJsonProtocol._ + + val source = """{ "some": "JSON source" }""" + val jsonAst = source.asJson + jsonAst mustEqual JsObject("some" -> JsString("JSON source")) + + val json2 = jsonAst.prettyPrint + json2 mustEqual + """{ + | "some": "JSON source" + |}""".stripMargin + + val jsonAst2 = List(1, 2, 3).toJson + jsonAst2 mustEqual JsArray(JsNumber(1), JsNumber(2), JsNumber(3)) + } + } + + case class Color(name: String, red: Int, green: Int, blue: Int) + val color = Color("CadetBlue", 95, 158, 160) + + "The case class example" should { + "behave as expected" in { + object MyJsonProtocol extends DefaultJsonProtocol { + implicit val colorFormat = jsonFormat4(Color) + } + import MyJsonProtocol._ + color.toJson.convertTo[Color] mustEqual color + } + } + + "The non case class (array) example" should { + "behave as expected" in { + object MyJsonProtocol extends DefaultJsonProtocol { + implicit object ColorJsonFormat extends JsonFormat[Color] { + def write(c: Color) = + JsArray(JsString(c.name), JsNumber(c.red), JsNumber(c.green), JsNumber(c.blue)) + + def read(value: JsValue) = value match { + case JsArray(JsString(name) :: JsNumber(red) :: JsNumber(green) :: JsNumber(blue) :: Nil) => + new Color(name, red.toInt, green.toInt, blue.toInt) + case _ => deserializationError("Color expected") + } + } + } + import MyJsonProtocol._ + color.toJson.convertTo[Color] mustEqual color + } + } + + "The non case class (object) example" should { + "behave as expected" in { + object MyJsonProtocol extends DefaultJsonProtocol { + implicit object ColorJsonFormat extends JsonFormat[Color] { + def write(c: Color) = JsObject( + "name" -> JsString(c.name), + "red" -> JsNumber(c.red), + "green" -> JsNumber(c.green), + "blue" -> JsNumber(c.blue) + ) + def read(value: JsValue) = { + value.asJsObject.getFields("name", "red", "green", "blue") match { + case Seq(JsString(name), JsNumber(red), JsNumber(green), JsNumber(blue)) => + new Color(name, red.toInt, green.toInt, blue.toInt) + case _ => throw new DeserializationException("Color expected") + } + } + } + } + import MyJsonProtocol._ + color.toJson.convertTo[Color] mustEqual color + } + } + +}
\ No newline at end of file diff --git a/src/test/scala/spray/json/StandardFormatsSpec.scala b/src/test/scala/spray/json/StandardFormatsSpec.scala new file mode 100644 index 0000000..0b4dc26 --- /dev/null +++ b/src/test/scala/spray/json/StandardFormatsSpec.scala @@ -0,0 +1,76 @@ +/* + * 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._ +import scala.Right + +class StandardFormatsSpec extends Specification with DefaultJsonProtocol { + + "The optionFormat" should { + "convert None to JsNull" in { + None.asInstanceOf[Option[Int]].toJson mustEqual JsNull + } + "convert JsNull to None" in { + JsNull.convertTo[Option[Int]] mustEqual None + } + "convert Some(Hello) to JsString(Hello)" in { + Some("Hello").asInstanceOf[Option[String]].toJson mustEqual JsString("Hello") + } + "convert JsString(Hello) to Some(Hello)" in { + JsString("Hello").convertTo[Option[String]] mustEqual Some("Hello") + } + } + + "The eitherFormat" should { + val a: Either[Int, String] = Left(42) + val b: Either[Int, String] = Right("Hello") + + "convert the left side of an Either value to Json" in { + a.toJson mustEqual JsNumber(42) + } + "convert the right side of an Either value to Json" in { + b.toJson mustEqual JsString("Hello") + } + "convert the left side of an Either value from Json" in { + JsNumber(42).convertTo[Either[Int, String]] mustEqual Left(42) + } + "convert the right side of an Either value from Json" in { + JsString("Hello").convertTo[Either[Int, String]] mustEqual Right("Hello") + } + } + + "The tuple1Format" should { + "convert (42) to a JsNumber" in { + Tuple1(42).toJson mustEqual JsNumber(42) + } + "be able to convert a JsNumber to a Tuple1[Int]" in { + JsNumber(42).convertTo[Tuple1[Int]] mustEqual Tuple1(42) + } + } + + "The tuple2Format" should { + val json = JsArray(JsNumber(42), JsNumber(4.2)) + "convert (42, 4.2) to a JsArray" in { + (42, 4.2).toJson mustEqual json + } + "be able to convert a JsArray to a (Int, Double)]" in { + json.convertTo[(Int, Double)] mustEqual (42, 4.2) + } + } + +}
\ No newline at end of file |