JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
jsonrpc::server::Request Class Reference

Represents a JSON-RPC request. More...

#include <request.hpp>

Collaboration diagram for jsonrpc::server::Request:

Public Member Functions

 Request (std::string method, const std::optional< nlohmann::json > &params=std::nullopt, std::optional< nlohmann::json > id=std::nullopt)
 Constructs a Request object.
 
auto ToJson () const -> nlohmann::json
 Serializes the Request object to a JSON object.
 
auto GetMethod () const -> std::string
 Gets the method name.
 
auto GetParams () const -> std::optional< nlohmann::json >
 Gets the parameters.
 
auto GetId () const -> std::optional< nlohmann::json >
 Gets the request ID.
 

Static Public Member Functions

static auto FromJson (const nlohmann::json &json_obj) -> Request
 Creates a Request object from a JSON object.
 

Detailed Description

Represents a JSON-RPC request.

Definition at line 13 of file request.hpp.

Constructor & Destructor Documentation

◆ Request()

jsonrpc::server::Request::Request ( std::string method,
const std::optional< nlohmann::json > & params = std::nullopt,
std::optional< nlohmann::json > id = std::nullopt )
explicit

Constructs a Request object.

Parameters
methodThe name of the method to be invoked.
paramsThe parameters for the method (optional).
idThe ID of the request (optional).

Definition at line 5 of file request.cpp.

8 : method_(std::move(method)), params_(params), id_(std::move(id)) {
9}

Member Function Documentation

◆ FromJson()

auto jsonrpc::server::Request::FromJson ( const nlohmann::json & json_obj) -> Request
static

Creates a Request object from a JSON object.

Parameters
jsonObjThe JSON object representing the request.
Returns
A Request object.

Definition at line 11 of file request.cpp.

11 {
12 auto params = json_obj.contains("params")
13 ? std::optional<nlohmann::json>(json_obj["params"])
14 : std::nullopt;
15
16 auto id = json_obj.contains("id")
17 ? std::optional<nlohmann::json>(json_obj["id"])
18 : std::nullopt;
19 return Request(json_obj["method"], params, id);
20}
Request(std::string method, const std::optional< nlohmann::json > &params=std::nullopt, std::optional< nlohmann::json > id=std::nullopt)
Constructs a Request object.
Definition request.cpp:5

◆ GetId()

auto jsonrpc::server::Request::GetId ( ) const -> std::optional<nlohmann::json>
inlinenodiscard

Gets the request ID.

Definition at line 53 of file request.hpp.

53 {
54 return id_;
55 }

◆ GetMethod()

auto jsonrpc::server::Request::GetMethod ( ) const -> std::string
inlinenodiscard

Gets the method name.

Definition at line 43 of file request.hpp.

43 {
44 return method_;
45 }

◆ GetParams()

auto jsonrpc::server::Request::GetParams ( ) const -> std::optional<nlohmann::json>
inlinenodiscard

Gets the parameters.

Definition at line 48 of file request.hpp.

48 {
49 return params_;
50 }

◆ ToJson()

auto jsonrpc::server::Request::ToJson ( ) const -> nlohmann::json
nodiscard

Serializes the Request object to a JSON object.

Returns
The JSON representation of the request.

Definition at line 22 of file request.cpp.

22 {
23 nlohmann::json json_obj;
24 json_obj["jsonrpc"] = "2.0";
25 json_obj["method"] = method_;
26 if (params_.has_value()) {
27 json_obj["params"] = params_.value();
28 }
29 if (id_.has_value()) {
30 json_obj["id"] = id_.value();
31 }
32 return json_obj;
33}

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