using named parameters in a client invocation
mfrigerio17 opened this issue · 6 comments
Hi, there is a check in the dispatcher code that prevents passing named parameters:
... "invalid parameter: procedure doesn't support named parameter" ..
Why is that? I suspect it is because the (quite esoteric :)) createMethodHandle(... can only work with a sequence of parameters, is that right?
It is a pity because it prevents writing something very natural like this
json args;
args["arg1"] = 1;
args["arg2"] = 33;
client.CallMethod... (... ,args);
which was actually possible with libjson-rpc-cpp
I know I can still do it by passing the parameter mapping when adding a method handle, but then the JSON arguments object in the server-side function will not have the same structure (it will be [1,33] instead of an object with fields)
Hi! That is what the NamedParamMapping is for: https://github.com/jsonrpcx/json-rpc-cxx/blob/master/include/jsonrpccxx/dispatcher.hpp#L20
Do you have a code snippet?
Hello,
sorry I think I formulated badly my original question.
Let's start from this one:
#include <iostream>
#include <jsonrpccxx/server.hpp>
#include <jsonrpccxx/client.hpp>
#include <jsonrpccxx/inmemoryconnector.hpp>
using json = jsonrpccxx::json;
json aFunction(const json& params) {
std::cout << "json params: " << params << std::endl;
return json(true);
}
int main() {
jsonrpccxx::JsonRpc2Server rpcServer;
rpcServer.Add("aFunction", jsonrpccxx::MethodHandle(aFunction), {"arg1", "arg2"});
InMemoryConnector connector(rpcServer);
jsonrpccxx::JsonRpcClient client(connector, jsonrpccxx::version::v2);
json args, ret;
args["arg1"] = 1.0;
args["arg2"] = 2.0;
ret = client.CallMethod<bool>(1, "aFunction", args);
std::cout << ret << std::endl;
return 0;
}which fails with a JSON exception:
terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_2::detail::type_error'
what(): [json.exception.type_error.302] type must be array, but is object
Probably related: why does CallMethod only take a std::vector or std::map of json, and not directly a json object?
Hi!
Something like this should work:
json aFunction(const json& args) {
std::cout << "json params" << args << std::endl;
return json(true);
}
int main() {
jsonrpccxx::JsonRpc2Server rpcServer;
rpcServer.Add("aFunction", jsonrpccxx::MethodHandle(aFunction), {"arg1", "arg2"});
InMemoryConnector connector(rpcServer);
jsonrpccxx::JsonRpcClient client(connector, jsonrpccxx::version::v2);
jsonrpccxx::named_parameter args;
json ret;
args["arg1"] = 1.0;
args["arg2"] = 2.0;
ret = client.CallMethod<json>(1, "aFunction", args);
std::cout << ret << std::endl;
return 0;
}Hi, thanks for your reply.
I see what you did (btw, it should be CallMethodNamed, not CallMethod).
However, as I expected, it prints:
json params: [1.0,2.0]
meaning that the argument was turned into an array (so one cant use the key-value mapping in the method).
Furthermore, and more importantly, one is still not able to use a json object with named arguments, from a different client (possibly written in another language and not using your library).
I suspect implementing true named parameter would be trivial, if one does not use createMethodHandle(... (as I do in my example, where I provide myself the wrapper method aFunction() that uses JSON types)
I would go for the JSON types approach then.