=htp introduce ToByteStringMarshaller alias and fix fixme

This commit is contained in:
Konrad Malawski 2016-07-29 15:21:19 +02:00
parent f2419f5a08
commit bc536be32c
2 changed files with 7 additions and 5 deletions

View file

@ -8,9 +8,9 @@ import akka.http.scaladsl.util.FastFuture
import akka.util.ByteString
import scala.language.implicitConversions
import akka.http.scaladsl.marshalling.{ ToEntityMarshaller, Marshaller }
import akka.http.scaladsl.unmarshalling.{ FromEntityUnmarshaller, Unmarshaller }
import akka.http.scaladsl.model.{ ContentTypes, MediaTypes, HttpCharsets }
import akka.http.scaladsl.marshalling.{Marshaller, ToByteStringMarshaller, ToEntityMarshaller}
import akka.http.scaladsl.unmarshalling.{FromEntityUnmarshaller, Unmarshaller}
import akka.http.scaladsl.model.{ContentTypes, HttpCharsets, MediaTypes}
import akka.http.scaladsl.model.MediaTypes.`application/json`
import spray.json._
@ -30,7 +30,7 @@ trait SprayJsonSupport {
Unmarshaller.byteStringUnmarshaller.forContentTypes(`application/json`).mapWithCharset { (data, charset)
val input =
if (charset == HttpCharsets.`UTF-8`) ParserInput(data.toArray)
else ParserInput(data.decodeString(charset.nioCharset.name)) // FIXME: identify charset by instance, not by name!
else ParserInput(data.decodeString(charset.nioCharset))
JsonParser(input)
}
@ -40,7 +40,7 @@ trait SprayJsonSupport {
sprayJsValueMarshaller compose writer.write
implicit def sprayJsValueMarshaller(implicit printer: JsonPrinter = CompactPrinter): ToEntityMarshaller[JsValue] =
Marshaller.StringMarshaller.wrap(MediaTypes.`application/json`)(printer)
implicit def sprayByteStringMarshaller[T](implicit writer: RootJsonFormat[T], printer: JsonPrinter = CompactPrinter): Marshaller[T, ByteString] =
implicit def sprayByteStringMarshaller[T](implicit writer: RootJsonFormat[T], printer: JsonPrinter = CompactPrinter): ToByteStringMarshaller[T] =
sprayJsValueMarshaller.map(s ByteString(s.toString)) compose writer.write
}
object SprayJsonSupport extends SprayJsonSupport

View file

@ -6,10 +6,12 @@ package akka.http.scaladsl
import scala.collection.immutable
import akka.http.scaladsl.model._
import akka.util.ByteString
package object marshalling {
//# marshaller-aliases
type ToEntityMarshaller[T] = Marshaller[T, MessageEntity]
type ToByteStringMarshaller[T] = Marshaller[T, ByteString]
type ToHeadersAndEntityMarshaller[T] = Marshaller[T, (immutable.Seq[HttpHeader], MessageEntity)]
type ToResponseMarshaller[T] = Marshaller[T, HttpResponse]
type ToRequestMarshaller[T] = Marshaller[T, HttpRequest]