diff options
author | Mathias <mathias@spray.cc> | 2011-09-06 11:55:22 +0200 |
---|---|---|
committer | Mathias <mathias@spray.cc> | 2011-09-06 11:55:22 +0200 |
commit | 14670623eff7d5394eadc41c56056f2a5a047a70 (patch) | |
tree | 86828d895ce38645eb6b2dc0f21ef7e3ff23334d /src/test | |
parent | b93e81edfe49c677cce64e32fe49850204e14c97 (diff) | |
download | spray-json-14670623eff7d5394eadc41c56056f2a5a047a70.tar.gz spray-json-14670623eff7d5394eadc41c56056f2a5a047a70.tar.bz2 spray-json-14670623eff7d5394eadc41c56056f2a5a047a70.zip |
Improve JsonReader/JsonWriter lifting to JsonFormat
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala | 41 | ||||
-rw-r--r-- | src/test/scala/cc/spray/json/LiftedFormatsSpec.scala | 40 |
2 files changed, 41 insertions, 40 deletions
diff --git a/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala b/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala new file mode 100644 index 0000000..7368128 --- /dev/null +++ b/src/test/scala/cc/spray/json/AdditionalFormatsSpec.scala @@ -0,0 +1,41 @@ +package cc.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(JsField("content", obj: JsValue) :: Nil) => Container(Some(jsonReader[T].read(obj))) + case _ => throw new DeserializationException("Unexpected format: " + value.toString) + } + } + } + } + + 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)) + } + } + } + + "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._ + JsonParser("""{"content":{"content":[1,2,3]}}""").fromJson[Container[Container[List[Int]]]] mustEqual obj + } + } +}
\ No newline at end of file diff --git a/src/test/scala/cc/spray/json/LiftedFormatsSpec.scala b/src/test/scala/cc/spray/json/LiftedFormatsSpec.scala deleted file mode 100644 index f98e2d5..0000000 --- a/src/test/scala/cc/spray/json/LiftedFormatsSpec.scala +++ /dev/null @@ -1,40 +0,0 @@ -package cc.spray.json - -import org.specs2.mutable._ -import cc.spray.json.DefaultJsonProtocol._ - -/** - * User: dirk - * Date: 31-08-11 - * Time: 10:01 - */ - -class LiftedFormatsSpec extends Specification { - - case class Container[A](obj: Option[A]) - - implicit def containerWriter[T](implicit writer: JsonWriter[T]) = new JsonWriter[Container[T]] { - import LiftedFormats.liftJsonWriter - def write(obj: Container[T]): JsValue = JsObject(JsField("content", obj.obj.toJson)) - } - - implicit def containerReader[T](implicit reader: JsonReader[T]) = new JsonReader[Container[T]] { - import LiftedFormats.liftJsonReader - def read(value: JsValue): Container[T] = { - value match { - case JsObject(JsField("content", obj: JsValue) :: Nil) => Container(Some(reader.read(obj))) - case _ => throw new DeserializationException("Unexpected format: " + value.toString) - } - } - } - - val obj = Container(Some(Container(Some(List(1, 2, 3))))) - - "The liftJsonWriter" should { - "convert a Container[Container[List[Int]]] to JsValue and back" in { - val r = obj.toJson - r.fromJson[Container[Container[List[Int]]]] - ok - } - } -}
\ No newline at end of file |