-
-
Notifications
You must be signed in to change notification settings - Fork 47
Invalid type error when calling a function where one of the types is Json. #51
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
Comments
The following changes to check_param_type fixes this issue: template <typename T>
inline void check_param_type(size_t index, const json &x, json::value_t expectedType, typename std::enable_if<std::is_same<T, json>::value>::type * = 0) {
// No checking if type is Json - already json.
}
template <typename T>
inline void check_param_type(size_t index, const json &x, json::value_t expectedType,
typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, json>::value>::type * = 0) {
if (expectedType == json::value_t::number_unsigned && x.type() == json::value_t::number_integer) {
if (x.get<long long int>() < 0)
throw JsonRpcException(invalid_params, "invalid parameter: must be " + type_name(expectedType) + ", but is " + type_name(x.type()), index);
} else if (x.type() == json::value_t::number_unsigned && expectedType == json::value_t::number_integer) {
if (x.get<long long unsigned>() > (long long unsigned)std::numeric_limits<T>::max()) {
throw JsonRpcException(invalid_params, "invalid parameter: exceeds value range of " + type_name(expectedType), index);
}
} else if ((x.type() == json::value_t::number_unsigned || x.type() == json::value_t::number_integer) && expectedType == json::value_t::number_float) {
if (static_cast<long long int>(x.get<double>()) != x.get<long long int>()) {
throw JsonRpcException(invalid_params, "invalid parameter: exceeds value range of " + type_name(expectedType), index);
}
} else if (x.type() != expectedType) {
throw JsonRpcException(invalid_params, "invalid parameter: must be " + type_name(expectedType) + ", but is " + type_name(x.type()), index);
}
}
template <typename T>
inline void check_param_type(size_t index, const json &x, json::value_t expectedType,
typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, json>::value>::type * = 0) {
if (x.type() != expectedType) {
throw JsonRpcException(invalid_params, "invalid parameter: must be " + type_name(expectedType) + ", but is " + type_name(x.type()), index);
}
} |
Also note this is hard to read - suggest improving since you are using C++17 and above: template <typename T>
inline void check_param_type(size_t index, const json &x, json::value_t expectedType) {
if constexpr (std::is_same_v<T, json>) {
// No checking if type is Json - already json.
return;
} else if constexpr (std::is_arithmetic_v<T>) {
if (expectedType == json::value_t::number_unsigned && x.type() == json::value_t::number_integer) {
if (x.get<long long int>() < 0)
throw JsonRpcException(invalid_params, "invalid parameter: must be " + type_name(expectedType) + ", but is " + type_name(x.type()), index);
} else if (x.type() == json::value_t::number_unsigned && expectedType == json::value_t::number_integer) {
if (x.get<long long unsigned>() > (long long unsigned)std::numeric_limits<T>::max()) {
throw JsonRpcException(invalid_params, "invalid parameter: exceeds value range of " + type_name(expectedType), index);
}
} else if ((x.type() == json::value_t::number_unsigned || x.type() == json::value_t::number_integer) && expectedType == json::value_t::number_float) {
if (static_cast<long long int>(x.get<double>()) != x.get<long long int>()) {
throw JsonRpcException(invalid_params, "invalid parameter: exceeds value range of " + type_name(expectedType), index);
}
} else if (x.type() != expectedType) {
throw JsonRpcException(invalid_params, "invalid parameter: must be " + type_name(expectedType) + ", but is " + type_name(x.type()), index);
}
} else {
if (x.type() != expectedType) {
throw JsonRpcException(invalid_params, "invalid parameter: must be " + type_name(expectedType) + ", but is " + type_name(x.type()), index);
}
}
} |
Adding Patch File: Cannot push a branch: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have a call that is defined in the following manner:
It's used in a manner to allow independent values within a class/struct that can be converted to_json(json, SomeStruct) and allow the property to be changed. - So using the Json type as a variant of what is allowed and then converting and reconverting the values to allow the API to identify a single value to change.
The problem I have is when I do the following:
When I send the following for example:
I get the return of:
But there should be no comparison needed as the param_type is already json.
Suggest changing:
The text was updated successfully, but these errors were encountered: