Is cppcms::json::value get(onCustomObjects) supported?
Brought to you by:
artyom-beilis
I have following structure defined for Class user:
namespace app_user {
namespace data {
struct User {
uint user_id;
std::string email;
std::string first_name;
std::string last_name;
std::string password;
uint last_login;
uint joining_date;
std::string address1;
std::string address2;
std::string city;
std::string country;
std::string state;
std::string pincode;
};
} //data
}
namespace cppcms {
namespace json {
template<>
struct traits<app_user::data::User> {
static app_user::data::User get(value const &v){
app_user::data::User user;
if(v.type() != is_object) {
throw bad_value_cast();
}
user.user_id = v.get<uint>("userId");
user.email = v.get<std::string>("email");
user.password = v.get<std::string>("password");
user.first_name = v.get<std::string>("firstName");
user.last_name = v.get<std::string>("lastName");
user.last_login = v.get<uint>("lastLogin");
user.joining_date = v.get<uint>("joiningDate");
user.address1 = v.get<std::string>("address1");
user.address2 = v.get<std::string>("address2");
user.city = v.get<std::string>("city");
user.state = v.get<std::string>("state");
user.country = v.get<std::string>("country");
user.pincode = v.get<std::string>("pincode");
return user;
}
static void set(value &v, app_user::data::User const &user){
v.set("userId",user.user_id);
v.set("email",user.email);
v.set("password",user.password);
v.set("firstName",user.first_name);
v.set("lastName",user.last_name);
v.set("lastLogin",user.last_login);
v.set("joiningDate",user.joining_date);
v.set("address1",user.address1);
v.set("address2",user.address2);
v.set("city",user.city);
v.set("state",user.state);
v.set("country",user.country);
v.set("pincode",user.pincode);
}
};
}
}
And following is the JsonRPC call method signature:
bind("user.add_user", cppcms::rpc::json_method(&User_json_service::add_user,this),method_role);
void User_json_service::add_user(app_user::data::User user)
And the json that I send is:
{
"method": "user.add_user",
"params": [
{"user":{
"email": "kaushik@gmail.com",
"password": "testing",
"firstName": "Abhis",
"lastName": "Kaushi",
"lastLogin": "0",
"joiningDate": "0",
"address1": "Kaushi",
"address2": "Kaushi",
"country": "Kaushi",
"pincode": "Kaushi",
"userId": 0,
"city": "Mumbai",
"state": "Maharashtra"
}}
],
"id": 4
}
However, the error I get it is "Invalid Parameters". Is there something done here which is not supported by CppCMS?
Let me know if I should provide any more additional details.
Anonymous
From the fast glance, because you have an extra layer:
Instead of
Last edit: Artyom Beilis 2014-11-25
@Anonymous, I am sorry, I need to correct that JSON. That thing was another try I did, but still facing similar error. The correct json would be without that user layer, as below:
Last edit: Abhishek Kaushik 2014-11-25
CppCMS JSON rpc supports params as array. As it is impossible to connect named parameters in to C++.
So can I close the bug?
Last edit: Artyom Beilis 2014-11-25
Ohk. However, I have seen that it supports named parameter if we use:
cppcms::json::value when used as rpc-method parameter.
app_user::data::User user = userJson.get<app_user::data::user>("user");</app_user::data::user>
where userJson is cppcms::json::value with the above JSON.
Can this work?
Last edit: Abhishek Kaushik 2014-11-25
See the edit
updated my answer too in the meanwhile.
Ok, for now I am considering that following may not work either:
where userJson is cppcms::json::value with the above JSON.
Fine then, the ticket can be closed.
See... the stuff is simple
Pure method is function that receives cppcms::json::array:
The rest is "sugar" that allows to expand the array to parameters. i.e.
If you bind a function
than it should be
Simple.
Please refer to:
http://cppcms.com/cppcms_ref/latest/namespacecppcms_1_1rpc.html
http://cppcms.com/wikipp/en/page/cppcms_1x_json_rpc
http://cppcms.com/cppcms_ref/latest/ex_json_rpc.html
And if you have Question please address the CppCMS users mailing list
Thanks Artyom. The above code would have worked in the first place. The issue was with joiningDate & lastLogin. A uint was expected whereas json contained "0", which somewhat denotes a String. So anything without "quotes" would work, like 0,1,2,etc.
Anyway, thanks for you patience.
Last edit: Abhishek Kaushik 2014-11-25