diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/scala/cc/spray/json/AdditionalFormats.scala | 62 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/DefaultJsonProtocol.scala | 7 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/ProductFormats.scala (renamed from src/main/scala/cc/spray/json/GenericFormats.scala) | 16 | ||||
-rw-r--r-- | src/main/scala/cc/spray/json/StandardFormats.scala | 11 |
4 files changed, 73 insertions, 23 deletions
diff --git a/src/main/scala/cc/spray/json/AdditionalFormats.scala b/src/main/scala/cc/spray/json/AdditionalFormats.scala new file mode 100644 index 0000000..62a30e7 --- /dev/null +++ b/src/main/scala/cc/spray/json/AdditionalFormats.scala @@ -0,0 +1,62 @@ +/* + * Original implementation (C) 2009-2011 Debasish Ghosh + * Adapted and extended in 2011 by Mathias Doenitz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cc.spray.json + +/** + * Provides additional JsonFormats and helpers + */ +trait AdditionalFormats { + + implicit object JsValueFormat extends JsonFormat[JsValue] { + def write(value: JsValue) = value + def read(value: JsValue) = value + } + + def formatFromWriter[T :JsonWriter] = new JsonFormat[T] { + def write(obj: T) = obj.toJson + def read(value: JsValue) = throw new RuntimeException("JsonFormat constructed from JsonWriter can't read from JSON") + } + + def formatFromReader[T :JsonReader] = new JsonFormat[T] { + def write(obj: T) = throw new RuntimeException("JsonFormat constructed from JsonReader can't write JSON") + def read(value: JsValue) = value.fromJson[T] + } + + /** + * Lazy wrapper around serialization. Useful when you want to serialize (mutually) recursive structures. + */ + def lazyFormat[T](format: => JsonFormat[T]) = new JsonFormat[T]{ + lazy val delegate = format; + def write(x: T) = delegate.write(x); + def read(value: JsValue) = delegate.read(value); + } + + /** + * Wraps an existing JsonReader with Exception protection. + */ + def safeReader[A :JsonReader] = new JsonReader[Either[Exception, A]] { + def read(json: JsValue) = { + try { + Right(json.fromJson) + } catch { + case e: Exception => Left(e) + } + } + } + +}
\ No newline at end of file diff --git a/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala b/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala index a8201c2..6f8daf5 100644 --- a/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala +++ b/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala @@ -20,6 +20,11 @@ package cc.spray.json /** * Provides all the predefined JsonFormats. */ -trait DefaultJsonProtocol extends BasicFormats with StandardFormats with CollectionFormats with GenericFormats +trait DefaultJsonProtocol + extends BasicFormats + with StandardFormats + with CollectionFormats + with ProductFormats + with AdditionalFormats object DefaultJsonProtocol extends DefaultJsonProtocol diff --git a/src/main/scala/cc/spray/json/GenericFormats.scala b/src/main/scala/cc/spray/json/ProductFormats.scala index 8856e02..39b44fb 100644 --- a/src/main/scala/cc/spray/json/GenericFormats.scala +++ b/src/main/scala/cc/spray/json/ProductFormats.scala @@ -18,21 +18,13 @@ package cc.spray.json /** - * Provides the helpers for constructing custom JsonFormat implementations. + * Provides the helpers for constructing custom JsonFormat implementations for types implementing the Product trait + * (especially case classes) */ -trait GenericFormats { +trait ProductFormats { private type JF[T] = JsonFormat[T] // simple alias for reduced verbosity - - /** - * Lazy wrapper around serialization. Useful when you want to serialize mutually recursive structures. - */ - def lazyFormat[T](format: => JF[T]) = new JF[T]{ - lazy val delegate = format; - def write(x: T) = delegate.write(x); - def read(value: JsValue) = delegate.read(value); - } - + def jsonFormat[A :JF, T <: Product](construct: A => T, a: String) = new JF[T]{ def write(p: T) = JsObject( JsField(a, element[A](p, 0).toJson) diff --git a/src/main/scala/cc/spray/json/StandardFormats.scala b/src/main/scala/cc/spray/json/StandardFormats.scala index 20012b4..8b0dc3a 100644 --- a/src/main/scala/cc/spray/json/StandardFormats.scala +++ b/src/main/scala/cc/spray/json/StandardFormats.scala @@ -23,19 +23,10 @@ import scala.{Left, Right} * Provides the JsonFormats for the non-collection standard types. */ trait StandardFormats { + this: AdditionalFormats => private type JF[T] = JsonFormat[T] // simple alias for reduced verbosity - def safeReader[A :JsonFormat] = new JsonReader[Either[Exception, A]] { - def read(json: JsValue) = { - try { - Right(json.fromJson) - } catch { - case e: Exception => Left(e) - } - } - } - implicit def optionFormat[T :JF] = new JF[Option[T]] { def write(option: Option[T]) = option match { case Some(x) => x.toJson |