Custom marshaller for custom type

52 views
Skip to first unread message

Mohnish Kodnani

unread,
May 7, 2015, 2:26:49 AM5/7/15
to spray...@googlegroups.com
Hi, 
I have a route where I return an Option of a custom type.Something, on the lines of the following
get {
  path {
    complete {
      Result <-- custom type
    }
  }
}
I defined an marshaller for the type Result 
ToResponseMarshaller[Result] = Marshaller.of[Result](ContentTypes.`application/octet-stream`) { (value, contentType, ctx) =>
val serialized = value.getBytes.copyBytes()
ctx.marshalTo(HttpEntity(contentType, serialized))
}


This works and I can also return Option[Result] and Future[Option[Result]].

Then I have another route, which returns List[Option[Result]]] and that is giving an error saying "Expression of type List[Option[Result]] does not conform to type ToResponseMarshallable.

Do I have to specify how to serialize a list of Result type to make this work ?

Thanks
mohnish

Johannes Rudolph

unread,
May 7, 2015, 5:22:23 AM5/7/15
to spray...@googlegroups.com
Hi Mohnish,

On Thu, May 7, 2015 at 8:26 AM, Mohnish Kodnani
<mohnish...@gmail.com> wrote:
> This works and I can also return Option[Result] and Future[Option[Result]].
>
> Then I have another route, which returns List[Option[Result]]] and that is
> giving an error saying "Expression of type List[Option[Result]] does not
> conform to type ToResponseMarshallable.
>
>
> Do I have to specify how to serialize a list of Result type to make this
> work ?

Yes, exactly. As you noticed we have automatic support for the
`Option` type, where `None` values are rendered to a `404 NotFound`
response [1] (which, in fact, may or may not be what you want). For
marshalling Lists there is no such intuitive behavior and it depends
on the content-type you are rendering to how such a list should be
rendered.

--
Johannes

[1] https://github.com/spray/spray/blob/master/spray-httpx/src/main/scala/spray/httpx/marshalling/MetaToResponseMarshallers.scala#L25

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net
Reply all
Reply to author
Forward
0 new messages