58 r.params = nlohmann::json(nlohmann::json::value_t::discarded);
59 r.id = nlohmann::json(nlohmann::json::value_t::discarded);
61 j.at(
"jsonrpc").get_to(r.jsonrpc);
62 j.at(
"method").get_to(r.method);
64 if (j.contains(
"params")) {
65 j.at(
"params").get_to(r.params);
68 if (j.contains(
"id")) {
69 j.at(
"id").get_to(r.id);
72 if (r.params.is_discarded()) {
73 r.params = nlohmann::json::array();
75 else if (r.params.is_object()) {
76 r.params = nlohmann::json::array({r.params});
104 if (request.empty()) {
105 this->q_ptr->on_request(extra);
106 this->q_ptr->on_request_processed({}, exception::INVALID_REQUEST, extra);
107 return dispatcher_private::generate_error_response(
108 exception(exception::INVALID_REQUEST, err_empty_batch), nlohmann::json(
nullptr)
112 auto response = nlohmann::json::array();
113 for (
const auto& req : request) {
114 if (!req.is_object()) {
115 this->q_ptr->on_request(extra);
116 const auto r = dispatcher_private::generate_error_response(
117 exception(exception::INVALID_REQUEST, err_not_jsonrpc_2_0_request), nlohmann::json(
nullptr)
120 response.push_back(r);
121 this->q_ptr->on_request_processed({}, exception::INVALID_REQUEST, extra);
123 else if (
const auto res =
this->process_request(req, extra); !res.is_discarded()) {
124 response.push_back(res);
128 return response.empty() ? nlohmann::json(nlohmann::json::value_t::discarded) : response;
134 if (request.is_array()) {
135 return this->process_batch_request(request, extra);
138 this->q_ptr->on_request(extra);
139 auto request_id = request.contains(
"id") ? request[
"id"] : nlohmann::json(
nullptr);
140 if (!is_valid_request_id(request_id)) {
141 request_id = nlohmann::json(
nullptr);
145 nlohmann::json extra_data = extra;
147 nlohmann::json extra_fields;
148 auto req = dispatcher_private::parse_request(request, extra_fields);
149 dispatcher_private::validate_request(req);
153 if (extra.is_object() && !extra_fields.empty()) {
154 extra_data[
"extra"] = extra_fields;
157 const auto res =
this->invoke(method, req.params, extra_data);
158 if (!req.id.is_discarded()) {
159 return nlohmann::json({{
"jsonrpc",
"2.0"}, {
"result", res}, {
"id", req.id}});
163 return nlohmann::json(nlohmann::json::value_t::discarded);
166 this->q_ptr->on_request_processed(method, e.code(), extra_data);
167 return request_id.is_discarded() ? nlohmann::json(nlohmann::json::value_t::discarded)
168 : dispatcher_private::generate_error_response(e, request_id);
170 catch (
const std::exception& e) {
171 this->q_ptr->on_request_processed(method, exception::INTERNAL_ERROR, extra_data);
172 return request_id.is_discarded() ? nlohmann::json(nlohmann::json::value_t::discarded)
173 : dispatcher_private::generate_error_response(
174 exception(exception::INTERNAL_ERROR, e.what()), request_id
181 if (r.jsonrpc !=
"2.0") {
182 throw json_rpc::exception(json_rpc::exception::INVALID_REQUEST, json_rpc::err_not_jsonrpc_2_0_request);
185 if (!r.params.is_array()) {
186 throw json_rpc::exception(json_rpc::exception::INVALID_PARAMS, json_rpc::err_bad_params_type);
189 if (r.method.empty()) {
190 throw json_rpc::exception(json_rpc::exception::INVALID_REQUEST, json_rpc::err_empty_method);
193 if (!is_valid_request_id(r.id)) {
194 throw json_rpc::exception(json_rpc::exception::INVALID_REQUEST, json_rpc::err_bad_id_type);
206 if (
const auto it =
this->m_methods.find(method); it !=
this->m_methods.end()) {
207 this->q_ptr->on_method(method, extra);
208 const auto response = it->second(extra, params);
209 this->q_ptr->on_request_processed(method, 0, extra);
213 throw exception(exception::METHOD_NOT_FOUND, err_method_not_found);