JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
request.cpp
Go to the documentation of this file.
2
3namespace jsonrpc::client {
4
6 std::string method, std::optional<nlohmann::json> params,
7 bool is_notification, const std::function<int()> &id_generator)
8 : method_(std::move(method)),
9 params_(std::move(params)),
10 is_notification_(is_notification),
11 id_(0) {
12 if (!is_notification_) {
13 id_ = id_generator();
14 }
15}
16
17auto Request::RequiresResponse() const -> bool {
18 return !is_notification_;
19}
20
21auto Request::GetKey() const -> int {
22 return id_;
23}
24
25auto Request::Dump() const -> std::string {
26 nlohmann::json json_request;
27 json_request["jsonrpc"] = "2.0";
28 json_request["method"] = method_;
29 if (params_) {
30 json_request["params"] = *params_;
31 }
32 if (!is_notification_) {
33 json_request["id"] = id_;
34 }
35 return json_request.dump();
36}
37
38} // namespace jsonrpc::client
auto Dump() const -> std::string
Serializes the request to a JSON string.
Definition request.cpp:25
auto GetKey() const -> int
Returns the unique key (ID) for the request.
Definition request.cpp:21
auto RequiresResponse() const -> bool
Checks if the request requires a response.
Definition request.cpp:17
Request(std::string method, std::optional< nlohmann::json > params, bool is_notification, const std::function< int()> &id_generator)
Constructs a new Request object.
Definition request.cpp:5