Skip to content

Instantly share code, notes, and snippets.

@yaroot
Created February 27, 2018 08:57
Show Gist options
  • Save yaroot/fbe60205202e1d3e9ddff72d0acc599d to your computer and use it in GitHub Desktop.
Save yaroot/fbe60205202e1d3e9ddff72d0acc599d to your computer and use it in GitHub Desktop.
trait CirceSupport {
import io.circe.jawn.decode
import akka.http.scaladsl.model.ContentTypes._
implicit def unmarshaller[A: Decoder]: FromEntityUnmarshaller[A] =
Unmarshaller.stringUnmarshaller
.forContentTypes(`application/json`)
.flatMap { _ => _ => body =>
decode[A](body)
.fold(Future.failed, Future.successful)
}
implicit def marshaller[A: Encoder](implicit printer: Printer = Printer.noSpaces): ToEntityMarshaller[A] =
Marshaller.withFixedContentType(`application/json`) { a: A =>
HttpEntity(`application/json`, printer.pretty(a.asJson))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment