diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/scala/cc/spray/json/ProductFormatsSpec.scala | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/test/scala/cc/spray/json/ProductFormatsSpec.scala b/src/test/scala/cc/spray/json/ProductFormatsSpec.scala index 763b4b0..221d8ae 100644 --- a/src/test/scala/cc/spray/json/ProductFormatsSpec.scala +++ b/src/test/scala/cc/spray/json/ProductFormatsSpec.scala @@ -4,25 +4,31 @@ import org.specs2.mutable._ class ProductFormatsSpec extends Specification with DefaultJsonProtocol { - case class Test2(a: Int, b: Double) + case class Test2(a: Int, b: Option[Double]) implicit val test2Format = jsonFormat(Test2, "a", "b") case class Test3[A, B](as: List[A], bs: List[B]) implicit def test3Format[A: JsonFormat, B: JsonFormat] = jsonFormat(Test3.apply[A, B], "as", "bs") "A JsonFormat created with `jsonFormat`, for a case class with 2 elements," should { - val obj = Test2(42, 4.2) + val obj = Test2(42, Some(4.2)) val json = JsObject(JsField("a", 42), JsField("b", 4.2)) "convert to a respective JsObject" in { obj.toJson mustEqual json } "convert a JsObject to the respective case class instance" in { - json.fromJson[Test2] mustEqual obj + json.fromJson[Test2] mustEqual obj } - "throw a DeserializationException if the JsObject does not define the right members" in ( - JsObject(JsField("a", 42), JsField("x", 4.2)).fromJson[Test2] must - throwA(new DeserializationException("Object is missing required member 'b'")) + "throw a DeserializationException if the JsObject does not all required members" in ( + JsObject(JsField("b", 4.2)).fromJson[Test2] must + throwA(new DeserializationException("Object is missing required member 'a'")) ) + "not require the presence of optional fields for deserialization" in { + JsObject(JsField("a", 42)).fromJson[Test2] mustEqual Test2(42, None) + } + "not render `None` members during serialization" in { + Test2(42, None).toJson mustEqual JsObject(JsField("a", 42)) + } "ignore additional members during deserialization" in { JsObject(JsField("a", 42), JsField("b", 4.2), JsField("c", 'no)).fromJson[Test2] mustEqual obj } |