diff options
author | Mathias <mathias@spray.cc> | 2011-11-30 16:07:43 +0100 |
---|---|---|
committer | Mathias <mathias@spray.cc> | 2011-11-30 16:15:12 +0100 |
commit | 938576d2859f3749a357c96ac7a9be6c5fd4ec7e (patch) | |
tree | 9da1dfc0ae49831927acb57cfb66bc66f10cc85c /src/main/scala/cc/spray/json/AdditionalFormats.scala | |
parent | cf840bcbe341a2976ea8ea9afefa157bf426e9f8 (diff) | |
download | spray-json-938576d2859f3749a357c96ac7a9be6c5fd4ec7e.tar.gz spray-json-938576d2859f3749a357c96ac7a9be6c5fd4ec7e.tar.bz2 spray-json-938576d2859f3749a357c96ac7a9be6c5fd4ec7e.zip |
Add RootJsonReader and RootJsonWriter as well as some helper methods, closes #10
Diffstat (limited to 'src/main/scala/cc/spray/json/AdditionalFormats.scala')
-rw-r--r-- | src/main/scala/cc/spray/json/AdditionalFormats.scala | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/main/scala/cc/spray/json/AdditionalFormats.scala b/src/main/scala/cc/spray/json/AdditionalFormats.scala index dc78115..889fdaf 100644 --- a/src/main/scala/cc/spray/json/AdditionalFormats.scala +++ b/src/main/scala/cc/spray/json/AdditionalFormats.scala @@ -36,6 +36,12 @@ trait AdditionalFormats { } /** + * Constructs a RootJsonFormat from its two parts, RootJsonReader and RootJsonWriter. + */ + def rootJsonFormat[T](reader: RootJsonReader[T], writer: RootJsonWriter[T]) = + rootFormat(jsonFormat(reader, writer)) + + /** * Turns a JsonWriter into a JsonFormat that throws an UnsupportedOperationException for reads. */ def lift[T](writer :JsonWriter[T]) = new JsonFormat[T] { @@ -45,6 +51,12 @@ trait AdditionalFormats { } /** + * Turns a RootJsonWriter into a RootJsonFormat that throws an UnsupportedOperationException for reads. + */ + def lift[T](writer :RootJsonWriter[T]): RootJsonFormat[T] = + rootFormat(lift(writer :JsonWriter[T])) + + /** * Turns a JsonReader into a JsonFormat that throws an UnsupportedOperationException for writes. */ def lift[T <: AnyRef](reader :JsonReader[T]) = new JsonFormat[T] { @@ -54,15 +66,29 @@ trait AdditionalFormats { } /** + * Turns a RootJsonReader into a RootJsonFormat that throws an UnsupportedOperationException for writes. + */ + def lift[T <: AnyRef](reader :RootJsonReader[T]): RootJsonFormat[T] = + rootFormat(lift(reader :JsonReader[T])) + + /** * Lazy wrapper around serialization. Useful when you want to serialize (mutually) recursive structures. */ - def lazyFormat[T](format: => JsonFormat[T]) = new JsonFormat[T]{ + 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); } /** + * Explicitly turns a JsonFormat into a RootJsonFormat. + */ + def rootFormat[T](format: JsonFormat[T]) = new RootJsonFormat[T] { + def write(obj: T) = format.write(obj) + def read(json: JsValue) = format.read(json) + } + + /** * Wraps an existing JsonReader with Exception protection. */ def safeReader[A :JsonReader] = new JsonReader[Either[Exception, A]] { |