JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
response.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <expected>
4#include <optional>
5
6#include <nlohmann/json.hpp>
7
10
11namespace jsonrpc::endpoint {
12
15
16class Response {
17 public:
18 Response() = default;
19 Response(const Response&) = default;
20 Response(Response&& other) = default;
21 auto operator=(const Response&) -> Response& = default;
22 auto operator=(Response&& other) noexcept -> Response& = default;
23
24 ~Response() = default;
25
26 static auto FromJson(const nlohmann::json& json)
27 -> std::expected<Response, error::RpcError>;
28
29 static auto CreateSuccess(
30 const nlohmann::json& result, const std::optional<RequestId>& id)
31 -> Response;
32
33 static auto CreateError(
34 RpcErrorCode code, const std::optional<RequestId>& id = std::nullopt)
35 -> Response;
36
37 static auto CreateError(
38 const RpcError& error, const std::optional<RequestId>& id = std::nullopt)
39 -> Response;
40
41 static auto CreateError(
42 const nlohmann::json& error, const std::optional<RequestId>& id)
43 -> Response;
44
45 [[nodiscard]] auto IsSuccess() const -> bool;
46
47 [[nodiscard]] auto GetResult() const -> const nlohmann::json&;
48
49 [[nodiscard]] auto GetError() const -> const nlohmann::json&;
50
51 [[nodiscard]] auto GetId() const -> std::optional<RequestId>;
52
53 [[nodiscard]] auto ToJson() const -> nlohmann::json;
54
55 private:
56 explicit Response(nlohmann::json response) : response_(std::move(response)) {
57 }
58
59 [[nodiscard]] auto ValidateResponse() const
60 -> std::expected<void, error::RpcError>;
61
62 nlohmann::json response_;
63};
64
65} // namespace jsonrpc::endpoint
66
67namespace nlohmann {
68template <>
69struct adl_serializer<jsonrpc::endpoint::Response> {
70 static void to_json(json& j, const jsonrpc::endpoint::Response& r) {
71 j = r.ToJson();
72 }
73};
74} // namespace nlohmann
static auto CreateSuccess(const nlohmann::json &result, const std::optional< RequestId > &id) -> Response
Definition response.cpp:18
auto GetResult() const -> const nlohmann::json &
Definition response.cpp:63
static auto CreateError(RpcErrorCode code, const std::optional< RequestId > &id=std::nullopt) -> Response
Definition response.cpp:28
auto GetId() const -> std::optional< RequestId >
Definition response.cpp:77
auto operator=(const Response &) -> Response &=default
static auto FromJson(const nlohmann::json &json) -> std::expected< Response, error::RpcError >
Definition response.cpp:9
auto operator=(Response &&other) noexcept -> Response &=default
auto IsSuccess() const -> bool
Definition response.cpp:59
auto GetError() const -> const nlohmann::json &
Definition response.cpp:70
Response(Response &&other)=default
Response(const Response &)=default
auto ToJson() const -> nlohmann::json
Definition response.cpp:89
std::variant< int64_t, std::string > RequestId
Definition types.hpp:15
static void to_json(json &j, const jsonrpc::endpoint::Response &r)
Definition response.hpp:70