
JSON-RPC 2.0 library for C++
Introduction
This library provides a robust implementation of the JSON-RPC 2.0 protocol for C++. It allows you to easily handle JSON-RPC requests, and manage responses.
Features
- Full support for JSON-RPC 2.0
- Easy-to-use API
- Error handling and validation
Installation
To install the library, follow these steps:
- Clone the repository:
git clone https://github.com/sjinks/jsonrpc-cpp.git
cd jsonrpc-cpp
- The library depends on
nlohmann-json
. However, if you do not have it installed, it will be downloaded from the GitHub repository.
- Build the project using CMake:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
- Install the library:
cmake --install build # In Linux, you may have to use `sudo`
Usage
Basic Usage
#include <wwa/jsonrpc/dispatcher.h>
class my_server {
public:
my_server()
{
this->m_dispatcher.add("add", &my_server::add, this);
}
void handle_request()
{
const std::string input = read_request();
try {
const auto json = nlohmann::json::parse(input);
const auto result = this->m_dispatcher.process_request(json);
if (!response.empty()) {
send_response(response);
}
}
catch (const nlohmann::json::exception& e) {
send_response(
)
);
}
}
private:
wwa::json_rpc::dispatcher m_dispatcher;
int add(int a, int b)
{
return a + b;
}
};
static constexpr int PARSE_ERROR
Invalid JSON was received by the server.
WWA_JSONRPC_EXPORT std::string serialize_repsonse(const nlohmann::json &response)
Serializes the JSON RPC response to a string.
WWA_JSONRPC_EXPORT nlohmann::json generate_error_response(const exception &e, const nlohmann::json &id=nlohmann::json::value_t::null)
Generates an error response.
Advanced Usage
Sometimes, it may be necessary to pass some additional information to the handler. For example, an IP address of the client or authentication information.
Method handlers can accept a context
parameter. That parameter is constructed from the data passed dispatcher::process_request()
method and additional fields from the JSON RPC request.
For example,
struct extra_data {
std::string ip;
};
class my_server {
public:
my_server()
{
this->m_dispatcher.add_ex("add", &my_server::add, this);
}
void handle_request()
{
const nlohmann::json input = read_request();
extra_data extra;
extra.ip = get_peer_ip();
if (!response.empty()) {
send_response(response);
}
}
private:
wwa::json_rpc::dispatcher m_dispatcher;
{
std::cout << "IP address is " << std::any_cast<extra_data>(ctx.first).ip << "\n";
return a + b;
}
};
std::pair< std::any, nlohmann::json > context_t
Optional context data for method handlers.
It is also possible to get the extra fields form the JSON RPC request.
For example, given the request:
{
"jsonrpc": "2.0",
"method": "subtract",
"params": {"minuend": 42, "subtrahend": 23},
"id": 1,
"auth": "secret",
"user": "admin"
}
There are extra fields in the request: auth
and user
. These fields will be collected into an object and passed as a part of the context.
{
const auto& extra = ctx.second;
std::string auth = extra.at("auth");
std::string user = extra.at("user");
}
There are more examples available in the test subdirectory (you may want to look at base.h
/base.cpp
or test_extra_param.cpp
)
The documentation is available at https://sjinks.github.io/jsonrpc-cpp/