• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

badaix/jsonrpcpp: C++ JSON-RPC 2.0 library

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

badaix/jsonrpcpp

开源软件地址:

https://github.com/badaix/jsonrpcpp

开源编程语言:

C++ 99.9%

开源软件介绍:

jsonrpc++

Leightweight C++ JSON-RPC 2.0 library

Github Releases Build Status Language grade: C/C++

What it is

jsonrpc++ parses and constructs JSON-RPC 2.0 objects, like

Example: Parsing a request

jsonrpcpp::entity_ptr entity = jsonrpcpp::Parser::do_parse(R"({"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3})");
if (entity->is_request())
{
    jsonrpcpp::request_ptr request = dynamic_pointer_cast<jsonrpcpp::Request>(entity);
    if (request->method() == "subtract")
    {
        int result = request->params().get<int>("minuend") - request->params().get<int>("subtrahend");
        jsonrpcpp::Response response(*request, result);
        cout << " Response: " << response.to_json().dump() << "\n";
        //will print: {"jsonrpc": "2.0", "result": 19, "id": 3}
    }
    else
        throw jsonrpcpp::MethodNotFoundException(*request);
}

What it not is

jsonrpc++ is completely transport agnostic, i.e. it doesn't care about transportation of the messages and there is no TCP client or server component shipped with this library.

As JSON backbone JSON for Modern C++ is used.

Some code example

jsonrpcpp::entity_ptr entity =
    jsonrpcpp::Parser::do_parse(R"({"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3})");
if (entity && entity->is_request())
{
    jsonrpcpp::request_ptr request = dynamic_pointer_cast<jsonrpcpp::Request>(entity);
    cout << " Request: " << request->method() << ", id: " << request->id() << ", has params: " << !request->params().is_null() << "\n";
    if (request->method() == "subtract")
    {
        int result;
        if (request->params().is_array())
            result = request->params().get<int>(0) - request->params().get<int>(1);
        else
            result = request->params().get<int>("minuend") - request->params().get<int>("subtrahend");

        jsonrpcpp::Response response(*request, result);
        cout << " Response: " << response.to_json().dump() << "\n";
    }
    else if (request->method() == "sum")
    {
        int result = 0;
        for (const auto& summand : request->params().param_array)
            result += summand.get<int>();
        jsonrpcpp::Response response(*request, result);
        cout << " Response: " << response.to_json().dump() << "\n";
    }
    else
    {
        throw jsonrpcpp::MethodNotFoundException(*request);
    }
}



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap