So I am stuck here:
I create serializer object:
import _root_.java.lang.reflect.{Type, ParameterizedType}
import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.{CaseClassModule, JacksonModule}
import spray.routing.{RequestEntityExpectedRejection, Directive}
import shapeless._
import spray.routing.directives.BasicDirectives._
import spray.routing.directives.MiscDirectives._
import spray.routing.UnsupportedRequestContentTypeRejection
object JacksonSerializer {
val module = new JacksonModule with CaseClassModule
val mapper = new ObjectMapper
mapper.registerModule(module)
def serialize[T](src: T): String = {
mapper.writeValueAsString(src)
}
def deserialize[T: Manifest](src: String): T = {
mapper.readValue(src, typeReference[T])
}
private[this] def typeReference[T: Manifest] = new TypeReference[T] {
override def getType = typeFromManifest(manifest[T])
}
private[this] def typeFromManifest(m: Manifest[_]): Type = {
if (m.typeArguments.isEmpty) {
m.erasure
}
else new ParameterizedType {
def getRawType = m.erasure
def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
def getOwnerType = null
}
}
def jacksonEntity[T: Manifest]: Directive[T :: HNil] = {
extract(_.request.entity.asString).flatMap[T :: HNil] {
case value: String => provide(deserialize[T](value))
} & cancelAllRejections(ofTypes(RequestEntityExpectedRejection.getClass, classOf[UnsupportedRequestContentTypeRejection]))
}
}
and trying to use it as
def signup =
path("signup") {
post {
jacksonEntity[RegistrationData] {
case regData => complete("Ok")
}
}
}
at this point it doesn't compile because of
scala: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: Manifest[RegistrationData]
jacksonEntity[RegistrationData] {
^
Please help