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::server {
4
6 std::string method, const std::optional<nlohmann::json> &params,
7 std::optional<nlohmann::json> id)
8 : method_(std::move(method)), params_(params), id_(std::move(id)) {
9}
10
11auto Request::FromJson(const nlohmann::json &json_obj) -> Request {
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}
21
22auto Request::ToJson() const -> nlohmann::json {
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}
34
35} // 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 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