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

Represents a JSON RPC request. More...

#include <request.h>

+ Collaboration diagram for wwa::json_rpc::jsonrpc_request:

Static Public Member Functions

static jsonrpc_request from_json (const nlohmann::json &request)
 Parses and validates a JSON RPC request.
 

Public Attributes

nlohmann::json extra
 Extra fields from the JSON RPC request.
 
nlohmann::json id
 The ID of the request.
 
std::string jsonrpc
 The JSON RPC version.
 
std::string method
 The name of the method to be invoked.
 
nlohmann::json params
 The parameters for the method.
 

Detailed Description

Represents a JSON RPC request.

This struct holds the components of a JSON RPC request, including the JSON RPC version, method name, parameters, and ID.

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

Definition at line 22 of file request.h.

Member Function Documentation

◆ from_json()

jsonrpc_request wwa::json_rpc::jsonrpc_request::from_json ( const nlohmann::json & request)
static

Parses and validates a JSON RPC request.

Parameters
requestThe JSON RPC request as a nlohmann::json object.
Returns
The parsed JSON RPC request.

This method extracts the components of a JSON RPC request from the provided JSON object and validates the request, ensuring that all required fields are present and correctly formatted.

Exceptions
exceptionIf the request is invalid.
See also
exception::INVALID_REQUEST, exception::INVALID_PARAMS

Definition at line 62 of file request.cpp.

63{
64 jsonrpc_request req;
65 try {
66 request.get_to(req);
67 }
68 catch (const nlohmann::json::exception&) {
70 }
71
72 if (req.jsonrpc != "2.0") {
74 }
75
76 if (!req.params.is_array()) {
78 }
79
80 if (req.method.empty()) {
82 }
83
84 if (!is_valid_request_id(req.id)) {
86 }
87
88 req.extra = request;
89 req.extra.erase("jsonrpc");
90 req.extra.erase("method");
91 req.extra.erase("params");
92 req.extra.erase("id");
93 return req;
94}
static constexpr int INVALID_PARAMS
Invalid method parameter(s).
Definition exception.h:115
static constexpr int INVALID_REQUEST
The JSON sent is not a valid Request object.
Definition exception.h:105
static constexpr std::string_view err_not_jsonrpc_2_0_request
Error message for when the request is not a JSON-RPC 2.0 request.
Definition exception.h:31
static constexpr std::string_view err_empty_method
Error message for when the method is empty.
Definition exception.h:56
static constexpr std::string_view err_bad_request
Error request for when the request is not valid.
Definition exception.h:38
static constexpr std::string_view err_bad_params_type
Error message for when the parameters are not an array or an object.
Definition exception.h:62
static constexpr std::string_view err_bad_id_type
Error message for when the ID is not a number, a string, or null.
Definition exception.h:70
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

Member Data Documentation

◆ extra

nlohmann::json wwa::json_rpc::jsonrpc_request::extra

Extra fields from the JSON RPC request.

Definition at line 27 of file request.h.

◆ id

nlohmann::json wwa::json_rpc::jsonrpc_request::id

The ID of the request.

Definition at line 26 of file request.h.

◆ jsonrpc

std::string wwa::json_rpc::jsonrpc_request::jsonrpc

The JSON RPC version.

Definition at line 23 of file request.h.

◆ method

std::string wwa::json_rpc::jsonrpc_request::method

The name of the method to be invoked.

Definition at line 24 of file request.h.

◆ params

nlohmann::json wwa::json_rpc::jsonrpc_request::params

The parameters for the method.

Definition at line 25 of file request.h.


The documentation for this struct was generated from the following files: