JSON RPC
JSON-RPC 2.0 library for C++
wwa::json_rpc Namespace Reference

Library namespace. More...

Namespaces

namespace  details
 Contains the implementation details of the JSON RPC library.
 

Classes

class  dispatcher
 A class that manages JSON RPC method handlers and processes JSON RPC requests. More...
 
class  dispatcher_private
 Private implementation of the JSON RPC dispatcher class. More...
 
class  exception
 JSON RPC Exception class. More...
 
struct  jsonrpc_request
 Represents a JSON RPC request. More...
 
class  method_not_found_exception
 Exception thrown when the method is not found. More...
 

Functions

static void from_json (const nlohmann::json &j, jsonrpc_request &r)
 Deserializes a JSON object into a jsonrpc_request structure.
 
WWA_JSONRPC_EXPORT nlohmann::json generate_error_response (const exception &e, const nlohmann::json &id=nlohmann::json::value_t::null)
 Generates an error response.
 
WWA_JSONRPC_EXPORT int get_error_code (const nlohmann::json &response)
 Gets the error code from an error response.
 
WWA_JSONRPC_EXPORT std::string get_error_message (const nlohmann::json &response)
 Gets the error message from an error response.
 
WWA_JSONRPC_EXPORT nlohmann::json get_request_id (const nlohmann::json &request)
 Get the request id object.
 
WWA_JSONRPC_EXPORT bool is_error_response (const nlohmann::json &response)
 Checks whether response is an error response.
 
WWA_JSONRPC_EXPORT bool is_valid_request_id (const nlohmann::json &id)
 Checks if the provided JSON value is a valid JSON RPC request ID.
 
WWA_JSONRPC_EXPORT std::string serialize_repsonse (const nlohmann::json &response)
 Serializes the JSON RPC response to a string.
 

Variables

static constexpr std::string_view err_bad_id_type = "ID must be either a number, a string, or null"
 Error message for when the ID is not a number, a string, or null.
 
static constexpr std::string_view err_bad_params_type = "Parameters must be either an array or an object or omitted"
 Error message for when the parameters are not an array or an object.
 
static constexpr std::string_view err_bad_request = "Bad request"
 Error request for when the request is not valid.
 
static constexpr std::string_view err_empty_batch = "Empty batch request"
 Error message for when the batch request is empty.
 
static constexpr std::string_view err_empty_method = "Method cannot be empty"
 Error message for when the method is empty.
 
static constexpr std::string_view err_invalid_params_passed_to_method = "Invalid parameters passed to method"
 Error message for when the parameters passed to the method are not correct.
 
static constexpr std::string_view err_method_not_found = "Method not found"
 Error message for when the method is not found.
 
static constexpr std::string_view err_not_jsonrpc_2_0_request = "Not a JSON-RPC 2.0 request"
 Error message for when the request is not a JSON-RPC 2.0 request.
 

Detailed Description

Library namespace.

Function Documentation

◆ from_json()

static void wwa::json_rpc::from_json ( const nlohmann::json & j,
jsonrpc_request & r )
static

Deserializes a JSON object into a jsonrpc_request structure.

Parameters
jThe JSON object to deserialize.
rThe jsonrpc_request structure to populate.

This function deserializes a JSON object into a jsonrpc_request structure.

  • It extracts the jsonrpc version, method name, params, and id from the JSON object.
  • If the params field is not present, it defaults to an empty array.
  • If the params field is an object, it is wrapped in an array.
Note
This function cannot be moved to an anonymous namespace because of Argument-Dependent Lookup (ADL).
See also
https://www.jsonrpc.org/specification#request_object
https://github.com/nlohmann/json?tab=readme-ov-file#arbitrary-types-conversions

Definition at line 38 of file request.cpp.

