Skip to content

Latest commit

 

History

History
75 lines (64 loc) · 2.9 KB

README.md

File metadata and controls

75 lines (64 loc) · 2.9 KB

jsonrpc++

C++ JSON-RPC 2.0 library

Build Status Github Releases

When grown up, this will be a leightweight JSON-RPC 2.0 C++ library.

What it is

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

Example: Parsing a request

jsonrpcpp::entity_ptr entity = jsonrpcpp::Parser::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 are no TCP client or server components shipped with this lib.

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

Some code

jsonrpcpp::entity_ptr entity = jsonrpcpp::Parser::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);
	}
}