diff options
Diffstat (limited to 'src/main/scala/cc/spray')
4 files changed, 21 insertions, 16 deletions
diff --git a/src/main/scala/cc/spray/json/AdditionalFormats.scala b/src/main/scala/cc/spray/json/AdditionalFormats.scala index db9d3ba..dc78115 100644 --- a/src/main/scala/cc/spray/json/AdditionalFormats.scala +++ b/src/main/scala/cc/spray/json/AdditionalFormats.scala @@ -68,7 +68,7 @@ trait AdditionalFormats { def safeReader[A :JsonReader] = new JsonReader[Either[Exception, A]] { def read(json: JsValue) = { try { - Right(json.fromJson) + Right(json.convertTo) } catch { case e: Exception => Left(e) } diff --git a/src/main/scala/cc/spray/json/CollectionFormats.scala b/src/main/scala/cc/spray/json/CollectionFormats.scala index 07592a8..bcfa198 100644 --- a/src/main/scala/cc/spray/json/CollectionFormats.scala +++ b/src/main/scala/cc/spray/json/CollectionFormats.scala @@ -25,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[T]) + case JsArray(elements) => elements.map(_.convertTo[T]) case _ => throw new DeserializationException("List expected") } } @@ -36,7 +36,7 @@ trait CollectionFormats { 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[T] + case JsArray(elements) => elements.map(_.convertTo[T]).toArray[T] case _ => throw new DeserializationException("Array expected") } } @@ -55,7 +55,7 @@ trait CollectionFormats { } } def read(value: JsValue) = value match { - case JsObject(fields) => fields.map(field => (JsString(field.name).fromJson[K], field.value.fromJson[V])).toMap + case JsObject(fields) => fields.map(field => (JsString(field.name).convertTo[K], field.value.convertTo[V])).toMap case _ => throw new DeserializationException("Map expected") } } @@ -84,7 +84,7 @@ trait CollectionFormats { def viaList[I <: Iterable[T], T :JsonFormat](f: List[T] => I): JsonFormat[I] = new JsonFormat[I] { def write(iterable: I) = JsArray(iterable.map(_.toJson).toList) def read(value: JsValue) = value match { - case JsArray(elements) => f(elements.map(_.fromJson[T])) + case JsArray(elements) => f(elements.map(_.convertTo[T])) case _ => throw new DeserializationException("Collection expected") } } diff --git a/src/main/scala/cc/spray/json/JsValue.scala b/src/main/scala/cc/spray/json/JsValue.scala index e4ab734..86d4101 100644 --- a/src/main/scala/cc/spray/json/JsValue.scala +++ b/src/main/scala/cc/spray/json/JsValue.scala @@ -24,9 +24,14 @@ import collection.mutable.ListBuffer * The general type of a JSON AST node. */ sealed trait JsValue { - override def toString = CompactPrinter(this) + override def toString = compactPrint def toString(printer: (JsValue => String)) = printer(this) - def fromJson[T :JsonReader]: T = jsonReader[T].read(this) + def compactPrint = CompactPrinter(this) + def prettyPrint = PrettyPrinter(this) + def convertTo[T :JsonReader]: T = jsonReader[T].read(this) + + @deprecated("Superceded by 'convertTo'", "1.1.0") + def fromJson[T :JsonReader]: T = convertTo } object JsValue { diff --git a/src/main/scala/cc/spray/json/StandardFormats.scala b/src/main/scala/cc/spray/json/StandardFormats.scala index 1c44716..56fb658 100644 --- a/src/main/scala/cc/spray/json/StandardFormats.scala +++ b/src/main/scala/cc/spray/json/StandardFormats.scala @@ -36,7 +36,7 @@ trait StandardFormats { } def read(value: JsValue) = value match { case JsNull => None - case x => Some(x.fromJson[T]) + case x => Some(x.convertTo[T]) } } @@ -45,7 +45,7 @@ trait StandardFormats { case Right(a) => a.toJson case Left(b) => b.toJson } - def read(value: JsValue) = (value.fromJson(safeReader[A]), value.fromJson(safeReader[B])) match { + def read(value: JsValue) = (value.convertTo(safeReader[A]), value.convertTo(safeReader[B])) match { case (Right(a), _: Left[_, _]) => Left(a) case (_: Left[_, _], Right(b)) => Right(b) case (_: Right[_, _], _: Right[_, _]) => throw new DeserializationException("Ambiguous Either value: can be read as both, Left and Right, values") @@ -55,13 +55,13 @@ trait StandardFormats { implicit def tuple1Format[A :JF] = new JF[Tuple1[A]] { def write(t: Tuple1[A]) = t._1.toJson - def read(value: JsValue) = Tuple1(value.fromJson[A]) + def read(value: JsValue) = Tuple1(value.convertTo[A]) } implicit def tuple2Format[A :JF, B :JF] = new JF[(A, B)] { def write(t: (A, B)) = JsArray(t._1.toJson, t._2.toJson) def read(value: JsValue) = value match { - case JsArray(a :: b :: Nil) => (a.fromJson[A], b.fromJson[B]) + case JsArray(a :: b :: Nil) => (a.convertTo[A], b.convertTo[B]) case _ => throw new DeserializationException("Tuple2 expected") } } @@ -69,7 +69,7 @@ trait StandardFormats { implicit def tuple3Format[A :JF, B :JF, C :JF] = new JF[(A, B, C)] { def write(t: (A, B, C)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson) def read(value: JsValue) = value match { - case JsArray(a :: b :: c :: Nil) => (a.fromJson[A], b.fromJson[B], c.fromJson[C]) + case JsArray(a :: b :: c :: Nil) => (a.convertTo[A], b.convertTo[B], c.convertTo[C]) case _ => throw new DeserializationException("Tuple3 expected") } } @@ -77,7 +77,7 @@ trait StandardFormats { implicit def tuple4Format[A :JF, B :JF, C :JF, D :JF] = new JF[(A, B, C, D)] { def write(t: (A, B, C, D)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson, t._4.toJson) def read(value: JsValue) = value match { - case JsArray(a :: b :: c :: d :: Nil) => (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D]) + case JsArray(a :: b :: c :: d :: Nil) => (a.convertTo[A], b.convertTo[B], c.convertTo[C], d.convertTo[D]) case _ => throw new DeserializationException("Tuple4 expected") } } @@ -87,7 +87,7 @@ trait StandardFormats { def write(t: (A, B, C, D, E)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson, t._4.toJson, t._5.toJson) def read(value: JsValue) = value match { case JsArray(a :: b :: c :: d :: e :: Nil) => { - (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D], e.fromJson[E]) + (a.convertTo[A], b.convertTo[B], c.convertTo[C], d.convertTo[D], e.convertTo[E]) } case _ => throw new DeserializationException("Tuple5 expected") } @@ -99,7 +99,7 @@ trait StandardFormats { def write(t: (A, B, C, D, E, F)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson, t._4.toJson, t._5.toJson, t._6.toJson) def read(value: JsValue) = value match { case JsArray(a :: b :: c :: d :: e :: f :: Nil) => { - (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D], e.fromJson[E], f.fromJson[F]) + (a.convertTo[A], b.convertTo[B], c.convertTo[C], d.convertTo[D], e.convertTo[E], f.convertTo[F]) } case _ => throw new DeserializationException("Tuple6 expected") } @@ -111,7 +111,7 @@ trait StandardFormats { def write(t: (A, B, C, D, E, F, G)) = JsArray(t._1.toJson, t._2.toJson, t._3.toJson, t._4.toJson, t._5.toJson, t._6.toJson, t._6.toJson) def read(value: JsValue) = value match { case JsArray(a :: b :: c :: d :: e :: f :: g :: Nil) => { - (a.fromJson[A], b.fromJson[B], c.fromJson[C], d.fromJson[D], e.fromJson[E], f.fromJson[F], g.fromJson[G]) + (a.convertTo[A], b.convertTo[B], c.convertTo[C], d.convertTo[D], e.convertTo[E], f.convertTo[F], g.convertTo[G]) } case _ => throw new DeserializationException("Tuple7 expected") } |