-
Notifications
You must be signed in to change notification settings - Fork 595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix and improve Spray JSON docs #330
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f9798d1
=doc Use reference-style links for linking to the spray-json site
jonas 943d566
=doc Create section on changing the JSON format and reflect reality
jonas ac5d0f2
=doc Rewrite the spray-json section to spell out the list of steps (#…
jonas 590a74e
=doc Format spray JSON docs on single lines
jonas 8064007
=doc Bring back "automatic support for ..."
jonas 360229f
Verify the pretty printed JSON output
jonas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,47 @@ | ||
<a id="akka-http-spray-json"></a> | ||
# JSON Support | ||
|
||
Akka HTTP's @ref[marshalling](marshalling.md#http-marshalling-scala) and @ref[unmarshalling](unmarshalling.md#http-unmarshalling-scala) | ||
infrastructure makes it rather easy to seamlessly support specific wire representations of your data objects, like JSON, | ||
XML or even binary encodings. | ||
Akka HTTP's @ref[marshalling](marshalling.md#http-marshalling-scala) and @ref[unmarshalling](unmarshalling.md#http-unmarshalling-scala) infrastructure makes it rather easy to seamlessly support specific wire representations of your data objects, like JSON, XML or even binary encodings. | ||
|
||
For JSON Akka HTTP currently provides support for [spray-json](https://github.com/spray/spray-json) right out of the box through it's | ||
`akka-http-spray-json` module. | ||
For JSON Akka HTTP currently provides support for [spray-json] right out of the box through its `akka-http-spray-json` module. | ||
|
||
Other JSON libraries are supported by the community. | ||
See [the list of current community extensions for Akka HTTP](http://akka.io/community/#extensions-to-akka-http). | ||
Other JSON libraries are supported by the community. See [the list of current community extensions for Akka HTTP](http://akka.io/community/#extensions-to-akka-http). | ||
|
||
## spray-json Support | ||
|
||
The [SprayJsonSupport](@github@/akka-http-marshallers-scala/akka-http-spray-json/src/main/scala/akka/http/scaladsl/marshallers/sprayjson/SprayJsonSupport.scala) trait provides a `FromEntityUnmarshaller[T]` and `ToEntityMarshaller[T]` for every type `T` | ||
that an implicit `spray.json.RootJsonReader` and/or `spray.json.RootJsonWriter` (respectively) is available for. | ||
|
||
This is how you enable automatic support for (un)marshalling from and to JSON with [spray-json](https://github.com/spray/spray-json): | ||
To enable automatic support for (un)marshalling from and to JSON with [spray-json], add a library dependency onto: | ||
|
||
1. Add a library dependency onto `"com.typesafe.akka" %% "akka-http-spray-json-experimental" % "@version@"`. <!-- FIXME: use preprocessing --> | ||
2. `import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._` or mix in the | ||
`akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport` trait. | ||
3. Provide a `RootJsonFormat[T]` for your type and bring it into scope. | ||
Check out the [spray-json](https://github.com/spray/spray-json) documentation for more info on how to do this. | ||
@@@vars | ||
```sbt | ||
"com.typesafe.akka" %% "akka-http-spray-json-experimental" % "$project.version$"` | ||
``` | ||
@@@ | ||
|
||
Next, provide a `RootJsonFormat[T]` for your type and bring it into scope. Check out the [spray-json] documentation for more info on how to do this. | ||
|
||
Finally, mix in the `akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport` trait as shown in the example below or import the `FromEntityUnmarshaller[T]` and `ToEntityMarshaller[T]` implicits directly from `SprayJsonSupport` | ||
|
||
```scala | ||
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good |
||
|
||
Once you have done this (un)marshalling between JSON and your type `T` should work nicely and transparently. | ||
|
||
@@snip [SprayJsonExampleSpec.scala](../../../../../test/scala/docs/http/scaladsl/SprayJsonExampleSpec.scala) { #minimal-spray-json-example } | ||
|
||
4. By default, spray-json marshals your types to pretty printed json by implicit conversion using PrettyPrinter, as defined in | ||
`implicit def sprayJsonMarshallerConverter[T](writer: RootJsonWriter[T])(implicit printer: JsonPrinter = PrettyPrinter): ToEntityMarshaller[T]`. | ||
Alternately to marshal your types to compact printed json, bring a `CompactPrinter` in scope to perform implicit conversion. | ||
### Pretty printing | ||
|
||
By default, spray-json marshals your types to compact printed JSON by implicit conversion using `CompactPrinter`, as defined in: | ||
|
||
@@snip [SprayJsonSupport.scala](../../../../../../../akka-http-marshallers-scala/akka-http-spray-json/src/main/scala/akka/http/scaladsl/marshallers/sprayjson/SprayJsonSupport.scala) { #sprayJsonMarshallerConverter } | ||
|
||
Alternatively to marshal your types to pretty printed JSON, bring a `PrettyPrinter` in scope to perform implicit conversion. | ||
|
||
@@snip [SprayJsonPrettyMarshalSpec.scala](../../../../../test/scala/docs/http/scaladsl/SprayJsonPrettyMarshalSpec.scala) { #example } | ||
|
||
@@snip [SprayJsonCompactMarshalSpec.scala](../../../../../test/scala/docs/http/scaladsl/SprayJsonCompactMarshalSpec.scala) { #example } | ||
To learn more about how spray-json works please refer to its [documentation][spray-json]. | ||
|
||
To learn more about how spray-json works please refer to its [documentation ](https://github.com/spray/spray-json). | ||
[spray-json]: https://github.com/spray/spray-json |
41 changes: 0 additions & 41 deletions
41
docs/src/test/scala/docs/http/scaladsl/SprayJsonCompactMarshalSpec.scala
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
docs/src/test/scala/docs/http/scaladsl/SprayJsonPrettyMarshalSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com> | ||
*/ | ||
package docs.http.scaladsl | ||
|
||
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport | ||
import akka.http.scaladsl.server.Directives | ||
import org.scalatest.{ Matchers, WordSpec } | ||
|
||
class SprayJsonPrettyMarshalSpec extends server.RoutingSpec { | ||
|
||
"spray-json example" in { | ||
//#example | ||
import spray.json._ | ||
|
||
// domain model | ||
final case class PrettyPrintedItem(name: String, id: Long) | ||
|
||
trait PrettyJsonFormatSupport extends DefaultJsonProtocol with SprayJsonSupport { | ||
implicit val printer = PrettyPrinter | ||
implicit val prettyPrintedItemFormat = jsonFormat2(PrettyPrintedItem) | ||
} | ||
|
||
// use it wherever json (un)marshalling is needed | ||
class MyJsonService extends Directives with PrettyJsonFormatSupport { | ||
|
||
// format: OFF | ||
val route = | ||
get { | ||
pathSingleSlash { | ||
complete { | ||
PrettyPrintedItem("akka", 42) // will render as JSON | ||
} | ||
} | ||
} | ||
// format: ON | ||
} | ||
|
||
val service = new MyJsonService | ||
|
||
// verify the pretty printed JSON | ||
Get("/") ~> service.route ~> check { | ||
responseAs[String] shouldEqual | ||
"""{ | ||
| "name": "akka", | ||
| "id": 42 | ||
|}""".stripMargin | ||
} | ||
//#example | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