All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hammock.akka.implicits.scala Maven / Gradle / Ivy

The newest version!
package hammock
package akka

import _root_.akka.http.scaladsl.marshalling._
import _root_.akka.http.scaladsl.unmarshalling._
import _root_.akka.http.scaladsl.model.HttpEntity

object implicits {
  implicit def encoderToEntityMarshaller[A: Encoder]: ToEntityMarshaller[A] = Marshaller.strict { a =>
    Encoder[A].encode(a) match {
      case Entity.StringEntity(body, _)    => Marshalling.Opaque(() => HttpEntity(body))
      case Entity.ByteArrayEntity(body, _) => Marshalling.Opaque(() => HttpEntity(body))
      case Entity.EmptyEntity              => Marshalling.Opaque(() => HttpEntity.Empty)
    }
  }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy