1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package cc.spray.json
package formats
import org.specs.Specification
class GenericFormatsSpec extends Specification with GenericFormats with BasicFormats {
case class Test2(a: Int, b: Double)
implicit val test2Format = format(Test2, "a", "b")
"A JsonFormat created with format, for a case class with 2 elements," should {
val obj = Test2(42, 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
}
"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'"))
)
"ignore additional members during deserialization" in {
JsObject(JsField("a", 42), JsField("b", 4.2), JsField("c", 'no)).fromJson[Test2] mustEqual obj
}
"throw a DeserializationException if the JsValue is not a JsObject" in (
JsNull.fromJson[Test2] must throwA(new DeserializationException("Object expected"))
)
}
}
|