Skip to content

Commit

Permalink
Merge pull request #281 from DanielKoohmarey/master
Browse files Browse the repository at this point in the history
Add Json::Value arg example
  • Loading branch information
cinemast authored Oct 30, 2019
2 parents 4e09ca1 + adbd5a3 commit d9ee96d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/examples/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
],
"returns": 7.5
},
{
"name": "calculate",
"params": {
"args": {}
},
"returns": []
},
{
"name": "isEqual",
"params": [
Expand All @@ -48,4 +55,4 @@
"name" : "methodWithoutParameters",
"returns": "String"
}
]
]
13 changes: 12 additions & 1 deletion src/examples/stubclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ int main() {

cout << " 3 + 5 = " << c.addNumbers(3, 5) << endl;
cout << " 3.2 + 5.3 = " << c.addNumbers2(3.2, 5.3) << endl;
Json::Value args;
args["arg1"] = 1;
args["arg2"] = 2;
args["operator"] = "+";
Json::Value result = c.calculate(args);
cout << " 1 + 2 = " << result[0].asInt() << endl;
args["arg1"] = 3;
args["arg2"] = 4;
args["operator"] = "*";
result = c.calculate(args);
cout << " 3 * 4 = " << result[0].asInt() << endl;
cout << "Compare: " << c.isEqual("Peter", "peter") << endl;
cout << "Build object: " << c.buildObject("Peter", 1990) << endl;

Expand All @@ -34,4 +45,4 @@ int main() {
} catch (JsonRpcException &e) {
cerr << e.what() << endl;
}
}
}
53 changes: 49 additions & 4 deletions src/examples/stubserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MyStubServer : public AbstractStubServer {
virtual std::string sayHello(const std::string &name);
virtual int addNumbers(int param1, int param2);
virtual double addNumbers2(double param1, double param2);
virtual Json::Value calculate(const Json::Value& args);
virtual bool isEqual(const std::string &str1, const std::string &str2);
virtual Json::Value buildObject(const std::string &name, int age);
virtual std::string methodWithoutParameters();
Expand All @@ -35,13 +36,14 @@ MyStubServer::MyStubServer(AbstractServerConnector &connector,
void MyStubServer::notifyServer() { cout << "Server got notified" << endl; }

string MyStubServer::sayHello(const string &name) {
if (name == "")
throw JsonRpcException(-32100, "Name was empty");
return "Hello " + name;
if (name == "")
throw JsonRpcException(-32100, "Name was empty");
return "Hello " + name;
}

int MyStubServer::addNumbers(int param1, int param2) { return param1 + param2; }


double MyStubServer::addNumbers2(double param1, double param2) {
return param1 + param2;
}
Expand All @@ -50,6 +52,49 @@ bool MyStubServer::isEqual(const string &str1, const string &str2) {
return str1 == str2;
}

Json::Value MyStubServer::calculate(const Json::Value& args) {
Json::Value result;
if((args.isMember("arg1") && args["arg1"].isInt()) &&
(args.isMember("arg2") && args["arg2"].isInt()) &&
(args.isMember("operator") && args["operator"].isString()))
{
int calculated = 0;

switch(args["operator"].asString()[0])
{
case '+':
{
calculated = args["arg1"].asInt() + args["arg2"].asInt();
break;
}
case '-':
{
calculated = args["arg1"].asInt() - args["arg2"].asInt();
break;
}
case '*':
{
calculated = args["arg1"].asInt() * args["arg2"].asInt();
break;
}
case '/':
{
if(args["arg2"].asInt() != 0)
{
calculated = args["arg1"].asInt() / args["arg2"].asInt();
}
break;
}
default:
break;
}

result.append(calculated);
}

return result;
}

Json::Value MyStubServer::buildObject(const string &name, int age) {
Json::Value result;
result["name"] = name;
Expand All @@ -70,4 +115,4 @@ int main() {
s.StopListening();

return 0;
}
}

0 comments on commit d9ee96d

Please sign in to comment.