JSON RPC
JSON-RPC 2.0 library for C++
wwa::json_rpc Namespace Reference

Library namespace. More...

Namespaces

namespace  details
 Contains the implementation details of the JSON RPC library.
 

Classes

class  dispatcher
 A class that manages JSON RPC method handlers and processes JSON RPC requests. More...
 
class  dispatcher_private
 Private implementation of the JSON RPC dispatcher class. More...
 
class  exception
 JSON RPC Exception class. More...
 
struct  hasher
 Custom hasher for std::string_view. More...
 
struct  jsonrpc_request
 Represents a JSON RPC request. More...
 

Functions

static void from_json (const nlohmann::json &j, jsonrpc_request &r)
 Deserializes a JSON object into a jsonrpc_request structure.
 

Variables

static constexpr std::string_view err_bad_id_type = "ID must be either a number, a string, or null"
 Error message for when the ID is not a number, a string, or null.
 
static constexpr std::string_view err_bad_params_type = "Parameters must be either an array or an object or omitted"
 Error message for when the parameters are not an array or an object.
 
static constexpr std::string_view err_empty_batch = "Empty batch request"
 Error message for when the batch request is empty.
 
static constexpr std::string_view err_empty_method = "Method cannot be empty"
 Error message for when the method is empty.
 
static constexpr std::string_view err_invalid_params_passed_to_method = "Invalid parameters passed to method"
 Error message for when the parameters passed to the method are not correct.
 
static constexpr std::string_view err_method_not_found = "Method not found"
 Error message for when the method is not found.
 
static constexpr std::string_view err_not_jsonrpc_2_0_request = "Not a JSON-RPC 2.0 request"
 Error message for when the request is not a JSON-RPC 2.0 request.
 

Detailed Description

Library namespace.

Function Documentation

◆ from_json()

static void wwa::json_rpc::from_json ( const nlohmann::json & j,
jsonrpc_request & r )
static

Deserializes a JSON object into a jsonrpc_request structure.

Parameters
jThe JSON object to deserialize.
rThe jsonrpc_request structure to populate.

This function deserializes a JSON object into a jsonrpc_request structure.

  • It extracts the jsonrpc version, method name, params, and id from the JSON object.
  • If the params field is not present, it defaults to an empty array.
  • If the params field is an object, it is wrapped in an array.
Note
This function cannot be moved to an anonymous namespace because of Argument-Dependent Lookup (ADL).
See also
https://www.jsonrpc.org/specification#request_object
https://github.com/nlohmann/json?tab=readme-ov-file#arbitrary-types-conversions

Definition at line 56 of file dispatcher_p.cpp.

57{
58 r.params = nlohmann::json(nlohmann::json::value_t::discarded);
59 r.id = nlohmann::json(nlohmann::json::value_t::discarded);
60
61 j.at("jsonrpc").get_to(r.jsonrpc);
62 j.at("method").get_to(r.method);
63
64 if (j.contains("params")) {
65 j.at("params").get_to(r.params);
66 }
67
68 if (j.contains("id")) {
69 j.at("id").get_to(r.id);
70 }
71
72 if (r.params.is_discarded()) {
73 r.params = nlohmann::json::array();
74 }
75 else if (r.params.is_object()) {
76 r.params = nlohmann::json::array({r.params});
77 }
78}
std::string method
The name of the method to be invoked.
std::string jsonrpc
The JSON RPC version.
nlohmann::json id
The ID of the request.
nlohmann::json params
The parameters for the method.