JSON-RPC笔记

  json-rpc是一种非常轻量级的跨语言远程调用协议,使用简单,仅需几十行代码,即可实现一个远程调用的客户端,方便语言扩展客户端的实现。服务器端有php、java、python、ruby、.net等语言实现,是非常不错的轻量级远程调用协议。

1.JSON-RPC简介和相关用法

参考如下内容:
https://www.cnblogs.com/cielosun/p/6762550.html

2.对于非标准数据格式通过JSON-RPC传输的注意点

  一般的数据格式如int、std::string、std::vector等,都可以通过JSON-RPC进行直接传输,但如果是自己定义的一个结构体,作为一个函数API的输入参数或返回参数,比如定义了图像信息的结构体,

struct ImageInfo
{
    int height, width;
    std::vector<uint8_t> data;
    int channels;
};

  此时需要增加两个函数to_json()from_json() 用来告诉JSON-RPC我们传入的数据类型和传出的数据类型,如下:
data_types.h

#ifndef DATA_TYPES_H
#define DATA_TYPES_H

#include "nlohmann/json.hpp"

#define SAFE_PARSE_FEILD(a)   \
    if (j.contains(#a)) {     \
        j.at(#a).get_to(p.a); \
    }

inline void to_json(nlohmann::json &j, const ImageInfo &p)
{
    j["height"] = p.height;
    j["width"] = p.width;
    j["data"] = p.data;
    j["channels"] = p.channels;
}

inline void from_json(const nlohmann::json &j, ImageInfo &p)
{
    SAFE_PARSE_FEILD(height);
    SAFE_PARSE_FEILD(width);
    SAFE_PARSE_FEILD(data);
    SAFE_PARSE_FEILD(channels);
}

#endif // DAT_TYPES_H

3.curl接口通讯测试

服务器启动,输入如下指令,会在用户目录下生成ll.txt的文件夹,打印从服务器获取到的请求结果,保存在ll.txt文件中。

# shell命令调用http接口(curl方式)

#——enumCameras(√)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"enumCameras","params":[],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt
# 打印从服务器获取到的请求结果,保存在ll.txt文件中

#——connect(√)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"connect","params":["gige:00D63914744"],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——open(√)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"open","params":[],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——close(√)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"close","params":[],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——getDeviceInfo(√)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"getDeviceInfo","params":[],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——startGrab(√)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"startGrab","params":[true],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"startGrab","params":[false],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——setSoftTrigger
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"setSoftTrigger","params":[true],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——getExposureTime(√)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"getExposureTime","params":[],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——getGainValue(√)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"getGainValue","params":[],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——getFrameRate(√)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"getFrameRate","params":[],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——getOneFrameImageInfo(√,但图像的数据量很大,需要进行压缩处理)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"getOneFrameImageInfo","params":[],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

#——getCompressImageInfo(√,压缩处理后传输图像,方便在控件上显示)
curl -X POST -H 'content-type: application/json' --data '{"jsonrpc":"2.0","method":"getCompressImageInfo","params":[],"id":0}' "http://127.0.0.1:8986/jsonrpc" >> ll.txt

4.nlohmann::json 库

https://www.cnblogs.com/linuxAndMcu/p/14503341.html

5.Jsonrpc的错误码

https://blog.csdn.net/weixin_39900286/article/details/111297415

  • -32700: Parse error语法解析错误 (服务端接收到无效的json。该错误发送于服务器尝试解析json文本)
  • -32600: Invalid Request (发送的json不是一个有效的请求对象)
  • -32601: Method not found (该方法不存在或无效)
  • -32602: Invalid params (无效的方法参数)
  • -32603: Internal error (JSON-RPC内部错误)
  • -32000 ~ -32099: Server error (预留用于自定义的服务器错误)
  • -32768 ~ -32000: 保留的预定义错误代码, 保留下列以供将来使用

你可能感兴趣的:(网络通信编程,json,rpc,笔记)