Instead of using TurnJsonIntoHttp in each place:
class MyEndpoint extends Endpoint[HttpRequest, HttpResponse] {
def route = {
case Method.Get -> Root / "items" ⇒
List() ! TurnJsonIntoHttp[List[MyObj]]
case Method.Get -> Root / "item" / Long(id) ⇒
GetDetail(id) ! TurnJsonIntoHttp[MyObj]
//...
}
}
==========
We could do something like:
implicit class ToJsonOps[Req, Resp](service: Service[Req, Resp]) {
def asJson(implicit encode: EncodeJson[Resp]) =
new Service[Req, HttpResponse] {
def apply(req: Req) = service(req) flatMap TurnJsonIntoHttp[Resp]
}
}
And in route:
class MyEndpoint extends Endpoint[HttpRequest, HttpResponse] {
def route = {
case Method.Get -> Root / "items" ⇒
List().asJson
case Method.Get -> Root / "item" / Long(id) ⇒
GetDetail(id).asJson
//...
}
}
Yes @vkostyukov, that's the case.
Before 0.2 I was using like you said:
With the new implicit encoder/decoder I haven't found a reason to keep the endpoint returning
Json
(which in my case was a simple String).