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 <optional>
4#include <string>
5#include <unordered_map>
6
7#include <nlohmann/json.hpp>
8
9namespace jsonrpc::server {
10
19
21 std::unordered_map<LibErrorKind, std::pair<int, const char*>>;
22
24class Response {
25 public:
26 Response() = delete;
27 Response(const Response&) = delete;
28 auto operator=(const Response&) -> Response& = delete;
29
30 Response(Response&& other) noexcept;
31 auto operator=(Response&& other) noexcept -> Response& = delete;
32
33 ~Response() = default;
34
42 static auto FromUserResponse(
43 const nlohmann::json& response_json,
44 std::optional<nlohmann::json> id) -> Response;
45
52 static auto CreateResult(
53 const nlohmann::json& result,
54 const std::optional<nlohmann::json>& id) -> Response;
55
62 static auto CreateLibError(
63 LibErrorKind error_kind,
64 const std::optional<nlohmann::json>& id = std::nullopt) -> Response;
65
72 static auto CreateUserError(
73 const nlohmann::json& error,
74 const std::optional<nlohmann::json>& id) -> Response;
75
80 [[nodiscard]] auto ToJson() const -> nlohmann::json;
81
86 [[nodiscard]] auto ToStr() const -> std::string;
87
88 private:
95 explicit Response(
96 nlohmann::json response, std::optional<nlohmann::json> id = std::nullopt);
97
99 void ValidateResponse() const;
100
108 static auto CreateErrorResponse(
109 const std::string& message, int code,
110 const std::optional<nlohmann::json>& id) -> nlohmann::json;
111
113 nlohmann::json response_;
114
116 static const ErrorInfoMap kErrorInfoMap;
117};
118
119} // namespace jsonrpc::server
Represents a JSON-RPC response.
Definition response.hpp:24
static auto CreateLibError(LibErrorKind error_kind, const std::optional< nlohmann::json > &id=std::nullopt) -> Response
Creates a Response object for a library error.
Definition response.cpp:67
auto ToJson() const -> nlohmann::json
Serializes the Response object to a JSON object.
Definition response.cpp:59
auto operator=(Response &&other) noexcept -> Response &=delete
Response(const Response &)=delete
static auto CreateUserError(const nlohmann::json &error, const std::optional< nlohmann::json > &id) -> Response
Creates a Response object for a user error.
Definition response.cpp:49
static auto CreateResult(const nlohmann::json &result, const std::optional< nlohmann::json > &id) -> Response
Creates a successful Response object.
Definition response.cpp:39
auto operator=(const Response &) -> Response &=delete
static auto FromUserResponse(const nlohmann::json &response_json, std::optional< nlohmann::json > id) -> Response
Creates a Response object from a JSON object that represents a user response.
Definition response.cpp:26
auto ToStr() const -> std::string
Serializes the Response object to a string.
Definition response.cpp:63
LibErrorKind
Enumeration for library error kinds.
Definition response.hpp:12
std::unordered_map< LibErrorKind, std::pair< int, const char * > > ErrorInfoMap
Definition response.hpp:20