jsonrpcx/json-rpc-cxx

RPC methods expecting json type argument throw exception if the argument is not of value_t::object

m29h opened this issue · 3 comments

m29h commented

Hi,
I am trying to use RPC methods with arguments of type json.
So far i can only get a call to such a method to work if the argument is exactly of value_t::object
In all other cases there is an "invalid parameter" exception thrown.
For my use case i need the method to be invokeable with any, most importantly with "non-structured" json value types (e.g. bool/number/array or string)

I think the issue #18 is somewhat similar/related to this. Any ideas on this would be appreciated.
Thank you!

The following example illustrates what works and what not:

#include "inmemoryconnector.hpp"
#include <iostream>
#include <jsonrpccxx/client.hpp>
#include <jsonrpccxx/server.hpp>

using namespace jsonrpccxx;

std::string jsonToString(json j) { return j.dump(); }
int main() {
  JsonRpc2Server rpcServer;
  rpcServer.Add("jsonToString", GetHandle(&jsonToString), {"json"});
  InMemoryConnector inMemoryConnector(rpcServer);
  JsonRpcClient client(inMemoryConnector, version::v2);
  std::cout << jsonToString(true) << std::endl; // This works as expected
  {
    json j = json::parse(R"({"val" : true})");
    std::cout << client.CallMethod<std::string>(1, "jsonToString", {j}) << std::endl; // this also works as expected but is NOT what I want!
  }
  {
    json j = json::parse(R"(true)");
    std::cout << client.CallMethod<std::string>(1, "jsonToString", {j}); // this throws an exception
    //   what():  -32602: invalid parameter: must be object, but is boolean for parameter "json"
  }
  return 0;
}

I agree, this should work of course, I need to figure out something in typemapper.hpp.

@m29h could you check the referenced commits? Would this solve your issue?

m29h commented

Yes indeed the issue is solved with the referenced changes!
Thank you!

Martin