pepegar / hammock
1
package hammock
2
package akka
3

4
import _root_.akka.http.scaladsl.marshalling._
5
import _root_.akka.http.scaladsl.unmarshalling._
6
import _root_.akka.http.scaladsl.model.HttpEntity
7

8
object implicits {
9 0
  implicit def encoderToEntityMarshaller[A: Encoder]: ToEntityMarshaller[A] = Marshaller.strict { a =>
10 0
    Encoder[A].encode(a) match {
11 0
      case Entity.StringEntity(body, _)    => Marshalling.Opaque(() => HttpEntity(body))
12 0
      case Entity.ByteArrayEntity(body, _) => Marshalling.Opaque(() => HttpEntity(body))
13 0
      case Entity.EmptyEntity              => Marshalling.Opaque(() => HttpEntity.Empty)
14
    }
15
  }
16

17
  implicit def decoderFromEntityUnmarshaller[A: Decoder]: FromEntityUnmarshaller[A] =
18
    PredefinedFromEntityUnmarshallers.stringUnmarshaller
19 0
      .map(str => Decoder[A].decode(Entity.StringEntity(str)))
20 0
      .map(_.fold(throw _, identity))
21
}

Read our documentation on viewing source code .

Loading