Skip to content

Instantly share code, notes, and snippets.

@takezoe
Last active July 18, 2016 04:42
Show Gist options
  • Save takezoe/18a4fe62e5c6e89337eb7715bde7743e to your computer and use it in GitHub Desktop.
Save takezoe/18a4fe62e5c6e89337eb7715bde7743e to your computer and use it in GitHub Desktop.
JacksonSupport for Akka-HTTP
package com.github.takezoe.akka.http.jackson
import akka.http.scaladsl.marshalling.{Marshaller, _}
import akka.http.scaladsl.model.{HttpEntity, HttpRequest}
import akka.http.scaladsl.model.MediaTypes._
import akka.http.scaladsl.unmarshalling._
import akka.stream.Materializer
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.core.Version
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._
import scala.reflect.ClassTag
import scala.language.postfixOps
object JacksonSupport {
private val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
implicit def JacksonMarshaller: ToEntityMarshaller[AnyRef] = {
Marshaller.withFixedContentType(`application/json`) { obj =>
HttpEntity(`application/json`, mapper.writeValueAsString(obj).getBytes("UTF-8"))
}
}
implicit def JacksonUnmarshaller[T <: AnyRef](implicit c: ClassTag[T]): FromRequestUnmarshaller[T] = {
new FromRequestUnmarshaller[T]{
override def apply(request: HttpRequest)(implicit ec: ExecutionContext, materializer: Materializer): Future[T] = {
request.entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8")).map { str =>
mapper.readValue(str, c.runtimeClass).asInstanceOf[T]
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment