jsonrpcx/json-rpc-cxx

Register void function method as JSON-RPC method, not a notification

zamazan4ik opened this issue · 6 comments

Hi!

Is there any way to register function methods, which returns void, as a JSON-RPC method with required response?

As far as I see all my void functions are registered as notifications (but I need them as methods).

Thank you!

I suppose somthing like constexpr json::value_t GetType(type<void>) { return json::value_t::null; } should be added.

Also possibly will be a good idea to add such helpers:

template <typename T, typename ReturnType, typename... ParamTypes>
  MethodHandle GetMethodHandle(ReturnType (T::*method)(ParamTypes...), T &instance) {
    std::function<ReturnType(ParamTypes...)> function = [&instance, method](ParamTypes &&... params) -> ReturnType {
      return (instance.*method)(std::forward<ParamTypes>(params)...);
    };
    return methodHandle(function);
  }

  template <typename T, typename ReturnType, typename... ParamTypes>
  MethodHandle GetNotificationHandle(ReturnType (T::*method)(ParamTypes...), T &instance) {
    std::function<ReturnType(ParamTypes...)> function = [&instance, method](ParamTypes &&... params) -> ReturnType {
        return (instance.*method)(std::forward<ParamTypes>(params)...);
    };
    return notificationHandle(function);
  }

In my opinion a void function is per definition a notification because it does not return anything. Could you explain why you want it implemented as a method? I am curious.

@cinemast Yeah, that's will be better if I explain why I need it.

I don't use JSON-RPC directly. I use JSON-RPC as a protocol for messages over Websockets. So client and server send to each other some messages. E.g. client sends over websocket JSON-RPC message to server, which maps message directly to class method (that's very convinient). Unfortunately during method execution can be thrown any exception. If I use JSON-RPC message I have a possibility to return an error according to the JSON-RPC standard. Otherwise, an error is lost (since it's a notification).

In the Internet I found related discussion: https://groups.google.com/forum/#!topic/json-rpc/esusPURMBu8

I the discussion above proposed a solution for void methods return result: null. It will be fine for me too.

I hope my explanation helps :)

Never thought about that. That makes sense. Could you open a PR for this? Thanks for the explanation.

I need to think a little bit more about the best way how to integrate this into the library. But yes, I'll try to prepare a PR with corresponding functionality and their we can discuss possible solutions.

Thank you!

P.S. I think the issue should be opened until we provide a solution for the issue.