-
Notifications
You must be signed in to change notification settings - Fork 0
Marshalling and Unmarshalling
HTTP requests and responses can have an "entity body", which can be text or binary content. However, when consuming requests and producing responses in your application you normally want to work with your domain objects rather than low level representations in XML, JSON or other serialization formats.
spray supports decoupling your application logic from these serialization concerns by separating out marshalling (converting domain objects to a serialized format) and unmarshalling (creating domain objects from a serialized format) in the way of type classes.
Suppose your application provides actions upon Person resources, which are represented in your code like this:
case class Person(name: string, firstname: String, age: int)A part of your spray service might be defined like this:
path("person" / IntNumber) { id =>
get { ctx =>
val person: Person = ... // load Person from the DB using the id
ctx.complete(person) // uses the in-scope Marshaller to convert the
// Person into a format accepted by the client
} ~
put {
content(as[Person]) { person =>
... // operate directly on the Person object,
// e.g. write it to the database
}
}
}When you use the RequestContext complete method with an argument of a custom type the Scala compiler will look
for an in-scope implicit Marshaller for your type to do the job of converting your custom object to a representation
accepted by the client. spray comes with the following marshallers already defined (as implicit objects in the
DefaultMarshallers trait):
- Array[Byte] to
application/octet-stream - Array[Char] to media type
text/plain - String to media type
text/plain - NodeSeq to
text/xml,text/htmlorapplication/xhtml+xml -
FormData to
application/x-www-form-urlencoded -
MultipartFormData to
multipart/form-data - [MultipartContent] to
multipart/mixed - Option[T] to a the respective type for
Tor a404 Not Foundresponse if undefined - Either[A, B] to the respective response of either
AorB - Future[T] to the respective response of
T - Stream[T] to the respective streaming response (see Streaming)
- Throwable to the respective error response
-
twirl.api.{Xml, Txt, Html}to the respective mediatype (with TwirlSupport - Any type
Ttoapplication/json(with [SprayJsonSupport] or LiftJsonSupport)
Creating your own marshallers for custom types is also not hard at all. For guidance take a look at the implementation of the built-in marshallers in DefaultMarshallers trait.
The opposite of marshalling is unmarshalling, i.e. the creation of custom objects from XML, JSON and the like.
This job is done by Unmarshaller instances, which just like Marshaller instances, are really easy to write for your
custom types and need to be available implicitly.
spray comes with the following unmarshallers already defined (as implicit vals in the
DefaultUnmarshallers trait):
- Media type
application/octet-streamto Array[Byte] - Media type
text/plainto Array[Char] - Media type
text/plainto String -
text/xml,text/htmlorapplication/xhtml+xmlto NodeSeq -
application/x-www-form-urlencodedto FormData -
multipart/form-datato MultipartFormData -
multipart/mixedto [MultipartContent] -
application/jsonto any typeT(with [SprayJsonSupport] or LiftJsonSupport)
- Home
- Requirements
- spray-server
- ... Getting Started
- ... Key Concepts
- ...... Request Lifecycle
- ...... Routes
- ...... Directives
- ...... Composing Directives
- ...... Rejections
- ...... Marshalling and Unmarshalling
- ... Predefined Directives
- ...... Method Filters
- ...... Path Filters
- ...... Parameter Filters
- ...... Form-Field Filters
- ...... Marshalling/Unmarshalling
- ...... Caching
- ...... Detach
- ...... Encoding/Decoding
- ...... Authentication/Authorization
- ...... File and Resource Directives
- ...... Misc Directives
- ... Advanced Topics
- ...... Case Class Extraction
- ...... Custom Directives
- ...... Custom Media Types
- ...... Custom Error Responses
- ... Configuration
- ... Testing
- ... Example Projects
- spray-client
- Patch Policy
- Credits
- Sponsors