In spray for scala, get a FromRequestUnmarshaller from an ordinary Unmarshaller -
in scala spray, there way convert unmarshaller[t] fromrequestunmarshaller[t]. i'm stuck trying make entity directive work without using implicits. example:
... } ~ post { path("mypath") { entity(sprayjsonunmarshaller[mycaseclass](mycaseclassrootjsonformat)) { mycaseclass => complete { handle(mycaseclass) } } } ~ ... compiler error:
multiple markers @ line - type mismatch; found : spray.httpx.unmarshalling.unmarshaller[mycaseclass] (which expands to) spray.httpx.unmarshalling.deserializer[spray.http.httpentity,mycaseclass] required: spray.httpx.unmarshalling.fromrequestunmarshaller[?] (which expands to) spray.httpx.unmarshalling.deserializer[spray.http.httprequest,?] - type mismatch; found : spray.httpx.unmarshalling.unmarshaller[mycaseclass] (which expands to) spray.httpx.unmarshalling.deserializer[spray.http.httpentity,mycaseclass] required: spray.httpx.unmarshalling.fromrequestunmarshaller[?] (which expands to) spray.httpx.unmarshalling.deserializer[spray.http.httprequest,?]
in part spray heavily depends on implicit resolution. might not correct, know there simple , elegant way this. designed should following directive: entity(as[mycaseclass]). then, if take @ as[_] directive, there not short way make simple unmarshaller (which takes entity , makes case class) fromrequestunmarshaller (from httprequest -> case class), implicit expanders can found [here] (https://github.com/spray/spray/blob/master/spray-httpx/src/main/scala/spray/httpx/unmarshalling/unmarshallerlifting.scala#l21). when calling entity(as[mycaseclass]) expands this:
entity { as[mycaseclass] { fromrequestunmarshaller[mycaseclass] { frommessageunmarshaller[mycaseclass] { sprayjsonunmarshaller[mycaseclass](mycaseclassrootjsonformat) } } } } if want make explicit, should write in upper form. in way can drop as[mycaseclass]
on other hand may choose other explicit way - extract entity , convert json:
requestinstance { req => val json = req.entity.asstring.parsejson json.convertto(mycaseclassrootjsonformat) }
Comments
Post a Comment