diff options
author | Mathias <mathias@spray.cc> | 2011-05-26 22:24:44 +0200 |
---|---|---|
committer | Mathias <mathias@spray.cc> | 2011-05-26 22:24:44 +0200 |
commit | 8dea90d7c9a3ee7a370840aa62a070ca8aacbb2a (patch) | |
tree | c501db1cd76e53188f51dd391508d94289dbcc51 /src | |
parent | 8297c7ae6f1a46df5887e7f3f1d23566b4bd1163 (diff) | |
download | spray-json-8dea90d7c9a3ee7a370840aa62a070ca8aacbb2a.tar.gz spray-json-8dea90d7c9a3ee7a370840aa62a070ca8aacbb2a.tar.bz2 spray-json-8dea90d7c9a3ee7a370840aa62a070ca8aacbb2a.zip |
Add more collection formats
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/cc/spray/json/CollectionFormats.scala | 37 | ||||
-rw-r--r-- | src/test/scala/cc/spray/json/CollectionFormatsSpec.scala | 16 |
2 files changed, 28 insertions, 25 deletions
diff --git a/src/main/scala/cc/spray/json/CollectionFormats.scala b/src/main/scala/cc/spray/json/CollectionFormats.scala index 2bce527..07592a8 100644 --- a/src/main/scala/cc/spray/json/CollectionFormats.scala +++ b/src/main/scala/cc/spray/json/CollectionFormats.scala @@ -17,8 +17,6 @@ package cc.spray.json -import reflect.Manifest - trait CollectionFormats { /** @@ -27,7 +25,7 @@ trait CollectionFormats { implicit def listFormat[T :JsonFormat] = new JsonFormat[List[T]] { def write(list: List[T]) = JsArray(list.map(_.toJson)) def read(value: JsValue) = value match { - case JsArray(elements) => elements.map(_.fromJson) + case JsArray(elements) => elements.map(_.fromJson[T]) case _ => throw new DeserializationException("List expected") } } @@ -35,10 +33,10 @@ trait CollectionFormats { /** * Supplies the JsonFormat for Arrays. */ - implicit def arrayFormat[T :JsonFormat :Manifest] = new JsonFormat[Array[T]] { + implicit def arrayFormat[T :JsonFormat :ClassManifest] = new JsonFormat[Array[T]] { def write(array: Array[T]) = JsArray(array.map(_.toJson).toList) def read(value: JsValue) = value match { - case JsArray(elements) => elements.map(_.fromJson[T]).toArray + case JsArray(elements) => elements.map(_.fromJson[T]).toArray[T] case _ => throw new DeserializationException("Array expected") } } @@ -62,17 +60,22 @@ trait CollectionFormats { } } - /** - * Supplies the JsonFormat for immutable Sets. - */ - implicit def immutableSetFormat[T :JsonFormat] = viaList[Set[T], T](list => Set(list :_*)) - - import collection.mutable.Set - - /** - * Supplies the JsonFormat for mutable Sets. - */ - implicit def mutableSetFormat[T :JsonFormat] = viaList[Set[T], T](list => Set.empty ++ list) + import collection.{immutable => imm} + + implicit def immIterableFormat[T :JsonFormat] = viaList[imm.Iterable[T], T](list => imm.Iterable(list :_*)) + implicit def immSeqFormat[T :JsonFormat] = viaList[imm.Seq[T], T](list => imm.Seq(list :_*)) + implicit def immIndexedSeqFormat[T :JsonFormat] = viaList[imm.IndexedSeq[T], T](list => imm.IndexedSeq(list :_*)) + implicit def immLinearSeqFormat[T :JsonFormat] = viaList[imm.LinearSeq[T], T](list => imm.LinearSeq(list :_*)) + implicit def immSetFormat[T :JsonFormat] = viaList[imm.Set[T], T](list => imm.Set(list :_*)) + implicit def vectorFormat[T :JsonFormat] = viaList[Vector[T], T](list => Vector(list :_*)) + + import collection._ + + implicit def iterableFormat[T :JsonFormat] = viaList[Iterable[T], T](list => Iterable(list :_*)) + implicit def seqFormat[T :JsonFormat] = viaList[Seq[T], T](list => Seq(list :_*)) + implicit def indexedSeqFormat[T :JsonFormat] = viaList[IndexedSeq[T], T](list => IndexedSeq(list :_*)) + implicit def linearSeqFormat[T :JsonFormat] = viaList[LinearSeq[T], T](list => LinearSeq(list :_*)) + implicit def setFormat[T :JsonFormat] = viaList[Set[T], T](list => Set(list :_*)) /** * A JsonFormat construction helper that creates a JsonFormat for an Iterable type I from a builder function @@ -85,5 +88,5 @@ trait CollectionFormats { case _ => throw new DeserializationException("Collection expected") } } - + }
\ No newline at end of file diff --git a/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala b/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala index ba792a3..126b96a 100644 --- a/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala +++ b/src/test/scala/cc/spray/json/CollectionFormatsSpec.scala @@ -51,15 +51,15 @@ class CollectionFormatsSpec extends Specification with DefaultJsonProtocol { json.fromJson[Set[Int]] mustEqual set } } - - "The mutableSetFormat" should { - val set = collection.mutable.Set(1, 2, 3) - val json = JsArray(JsNumber(1), JsNumber(3), JsNumber(2)) - "convert a collection.mutable.Set[Int] to a JsArray of JsNumbers" in { - set.toJson mustEqual json + + "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 collection.mutable.Set[Int]" in { - json.fromJson[collection.mutable.Set[Int]] mustEqual set + "convert a JsArray of JsNumbers to a IndexedSeq[Int]" in { + json.fromJson[collection.IndexedSeq[Int]] mustEqual seq } } |