2015-05-11 23:05:18 +02:00
|
|
|
JSON Support
|
|
|
|
|
============
|
|
|
|
|
|
2015-07-17 16:18:18 +02:00
|
|
|
Akka HTTP's :ref:`marshalling <http-marshalling-scala>` and :ref:`unmarshalling <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`_ right out of the box through it's
|
|
|
|
|
``akka-http-spray-json`` module.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
spray-json Support
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
The SprayJsonSupport_ 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`_:
|
|
|
|
|
|
|
|
|
|
1. Add a library dependency onto ``"com.typesafe.akka" %% "akka-http-spray-json-experimental" % "1.x"``.
|
|
|
|
|
|
|
|
|
|
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`_ documentation for more info on how to do this.
|
|
|
|
|
|
|
|
|
|
Once you have done this (un)marshalling between JSON and your type ``T`` should work nicely and transparently.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. _spray-json: https://github.com/spray/spray-json
|
|
|
|
|
.. _SprayJsonSupport: @github@/akka-http-marshallers-scala/akka-http-spray-json/src/main/scala/akka/http/scaladsl/marshallers/sprayjson/SprayJsonSupport.scala
|