39{
40 r.params = nlohmann::json(nlohmann::json::value_t::discarded);
41 r.id = nlohmann::json(nlohmann::json::value_t::discarded);
42
43 j.at("jsonrpc").get_to(r.jsonrpc);
44 j.at("method").get_to(r.method);
45
46 if (j.contains("params")) {
47 r.params = j["params"];
48 }
49
50 if (j.contains("id")) {
51 r.id = j["id"];
52 }
53
54 if (r.params.is_discarded()) {
55 r.params = nlohmann::json::array();
56 }
57 else if (r.params.is_object()) {
58 r.params = nlohmann::json::array({r.params});
59 }
60}
std::string method
The name of the method to be invoked.
Definition request.h:24
std::string jsonrpc
The JSON RPC version.
Definition request.h:23
nlohmann::json id
The ID of the request.
Definition request.h:26
nlohmann::json params
The parameters for the method.
Definition request.h:25

◆ generate_error_response()

nlohmann::json wwa::json_rpc::generate_error_response ( const exception & e,
const nlohmann::json & id = nlohmann::json::value_t::null )

Generates an error response.

Parameters
eThe exception containing the error details.
idThe ID of the request.
Returns
The error response serialized into a JSON object.

This method creates a JSON RPC error response based on the provided exception and request ID.

See also
exception::to_json()

Definition at line 43 of file utils.cpp.

44{
45 // clang-format off
46 return {
47 {"jsonrpc", "2.0"},
48 {"error", e.to_json()},
49 {"id", id}
50 };
51 // clang-format on
52}
nlohmann::json to_json() const
Returns the error message as an Error Object.
Definition exception.h:215

◆ get_error_code()

int wwa::json_rpc::get_error_code ( const nlohmann::json & response)

Gets the error code from an error response.

Parameters
responseJSON RPC error response.
Returns
The error code.

Definition at line 33 of file utils.cpp.

34{
35 return response.at("error").value("code", 0);
36}

◆ get_error_message()

std::string wwa::json_rpc::get_error_message ( const nlohmann::json & response)

Gets the error message from an error response.

Parameters
responseJSON RPC error response.
Returns
The error message.

Definition at line 38 of file utils.cpp.

39{
40 return response.at("error").value("message", "");
41}

◆ get_request_id()

nlohmann::json wwa::json_rpc::get_request_id ( const nlohmann::json & request)

Get the request id object.

Parameters
requestJSON RPC request
Returns
Request ID

This function extracts the ID field from a JSON RPC request object. If the id field is not present or is not valid (see is_valid_request_id()), it returns a null JSON value.

Definition at line 17 of file utils.cpp.

18{
19 auto id = request.contains("id") ? request["id"] : nlohmann::json(nullptr);
20 return is_valid_request_id(id) ? id : nlohmann::json(nullptr);
21}
WWA_JSONRPC_EXPORT bool is_valid_request_id(const nlohmann::json &id)
Checks if the provided JSON value is a valid JSON RPC request ID.
Definition utils.cpp:12

◆ is_error_response()

bool wwa::json_rpc::is_error_response ( const nlohmann::json & response)

Checks whether response is an error response.

Parameters
responseJSON RPC response.
Returns
Whether the response is an error response.

Definition at line 28 of file utils.cpp.

29{
30 return response.is_object() && response.contains("error") && response["error"].is_object();
31}

◆ is_valid_request_id()

bool wwa::json_rpc::is_valid_request_id ( const nlohmann::json & id)

Checks if the provided JSON value is a valid JSON RPC request ID.

Parameters
idThe JSON value to check.
Returns
true if the JSON value is a valid request ID, false otherwise.

This function checks if the provided JSON value is a valid JSON RPC request ID. According to the JSON RPC specification, a valid request ID can be a string, a number, or null. Additionally, this function also considers a discarded JSON value as valid.

See also
https://www.jsonrpc.org/specification#request_object

Definition at line 12 of file utils.cpp.

13{
14 return id.is_string() || id.is_number() || id.is_null() || id.is_discarded();
15}

◆ serialize_repsonse()

std::string wwa::json_rpc::serialize_repsonse ( const nlohmann::json & response)

Serializes the JSON RPC response to a string.

Parameters
responseResponse to serialize.
Returns
Response serialized to a string' empty string if response.is_discarded() is true.

Definition at line 23 of file utils.cpp.

24{
25 return response.is_discarded() ? std::string{} : response.dump();
26}