Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::tuple mapping to object instead of json::value_t::array #49

Open
jacobschloss opened this issue Nov 12, 2024 · 1 comment
Open

Comments

@jacobschloss
Copy link

Hi,

I'm having an issue where a function taking parameters of int, std::tuple<double, double, double> seems like it is getting detected as int, json-object instead of int, json-array.

nlohmann-json maps std::pair and std::tuple to json-array.

Example:

When sending a jsonrpc request like
{"jsonrpc":"2.0","id":1,"method":"method","params":{"x":0, "y":[1.0, 2.0, 3.0]}}

to a C++ method with signature
bool method(const int x, const std::tuple<double, double, double>& y);

I get an error:

{"error":{"code":-32602,"message":"invalid parameter: must be object, but is array for parameter \"y\""},"id":1,"jsonrpc":"2.0"}

Adding this to typemapper.hpp to map std::tuples to json::value_t::array

template <typename... T>
constexpr json::value_t GetType(type<std::tuple<T...>>) {
  return json::value_t::array;
}

Seemed to work, and the call was accepted.

@cinemast
Copy link
Contributor

Yes, you already found the solution to that. However, I will not add more custom type mappings built in, since there are just too many.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants