Menu

#137 Is cppcms::json::value get(onCustomObjects) supported?

cppcms-v1.0.x
closed
nobody
1
2015-02-18
2014-11-25
No

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.

Discussion

  • Anonymous

    Anonymous - 2014-11-25

    From the fast glance, because you have an extra layer:

    "params" : [ {"user": {"email" : ...  }} ]
    

    Instead of

    "params" : [ {"email" : ...  } ]
    
     

    Last edit: Artyom Beilis 2014-11-25
  • Abhishek Kaushik

    @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:

    {
    "method": "user.add_user",
    "params": [
        {
            "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
    }
    
     

    Last edit: Abhishek Kaushik 2014-11-25
  • Artyom Beilis

    Artyom Beilis - 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
  • Abhishek Kaushik

    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
  • Artyom Beilis

    Artyom Beilis - 2014-11-25

    See the edit

     
  • Abhishek Kaushik

    updated my answer too in the meanwhile.

     
  • Abhishek Kaushik

    Ok, for now I am considering that following may not work either:

    app_user::data::User user = userJson.get<app_user::data::User>("user");
    

    where userJson is cppcms::json::value with the above JSON.

    Fine then, the ticket can be closed.

     
  • Artyom Beilis

    Artyom Beilis - 2014-11-25
    • status: open --> closed
     
  • Abhishek Kaushik

    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

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.