Created
November 19, 2014 02:30
Revisions
-
chadselph created this gist
Nov 19, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.scala.DefaultScalaModule import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper import spray.http.{ContentTypes, HttpCharsets, HttpEntity, MediaTypes} import spray.httpx.marshalling.Marshaller import spray.httpx.unmarshalling.Unmarshaller /** * Use Jackson directly to avoid json4s's dependencies */ trait JacksonJsonSupport { val jacksonModules = Seq(DefaultScalaModule) val mapper = new ObjectMapper() with ScalaObjectMapper mapper.registerModules(jacksonModules:_*) implicit def jacksonJsonUnmarshaller[T : Manifest] = Unmarshaller[T](MediaTypes.`application/json`) { case x: HttpEntity.NonEmpty => val jsonSource = x.asString(defaultCharset = HttpCharsets.`UTF-8`) mapper.readValue[T](jsonSource) } implicit def jacksonJsonMarshaller[T <: AnyRef] = Marshaller.delegate[T, String](ContentTypes.`application/json`)(mapper.writeValueAsString(_)) }