JSON RPC
JSON-RPC 2.0 library for C++
utils.h
Go to the documentation of this file.
1#ifndef AB18CB0F_2A0A_401A_A253_B793A4B1FFB4
2#define AB18CB0F_2A0A_401A_A253_B793A4B1FFB4
3
4/**
5 * @file
6 * @brief Utility functions for JSON RPC handling.
7 */
8
9#include <string>
10#include <nlohmann/json.hpp>
11
12#include "export.h"
13
14namespace wwa::json_rpc {
15
16class exception;
17
18/**
19 * @brief Checks if the provided JSON value is a valid JSON RPC request ID.
20 *
21 * @param id The JSON value to check.
22 * @return `true` if the JSON value is a valid request ID, `false` otherwise.
23 *
24 * @details This function checks if the provided JSON value is a valid JSON RPC request ID.
25 * According to the JSON RPC specification, a valid request ID can be a string, a number, or null.
26 * Additionally, this function also considers a discarded JSON value as valid.
27 *
28 * @see https://www.jsonrpc.org/specification#request_object
29 */
30WWA_JSONRPC_EXPORT bool is_valid_request_id(const nlohmann::json& id);
31
32/**
33 * @brief Get the request id object
34 *
35 * @param request JSON RPC request
36 * @return Request ID
37 *
38 * @details This function extracts the ID field from a JSON RPC request object.
39 * If the `id` field is not present or is not valid (see @a is_valid_request_id()),
40 * it returns a `null` JSON value.
41 */
42WWA_JSONRPC_EXPORT nlohmann::json get_request_id(const nlohmann::json& request);
43
44/**
45 * @brief Serializes the JSON RPC response to a string.
46 *
47 * @param response Response to serialize.
48 * @return Response serialized to a string' empty string if `response.is_discarded()` is `true`.
49 */
50WWA_JSONRPC_EXPORT std::string serialize_repsonse(const nlohmann::json& response);
51
52/**
53 * @brief Checks whether @a response is an error response.
54 *
55 * @param response JSON RPC response.
56 * @return Whether the response is an error response.
57 */
58WWA_JSONRPC_EXPORT bool is_error_response(const nlohmann::json& response);
59
60/**
61 * @brief Gets the error code from an error response.
62 *
63 * @param response JSON RPC error response.
64 * @return The error code.
65 */
66WWA_JSONRPC_EXPORT int get_error_code(const nlohmann::json& response);
67
68/**
69 * @brief Gets the error message from an error response.
70 *
71 * @param response JSON RPC error response.
72 * @return The error message.
73 */
74WWA_JSONRPC_EXPORT std::string get_error_message(const nlohmann::json& response);
75
76/**
77 * @brief Generates an error response.
78 *
79 * @param e The exception containing the error details.
80 * @param id The ID of the request.
81 * @return The error response serialized into a JSON object.
82 *
83 * @details This method creates a JSON RPC error response based on the provided exception and request ID.
84 * @see exception::to_json()
85 */
87generate_error_response(const exception& e, const nlohmann::json& id = nlohmann::json::value_t::null);
88
89} // namespace wwa::json_rpc
90
91#endif /* AB18CB0F_2A0A_401A_A253_B793A4B1FFB4 */
Private implementation of the JSON RPC dispatcher class.
static std::uint64_t get_and_increment_counter() noexcept
Generates a unique request ID.
std::unordered_map< std::string, dispatcher::handler_t > m_methods
Map of method names to handler functions.
void add_handler(std::string &&method, dispatcher::handler_t &&handler)
Adds a method handler.
dispatcher::handler_t find_handler(const std::string &method) const
Finds a method handler.
static std::atomic_uint64_t m_id_counter
Counter for generating unique request IDs.
A class that manages JSON RPC method handlers and processes JSON RPC requests.
Definition dispatcher.h:77
virtual nlohmann::json do_process_request(const nlohmann::json &request, const std::any &data, bool is_batch, std::uint64_t unique_id)
Processes a single, non-batch JSON RPC request.
dispatcher & operator=(dispatcher &&rhs)=default
Move assignment operator.
dispatcher(dispatcher &&rhs)=default
Move constructor.
void add_ex(std::string_view method, F &&f, C instance)
Adds a method handler with a context parameter and a class instance.
Definition dispatcher.h:286
constexpr auto create_closure(C inst, F &&f) const
Creates a closure for invoking a member function with JSON parameters.
Definition dispatcher.h:453
virtual void request_parsed(const jsonrpc_request &request, const std::any &data, std::uint64_t unique_id)
Invoked after the request has been parsed.
void add_internal_method(std::string_view method, handler_t &&handler)
Adds a method handler for the specified method.
nlohmann::json process_request(const nlohmann::json &request, const std::any &data={})
Processes a JSON RPC request.
virtual void request_failed(const nlohmann::json &request_id, const std::exception *e, bool is_batch, std::uint64_t unique_id)
Invoked when a request fails.
virtual nlohmann::json invoke(const std::string &method, const nlohmann::json &params, const dispatcher::context_t &ctx, std::uint64_t unique_id)
Invokes a method handler.
void add(std::string_view method, F &&f)
Adds a method handler f for the method method.
Definition dispatcher.h:203
void add(std::string_view method, F &&f, C instance)
Adds a method to the dispatcher with the specified instance and function.
Definition dispatcher.h:221
virtual nlohmann::json process_batch_request(const nlohmann::json &request, const std::any &data, std::uint64_t unique_id)
Processes a batch request.
dispatcher()
Class constructor.
dispatcher(const dispatcher &)=delete
dispatcher & operator=(const dispatcher &)=delete
virtual ~dispatcher()
Class destructor.
void add_ex(std::string_view method, F &&f)
Adds a method handler with a context parameter.
Definition dispatcher.h:263
std::unique_ptr< dispatcher_private > d_ptr
Pointer to the implementation (Pimpl idiom).
Definition dispatcher.h:411
JSON RPC Exception class.
Definition exception.h:86
exception(const exception &)=default
Default copy constructor.
~exception() override
Default destructor.
nlohmann::json m_data
Custom data associated with the error.
Definition exception.h:231
std::string m_message
Error message.
Definition exception.h:230
int code() const noexcept
Returns the error code.
Definition exception.h:183
const std::string & message() const noexcept
Returns the error message.
Definition exception.h:190
exception(exception &&)=default
Default move constructor.
exception(int code, std::string_view message)
Construct a new exception object.
Definition exception.h:145
exception & operator=(const exception &rhs)=default
Default copy assignment operator.
const nlohmann::json & data() const noexcept
Returns custom data associated with the error.
Definition exception.h:197
exception & operator=(exception &&rhs)=default
Default move assignment operator.
const char * what() const noexcept override
Returns the error message.
Definition exception.h:207
nlohmann::json to_json() const
Returns the error message as an Error Object.
Definition exception.h:215
exception(int code, std::string_view message, const T &data)
Construct a new exception object with additional data.
Definition exception.h:134
int m_code
Error code.
Definition exception.h:232
Exception thrown when the method is not found.
Definition exception.h:238
method_not_found_exception(method_not_found_exception &&)=default
method_not_found_exception & operator=(method_not_found_exception &&)=default
method_not_found_exception & operator=(const method_not_found_exception &)=default
~method_not_found_exception() override
Default destructor.
method_not_found_exception(const method_not_found_exception &)=default
#define WWA_JSONRPC_EXPORT
Macro for exporting symbols when building the library dynamically or importing symbols when using the...
Definition export.h:42
static constexpr int INVALID_PARAMS
Invalid method parameter(s).
Definition exception.h:115
static constexpr int INTERNAL_ERROR
Internal JSON-RPC error.
Definition exception.h:120
static constexpr int METHOD_NOT_FOUND
The method does not exist or is not available.
Definition exception.h:110
static constexpr int INVALID_REQUEST
The JSON sent is not a valid Request object.
Definition exception.h:105
static constexpr int PARSE_ERROR
Invalid JSON was received by the server.
Definition exception.h:100
static constexpr std::string_view err_method_not_found
Error message for when the method is not found.
Definition exception.h:50
static constexpr std::string_view err_not_jsonrpc_2_0_request
Error message for when the request is not a JSON-RPC 2.0 request.
Definition exception.h:31
static constexpr std::string_view err_empty_batch
Error message for when the batch request is empty.
Definition exception.h:77
static constexpr std::string_view err_empty_method
Error message for when the method is empty.
Definition exception.h:56
static constexpr std::string_view err_bad_request
Error request for when the request is not valid.
Definition exception.h:38
static constexpr std::string_view err_bad_params_type
Error message for when the parameters are not an array or an object.
Definition exception.h:62
static constexpr std::string_view err_invalid_params_passed_to_method
Error message for when the parameters passed to the method are not correct.
Definition exception.h:44
static constexpr std::string_view err_bad_id_type
Error message for when the ID is not a number, a string, or null.
Definition exception.h:70
constexpr auto convert_args(const nlohmann::json &params, std::index_sequence< Indices... >)
Converts JSON parameters to a tuple of arguments based on the specified types.
Definition details.h:335
Contains the implementation details of the JSON RPC library.
Definition details.h:31
WWA_JSONRPC_EXPORT std::string serialize_repsonse(const nlohmann::json &response)
Serializes the JSON RPC response to a string.
Definition utils.cpp:23
WWA_JSONRPC_EXPORT std::string get_error_message(const nlohmann::json &response)
Gets the error message from an error response.
Definition utils.cpp:38
WWA_JSONRPC_EXPORT bool is_error_response(const nlohmann::json &response)
Checks whether response is an error response.
Definition utils.cpp:28
WWA_JSONRPC_EXPORT bool is_valid_request_id(const nlohmann::json &id)
Checks if the provided JSON value is a valid JSON RPC request ID.
Definition utils.cpp:12
WWA_JSONRPC_EXPORT nlohmann::json generate_error_response(const exception &e, const nlohmann::json &id=nlohmann::json::value_t::null)
Generates an error response.
Definition utils.cpp:43
WWA_JSONRPC_EXPORT nlohmann::json get_request_id(const nlohmann::json &request)
Get the request id object.
Definition utils.cpp:17
WWA_JSONRPC_EXPORT int get_error_code(const nlohmann::json &response)
Gets the error code from an error response.
Definition utils.cpp:33
Represents a JSON RPC request.
Definition request.h:22
std::string method
The name of the method to be invoked.
Definition request.h:24
std::string jsonrpc
The JSON RPC version.
Definition request.h:23
nlohmann::json id
The ID of the request.
Definition request.h:26
nlohmann::json extra
Extra fields from the JSON RPC request.
Definition request.h:27
static jsonrpc_request from_json(const nlohmann::json &request)
Parses and validates a JSON RPC request.
Definition request.cpp:62
nlohmann::json params
The parameters for the method.
Definition request.h:25