summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/scala/cc/spray/json/BasicFormats.scala (renamed from src/main/scala/cc/spray/json/formats/BasicFormats.scala)1
-rw-r--r--src/main/scala/cc/spray/json/CollectionFormats.scala (renamed from src/main/scala/cc/spray/json/formats/CollectionFormats.scala)1
-rw-r--r--src/main/scala/cc/spray/json/DefaultJsonProtocol.scala (renamed from src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala)5
-rw-r--r--src/main/scala/cc/spray/json/GenericFormats.scala (renamed from src/main/scala/cc/spray/json/formats/GenericFormats.scala)21
-rw-r--r--src/main/scala/cc/spray/json/JsValue.scala1
-rw-r--r--src/main/scala/cc/spray/json/JsonFormat.scala (renamed from src/main/scala/cc/spray/json/formats/JsonFormat.scala)1
-rw-r--r--src/main/scala/cc/spray/json/PimpedAny.scala25
-rw-r--r--src/main/scala/cc/spray/json/StandardFormats.scala (renamed from src/main/scala/cc/spray/json/formats/StandardFormats.scala)1
-rw-r--r--src/main/scala/cc/spray/json/package.scala8
-rw-r--r--src/test/scala/cc/spray/json/ReadmeSpec.scala66
-rw-r--r--src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala2
11 files changed, 85 insertions, 47 deletions
diff --git a/src/main/scala/cc/spray/json/formats/BasicFormats.scala b/src/main/scala/cc/spray/json/BasicFormats.scala
index 4be7ef6..e16b4ca 100644
--- a/src/main/scala/cc/spray/json/formats/BasicFormats.scala
+++ b/src/main/scala/cc/spray/json/BasicFormats.scala
@@ -16,7 +16,6 @@
*/
package cc.spray.json
-package formats
/**
* Provides the JsonFormats for the most important Scala types.
diff --git a/src/main/scala/cc/spray/json/formats/CollectionFormats.scala b/src/main/scala/cc/spray/json/CollectionFormats.scala
index 02a9999..2bce527 100644
--- a/src/main/scala/cc/spray/json/formats/CollectionFormats.scala
+++ b/src/main/scala/cc/spray/json/CollectionFormats.scala
@@ -16,7 +16,6 @@
*/
package cc.spray.json
-package formats
import reflect.Manifest
diff --git a/src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala b/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala
index 15f2abb..a8201c2 100644
--- a/src/main/scala/cc/spray/json/formats/DefaultJsonFormats.scala
+++ b/src/main/scala/cc/spray/json/DefaultJsonProtocol.scala
@@ -16,11 +16,10 @@
*/
package cc.spray.json
-package formats
/**
* Provides all the predefined JsonFormats.
*/
-trait DefaultJsonFormats extends BasicFormats with StandardFormats with CollectionFormats with GenericFormats
+trait DefaultJsonProtocol extends BasicFormats with StandardFormats with CollectionFormats with GenericFormats
-object DefaultJsonFormats extends DefaultJsonFormats
+object DefaultJsonProtocol extends DefaultJsonProtocol
diff --git a/src/main/scala/cc/spray/json/formats/GenericFormats.scala b/src/main/scala/cc/spray/json/GenericFormats.scala
index dbfb264..aa1da08 100644
--- a/src/main/scala/cc/spray/json/formats/GenericFormats.scala
+++ b/src/main/scala/cc/spray/json/GenericFormats.scala
@@ -16,7 +16,6 @@
*/
package cc.spray.json
-package formats
/**
* Provides the helpers for constructing custom JsonFormat implementations.
@@ -34,7 +33,7 @@ trait GenericFormats {
def read(value: JsValue) = delegate.read(value);
}
- def format[A :JF, B :JF, T <: Product](construct: (A, B) => T, a: String, b: String) = new JF[T]{
+ def jsonFormat[A :JF, B :JF, T <: Product](construct: (A, B) => T, a: String, b: String) = new JF[T]{
def write(p: T) = JsObject(
JsField(a, element[A](p, 0).toJson),
JsField(b, element[B](p, 1).toJson)
@@ -45,7 +44,7 @@ trait GenericFormats {
)
}
- def format[A :JF, B :JF, C :JF, T <: Product](construct: (A, B, C) => T,
+ def jsonFormat[A :JF, B :JF, C :JF, T <: Product](construct: (A, B, C) => T,
a: String, b: String, c: String) = new JF[T]{
def write(p: T) = JsObject(
JsField(a, element[A](p, 0).toJson),
@@ -59,7 +58,7 @@ trait GenericFormats {
)
}
- def format[A :JF, B :JF, C :JF, D :JF, T <: Product](construct: (A, B, C, D) => T,
+ def jsonFormat[A :JF, B :JF, C :JF, D :JF, T <: Product](construct: (A, B, C, D) => T,
a: String, b: String, c: String, d: String) = new JF[T]{
def write(p: T) = JsObject(
JsField(a, element[A](p, 0).toJson),
@@ -75,7 +74,7 @@ trait GenericFormats {
)
}
- def format[A :JF, B :JF, C :JF, D :JF, E :JF, T <: Product](construct: (A, B, C, D, E) => T,
+ def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, T <: Product](construct: (A, B, C, D, E) => T,
a: String, b: String, c: String, d: String, e: String) = new JF[T]{
def write(p: T) = JsObject(
JsField(a, element[A](p, 0).toJson),
@@ -93,7 +92,7 @@ trait GenericFormats {
)
}
- def format[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, T <: Product](construct: (A, B, C, D, E, F) => T,
+ def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, T <: Product](construct: (A, B, C, D, E, F) => T,
a: String, b: String, c: String, d: String, e: String, f: String) = new JF[T]{
def write(p: T) = JsObject(
JsField(a, element[A](p, 0).toJson),
@@ -113,7 +112,7 @@ trait GenericFormats {
)
}
- def format[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, T <: Product](construct: (A, B, C, D, E, F, G) => T,
+ def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, T <: Product](construct: (A, B, C, D, E, F, G) => T,
a: String, b: String, c: String, d: String, e: String, f: String, g: String) = new JF[T]{
def write(p: T) = JsObject(
JsField(a, element[A](p, 0).toJson),
@@ -135,7 +134,7 @@ trait GenericFormats {
)
}
- def format[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, T <: Product]
+ def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, T <: Product]
(construct: (A, B, C, D, E, F, G, H) => T,
a: String, b: String, c: String, d: String, e: String, f: String, g: String, h: String) = new JF[T]{
def write(p: T) = JsObject(
@@ -160,7 +159,7 @@ trait GenericFormats {
)
}
- def format[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, T <: Product]
+ def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, T <: Product]
(construct: (A, B, C, D, E, F, G, H, I) => T,
a: String, b: String, c: String, d: String, e: String, f: String, g: String, h: String, i: String) = new JF[T]{
def write(p: T) = JsObject(
@@ -187,7 +186,7 @@ trait GenericFormats {
)
}
- def format[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, T <: Product]
+ def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, T <: Product]
(construct: (A, B, C, D, E, F, G, H, I, J) => T, a: String, b: String, c: String, d: String, e: String,
f: String, g: String, h: String, i: String, j: String) = new JF[T]{
def write(p: T) = JsObject(
@@ -216,7 +215,7 @@ trait GenericFormats {
)
}
- def format[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, T <: Product]
+ def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, T <: Product]
(construct: (A, B, C, D, E, F, G, H, I, J, K) => T, a: String, b: String, c: String, d: String, e: String,
f: String, g: String, h: String, i: String, j: String, k: String) = new JF[T]{
def write(p: T) = JsObject(
diff --git a/src/main/scala/cc/spray/json/JsValue.scala b/src/main/scala/cc/spray/json/JsValue.scala
index f98103c..18e302e 100644
--- a/src/main/scala/cc/spray/json/JsValue.scala
+++ b/src/main/scala/cc/spray/json/JsValue.scala
@@ -18,7 +18,6 @@
package cc.spray.json
-import formats._
import collection.mutable.ListBuffer
/**
diff --git a/src/main/scala/cc/spray/json/formats/JsonFormat.scala b/src/main/scala/cc/spray/json/JsonFormat.scala
index 0951ef8..94a2576 100644
--- a/src/main/scala/cc/spray/json/formats/JsonFormat.scala
+++ b/src/main/scala/cc/spray/json/JsonFormat.scala
@@ -16,7 +16,6 @@
*/
package cc.spray.json
-package formats
/**
* Provides the JSON deserialization for type T.
diff --git a/src/main/scala/cc/spray/json/PimpedAny.scala b/src/main/scala/cc/spray/json/PimpedAny.scala
deleted file mode 100644
index 4c10771..0000000
--- a/src/main/scala/cc/spray/json/PimpedAny.scala
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (C) 2009-2011 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
-
-import formats.JsonWriter
-
-private[json] class PimpedAny[T](any: T, writer: JsonWriter[T]) {
-
- def toJson: JsValue = writer.write(any)
-
-} \ No newline at end of file
diff --git a/src/main/scala/cc/spray/json/formats/StandardFormats.scala b/src/main/scala/cc/spray/json/StandardFormats.scala
index 78bbe53..2a07d6c 100644
--- a/src/main/scala/cc/spray/json/formats/StandardFormats.scala
+++ b/src/main/scala/cc/spray/json/StandardFormats.scala
@@ -16,7 +16,6 @@
*/
package cc.spray.json
-package formats
/**
* Provides the JsonFormats for the non-collection standard types.
diff --git a/src/main/scala/cc/spray/json/package.scala b/src/main/scala/cc/spray/json/package.scala
index 362fb7b..a5daedb 100644
--- a/src/main/scala/cc/spray/json/package.scala
+++ b/src/main/scala/cc/spray/json/package.scala
@@ -16,8 +16,6 @@
package cc.spray
-import json.formats.{JsonReader, JsonWriter}
-
package object json {
def jsonReader[T](implicit reader: JsonReader[T]) = reader
@@ -25,4 +23,10 @@ package object json {
implicit def pimpAny[T :JsonWriter](any: T): PimpedAny[T] = new PimpedAny(any, jsonWriter)
+}
+
+package json {
+ private[json] class PimpedAny[T](any: T, writer: JsonWriter[T]) {
+ def toJson: JsValue = writer.write(any)
+ }
} \ No newline at end of file
diff --git a/src/test/scala/cc/spray/json/ReadmeSpec.scala b/src/test/scala/cc/spray/json/ReadmeSpec.scala
new file mode 100644
index 0000000..8669c0d
--- /dev/null
+++ b/src/test/scala/cc/spray/json/ReadmeSpec.scala
@@ -0,0 +1,66 @@
+package cc.spray.json
+
+import org.specs.Specification
+
+class ReadmeSpec extends Specification {
+
+ "The Usage snippets" should {
+ "behave as expected" in {
+ import DefaultJsonProtocol._
+
+ val json = """{ "some": "JSON source" }"""
+ val jsonAst = JsonParser(json)
+ jsonAst mustEqual JsObject(JsField("some", "JSON source"))
+
+ val json2 = PrettyPrinter(jsonAst)
+ json2 mustEqual
+ """{
+ | "some": "JSON source"
+ |}""".stripMargin
+
+ val jsonAst2 = List(1, 2, 3).toJson
+ jsonAst2 mustEqual JsArray(JsNumber(1), JsNumber(2), JsNumber(3))
+ }
+ }
+
+ case class Color(name: String, red: Int, green: Int, blue: Int)
+
+ "The case class example" should {
+ "behave as expected" in {
+ object MyJsonProtocol extends DefaultJsonProtocol {
+ implicit val colorFormat = jsonFormat(Color, "name", "red", "green", "blue")
+ }
+ import MyJsonProtocol._
+
+ val json = Color("CadetBlue", 95, 158, 160).toJson
+ val color = json.fromJson[Color]
+
+ color mustEqual Color("CadetBlue", 95, 158, 160)
+ }
+ }
+
+ "The non case class example" should {
+ "behave as expected" in {
+ object MyJsonProtocol extends DefaultJsonProtocol {
+ implicit object ColorJsonFormat extends JsonFormat[Color] {
+ def write(c: Color) = {
+ JsArray(JsString(c.name), JsNumber(c.red), JsNumber(c.green), JsNumber(c.blue))
+ }
+ def read(value: JsValue) = value match {
+ case JsArray(JsString(name) :: JsNumber(red) :: JsNumber(green) :: JsNumber(blue) :: Nil) => {
+ new Color(name, red.toInt, green.toInt, blue.toInt)
+ }
+ case _ => throw new DeserializationException("Color expected")
+ }
+ }
+ }
+ import MyJsonProtocol._
+
+ val json = Color("CadetBlue", 95, 158, 160).toJson
+ val color = json.fromJson[Color]
+
+ color mustEqual Color("CadetBlue", 95, 158, 160)
+ }
+ }
+
+} \ No newline at end of file
diff --git a/src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala b/src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala
index adc6fcb..60f4494 100644
--- a/src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala
+++ b/src/test/scala/cc/spray/json/formats/GenericFormatsSpec.scala
@@ -6,7 +6,7 @@ import org.specs.Specification
class GenericFormatsSpec extends Specification with GenericFormats with BasicFormats {
case class Test2(a: Int, b: Double)
- implicit val test2Format = format(Test2, "a", "b")
+ implicit val test2Format = jsonFormat(Test2, "a", "b")
"A JsonFormat created with format, for a case class with 2 elements," should {
val obj = Test2(42, 4.2)