summaryrefslogtreecommitdiff
path: root/README.markdown
diff options
context:
space:
mode:
authorMathias <mathias@spray.cc>2011-10-04 10:18:24 +0200
committerMathias <mathias@spray.cc>2011-10-04 10:18:24 +0200
commite95eb217857ebf6eff343b4ed14815db804d9009 (patch)
tree4e5b4b3cf7a2160e25752236da3821ace2aa46fb /README.markdown
parent24af701d7e3bce6f72bb8b2f12c6099f3d85403f (diff)
downloadspray-json-e95eb217857ebf6eff343b4ed14815db804d9009.tar.gz
spray-json-e95eb217857ebf6eff343b4ed14815db804d9009.tar.bz2
spray-json-e95eb217857ebf6eff343b4ed14815db804d9009.zip
Prepare for 1.0.1 releasev1.0.1
Diffstat (limited to 'README.markdown')
-rw-r--r--README.markdown37
1 files changed, 25 insertions, 12 deletions
diff --git a/README.markdown b/README.markdown
index 789182a..fcf4ba9 100644
--- a/README.markdown
+++ b/README.markdown
@@ -11,12 +11,11 @@ It sports the following features:
### Installation
_spray-json_ is available from the [scala-tools.org] repositories.
-The latest release is `1.0.0`.
-It's built against Scala 2.9.0-1, but backporting to 2.8.1 is no problem (let me know if you need a 2.8.1 compatible version).
+The latest release is `1.0.1` and is built against Scala 2.9.1.
-If you use SBT you can include _spray-json_ in your project with
+If you use SBT (0.7.x) you can include _spray-json_ in your project with
- val sprayJson = "cc.spray.json" %% "spray-json" % "1.0.0" % "compile" withSources()
+ val sprayJson = "cc.spray.json" %% "spray-json" % "1.0.1" % "compile" withSources()
_spray-json_ has only one dependency: the parsing library [parboiled]
(which is also a dependency of _spray-server_ and _spray-client_, so if you use _spray-json_ with either of them you are not incurring any additional dependency).
@@ -35,11 +34,11 @@ and do one or more of the following:
val json = """{ "some": "JSON source" }"""
val jsonAst = JsonParser(json)
-
+
2. Print a JSON AST back to a String using either the `CompactPrinter` or the `PrettyPrinter`
val json = PrettyPrinter(jsonAst)
-
+
3. Convert any Scala object to a JSON AST using the pimped `toJson` method
val jsonAst = List(1, 2, 3).toJson
@@ -48,23 +47,22 @@ and do one or more of the following:
val myObject = jsonAst.fromJson[MyObjectType]
-
In order to make steps 3 and 4 work for an object of type `T` you need to bring implicit values in scope that
-provide `JsonFormat[T]`s for `T` and all types used by `T` (directly or indirectly).
+provide `JsonFormat[T]` instances for `T` and all types used by `T` (directly or indirectly).
The way you normally do this is via a "JsonProtocol".
### JsonProtocol
-_spray-json_ uses [SJSON]s Scala-idiomatic type-class-based approach to connect an existing type `T` with the logic how to (de)serialize its instances to and from JSON. (In fact _spray-json_ even reuses [SJSON] code, see the 'Credits' section below).
+_spray-json_ uses [SJSON]s Scala-idiomatic type-class-based approach to connect an existing type `T` with the logic how to (de)serialize its instances to and from JSON. (In fact _spray-json_ even reuses some of [SJSON]s code, see the 'Credits' section below).
This approach has the advantage of not requiring any change (or even access) to `T`s source code. All (de)serialization
logic is attached 'from the outside'. There is no reflection involved, so the resulting conversions are fast. Scalas excellent type inference reduces verbosity and boilerplate to a minimum, while the Scala compiler will make sure at compile time that you provided all required (de)serialization logic.
-In _spray-jsons_ terminology a 'JsonProtocol' is nothing but a bunch of implicit values of type `JsonFormat[T]`, whereby each `JsonFormat[T]` contains the logic of how to convert instance of `T` to and from JSON. All `JsonFormat[T]`s of a protocol need to be "MECE" (mutually exclusive, collectively exhaustive), i.e. they are not allowed to overlap and together need to span all types required by the application.
+In _spray-jsons_ terminology a 'JsonProtocol' is nothing but a bunch of implicit values of type `JsonFormat[T]`, whereby each `JsonFormat[T]` contains the logic of how to convert instance of `T` to and from JSON. All `JsonFormat[T]`s of a protocol need to be "mece" (mutually exclusive, collectively exhaustive), i.e. they are not allowed to overlap and together need to span all types required by the application.
This may sound more complicated than it is.
-_spray-json_ comes with a `DefaultJsonProtocol` which already covers all of Scalas value types as well as the most important reference and collection types. As long as your code uses nothing more than these you only need the `DefaultJsonProtocol`.
+_spray-json_ comes with a `DefaultJsonProtocol`, which already covers all of Scalas value types as well as the most important reference and collection types. As long as your code uses nothing more than these you only need the `DefaultJsonProtocol`.
Here are the types already taken care of by the `DefaultJsonProtocol`:
* Byte, Short, Int, Long, Float, Double, Char, Unit, Boolean
@@ -96,9 +94,18 @@ If your custom type `T` is a case class then augmenting the `DefaultJsonProtocol
The `jsonFormat` method reduces the boilerplate to a minimum, just pass it the companion object of your case class as
well as the field names (in order) and it will return a ready-to-use `JsonFormat` for your type.
+There is one quirk though: If you explicitly declare the companion object for your case class the notation above will
+stop working. You'll have to explicitly refer to the companion objects `apply` method to fix this:
+
+ case class Color(name: String, red: Int, green: Int, blue: Int)
+ object Color
+
+ object MyJsonProtocol extends DefaultJsonProtocol {
+ implicit val colorFormat = jsonFormat(Color.apply, "name", "red", "green", "blue")
+ }
If your case class is generic in that it takes type parameters itself the `jsonFormat` method can also help you.
-However, there is a little something more boilerplate required as you need to add context bounds for all type parameters
+However, there is a little more boilerplate required as you need to add context bounds for all type parameters
and explicitly refer to the case classes `apply` method as in this example:
case class NamedList[A](name: String, items: List[A])
@@ -165,6 +172,12 @@ This is a bit more verbose in its definition and the resulting JSON but transpor
Note that this is the approach _spray-json_ uses for case classes.
+### API Documentation
+
+You can find the documentation for the _spray-json_ API here:
+<http://spray.github.com/spray/api/spray-json/>
+
+
### Credits
Most of type-class (de)serialization code is nothing but a polished copy of what **Debasish Ghosh** made available