JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
jsonrpc::server::Response Class Reference

Represents a JSON-RPC response. More...

#include <response.hpp>

Collaboration diagram for jsonrpc::server::Response:

Public Member Functions

 Response ()=delete
 
 Response (const Response &)=delete
 
auto operator= (const Response &) -> Response &=delete
 
 Response (Response &&other) noexcept
 
auto operator= (Response &&other) noexcept -> Response &=delete
 
 ~Response ()=default
 
auto ToJson () const -> nlohmann::json
 Serializes the Response object to a JSON object.
 
auto ToStr () const -> std::string
 Serializes the Response object to a string.
 

Static Public Member Functions

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.
 
static auto CreateResult (const nlohmann::json &result, const std::optional< nlohmann::json > &id) -> Response
 Creates a successful Response object.
 
static auto CreateLibError (LibErrorKind error_kind, const std::optional< nlohmann::json > &id=std::nullopt) -> Response
 Creates a Response object for a library error.
 
static auto CreateUserError (const nlohmann::json &error, const std::optional< nlohmann::json > &id) -> Response
 Creates a Response object for a user error.
 

Detailed Description

Represents a JSON-RPC response.

Definition at line 24 of file response.hpp.

Constructor & Destructor Documentation

◆ Response() [1/3]

jsonrpc::server::Response::Response ( )
delete

◆ Response() [2/3]

jsonrpc::server::Response::Response ( const Response & )
delete

◆ Response() [3/3]

jsonrpc::server::Response::Response ( Response && other)
noexcept

Definition at line 14 of file response.cpp.

15 : response_(std::move(other.response_)) {
16}

◆ ~Response()

jsonrpc::server::Response::~Response ( )
default

Member Function Documentation

◆ CreateLibError()

auto jsonrpc::server::Response::CreateLibError ( LibErrorKind error_kind,
const std::optional< nlohmann::json > & id = std::nullopt ) -> Response
static

Creates a Response object for a library error.

Parameters
errorKindThe kind of library error.
idThe ID of the request. It can be a JSON object or null.
Returns
A Response object indicating a library error.

Definition at line 67 of file response.cpp.

69 {
70 const auto &[code, message] = kErrorInfoMap.at(error_kind);
71 nlohmann::json error_response = CreateErrorResponse(message, code, id);
72 return Response{std::move(error_response)};
73}

Referenced by jsonrpc::server::Dispatcher::DispatchRequest().

Here is the caller graph for this function:

◆ CreateResult()

auto jsonrpc::server::Response::CreateResult ( const nlohmann::json & result,
const std::optional< nlohmann::json > & id ) -> Response
static

Creates a successful Response object.

Parameters
resultThe result of the method call.
idThe ID of the request. It can be a JSON object or null.
Returns
A Response object indicating success.

Definition at line 39 of file response.cpp.

41 {
42 nlohmann::json response = {{"jsonrpc", "2.0"}, {"result", result}};
43 if (id.has_value()) {
44 response["id"] = id.value();
45 }
46 return Response{std::move(response)};
47}

◆ CreateUserError()

auto jsonrpc::server::Response::CreateUserError ( const nlohmann::json & error,
const std::optional< nlohmann::json > & id ) -> Response
static

Creates a Response object for a user error.

Parameters
errorThe JSON object representing the error.
idThe ID of the request. It can be a JSON object or null.
Returns
A Response object indicating a user error.

Definition at line 49 of file response.cpp.

51 {
52 nlohmann::json response = {{"jsonrpc", "2.0"}, {"error", error}};
53 if (id.has_value()) {
54 response["id"] = id.value();
55 }
56 return Response{std::move(response)};
57}

◆ FromUserResponse()

auto jsonrpc::server::Response::FromUserResponse ( const nlohmann::json & response_json,
std::optional< nlohmann::json > id ) -> Response
static

Creates a Response object from a JSON object that represents a user response.

Parameters
responseJsonThe JSON object representing the user response.
idThe ID of the request. It can be a JSON object or null.
Returns
A Response object.

Definition at line 26 of file response.cpp.

28 {
29 if (response_json.contains("result")) {
30 return CreateResult(response_json["result"], id);
31 }
32 if (response_json.contains("error")) {
33 return CreateUserError(response_json["error"], id);
34 }
35 throw std::invalid_argument(
36 "Response JSON must contain either 'result' or 'error' field");
37}
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

◆ operator=() [1/2]

auto jsonrpc::server::Response::operator= ( const Response & ) -> Response &=delete
delete

◆ operator=() [2/2]

auto jsonrpc::server::Response::operator= ( Response && other) -> Response &=delete
deletenoexcept

◆ ToJson()

auto jsonrpc::server::Response::ToJson ( ) const -> nlohmann::json
nodiscard

Serializes the Response object to a JSON object.

Returns
The JSON representation of the response.

Definition at line 59 of file response.cpp.

59 {
60 return response_;
61}

◆ ToStr()

auto jsonrpc::server::Response::ToStr ( ) const -> std::string
nodiscard

Serializes the Response object to a string.

Returns
The string representation of the response.

Definition at line 63 of file response.cpp.

63 {
64 return response_.dump();
65}

The documentation for this class was generated from the following files: