Handling JSON-RPC requests in a spray (Scala)

Any handy json-rpc request handler in Spray ?

Let's say there is a request to process the jsonrpc2.0 route and sort it in some case class using the directive entity

:

case class RPCRequest(jsonrpc: String, method: String, params: String, id: Int) 

// .....

  post {
    entity(as[RPCRequest]) { request =>
      complete {
        s"Method: ${request.method}"
      }
    }
  }

      

When app / json asks for proceeds:

{"jsonrpc": "2.0", "method": "create", "params": {"token": "dhasgddkgakhadajdasdad237843848234=", "formType": "test2", "channel": "direct", "extra": "adsdasds"}, "id": 3}

      

it params

must be bound from json to string and throws an error:

Request content garbled: Expected string as JsString but received {"token": "dhasgddkgakhadajdasdad237843848234 =", "formType": "test2", "channel": "direct", "extra": "adsdasds"}

If marshall is params

for a specific case class (say FormOperation

):

abstract class FormOperation
case class FormOperationCreate(order: String, sign: String) extends FormOperation
case class FormOperationOpen(token: String, formType: String, channel: String, extra: String) extends FormOperation
case class FormOperationPhone(token: String, phone: String) extends FormOperation

case class RPCRequest(jsonrpc: String, method: String, params: FormOperation, id: Int)


// .....

  post {
    entity(as[RPCRequest]) { request =>
      complete {
        s"Method: ${request.method}"
      }
    }
  }

      

This works, but since each operation has diffirent parameters, so I can't seem to figure out how to tell the route which one is FormOperationCreate

, FormOperationOpen

or FormOperationPhone

choose to marshall in ...

+3


source to share





All Articles