JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
request.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <string>
5
6#include <nlohmann/json.hpp>
7
8namespace jsonrpc::server {
9
13class Request {
14 public:
22 explicit Request(
23 std::string method,
24 const std::optional<nlohmann::json> &params = std::nullopt,
25 std::optional<nlohmann::json> id = std::nullopt);
26
33 static auto FromJson(const nlohmann::json &json_obj) -> Request;
34
40 [[nodiscard]] auto ToJson() const -> nlohmann::json;
41
43 [[nodiscard]] auto GetMethod() const -> std::string {
44 return method_;
45 }
46
48 [[nodiscard]] auto GetParams() const -> std::optional<nlohmann::json> {
49 return params_;
50 }
51
53 [[nodiscard]] auto GetId() const -> std::optional<nlohmann::json> {
54 return id_;
55 }
56
57 private:
59 std::string method_;
60
62 std::optional<nlohmann::json> params_;
63
65 std::optional<nlohmann::json> id_;
66};
67
68} // namespace jsonrpc::server
Represents a JSON-RPC request.
Definition request.hpp:13
static auto FromJson(const nlohmann::json &json_obj) -> Request
Creates a Request object from a JSON object.
Definition request.cpp:11
auto GetParams() const -> std::optional< nlohmann::json >
Gets the parameters.
Definition request.hpp:48
auto GetId() const -> std::optional< nlohmann::json >
Gets the request ID.
Definition request.hpp:53
auto GetMethod() const -> std::string
Gets the method name.
Definition request.hpp:43
auto ToJson() const -> nlohmann::json
Serializes the Request object to a JSON object.
Definition request.cpp:22
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