update jsonrpc++ to v1.3.0

This commit is contained in:
badaix 2019-11-15 17:46:10 +01:00
parent e9dbb6c3b6
commit dbd0b018e2

View file

@ -3,7 +3,7 @@
_( )/ ___) / \ ( ( \( _ \( _ \ / __)( ) ( )
/ \) \\___ \( O )/ / ) / ) __/( (__(_ _)(_ _)
\____/(____/ \__/ \_)__)(__\_)(__) \___)(_) (_)
version 1.2.2
version 1.3.0
https://github.com/badaix/jsonrpcpp
This file is part of jsonrpc++
@ -19,8 +19,8 @@
/// run-clang-tidy-3.8.py -header-filter='jsonrpcpp.hpp'
/// -checks='*,-misc-definitions-in-headers,-google-readability-braces-around-statements,-readability-braces-around-statements,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-google-build-using-namespace,-google-build-using-namespace,-modernize-pass-by-value,-google-explicit-constructor'
#ifndef JSON_RPC_H
#define JSON_RPC_H
#ifndef JSON_RPC_HPP
#define JSON_RPC_HPP
#include "json.hpp"
#include <cstring>
@ -34,7 +34,6 @@ using Json = nlohmann::json;
namespace jsonrpcpp
{
class Entity;
class Request;
class Notification;
@ -43,15 +42,13 @@ class Response;
class Error;
class Batch;
typedef std::shared_ptr<Entity> entity_ptr;
typedef std::shared_ptr<Request> request_ptr;
typedef std::shared_ptr<Notification> notification_ptr;
typedef std::shared_ptr<Parameter> parameter_ptr;
typedef std::shared_ptr<Response> response_ptr;
typedef std::shared_ptr<Error> error_ptr;
typedef std::shared_ptr<Batch> batch_ptr;
using entity_ptr = std::shared_ptr<Entity>;
using request_ptr = std::shared_ptr<Request>;
using notification_ptr = std::shared_ptr<Notification>;
using parameter_ptr = std::shared_ptr<Parameter>;
using response_ptr = std::shared_ptr<Response>;
using error_ptr = std::shared_ptr<Error>;
using batch_ptr = std::shared_ptr<Batch>;
class Entity
@ -74,13 +71,13 @@ public:
Entity(const Entity&) = default;
Entity& operator=(const Entity&) = default;
bool is_exception();
bool is_id();
bool is_error();
bool is_response();
bool is_request();
bool is_notification();
bool is_batch();
bool is_exception() const;
bool is_id() const;
bool is_error() const;
bool is_response() const;
bool is_request() const;
bool is_notification() const;
bool is_batch() const;
virtual std::string type_str() const;
@ -95,7 +92,6 @@ protected:
};
class NullableEntity : public Entity
{
public:
@ -115,7 +111,6 @@ protected:
};
class Id : public Entity
{
public:
@ -163,7 +158,6 @@ protected:
};
class Parameter : public NullableEntity
{
public:
@ -225,7 +219,6 @@ public:
};
class Error : public NullableEntity
{
public:
@ -258,7 +251,6 @@ protected:
};
/// JSON-RPC 2.0 request
/**
* Simple jsonrpc 2.0 parser with getters
@ -295,7 +287,6 @@ protected:
};
class RpcException : public std::exception
{
public:
@ -309,7 +300,6 @@ protected:
};
class RpcEntityException : public RpcException, public Entity
{
public:
@ -328,7 +318,6 @@ protected:
};
class ParseErrorException : public RpcEntityException
{
public:
@ -338,7 +327,6 @@ public:
};
// -32600 Invalid Request The JSON sent is not a valid Request object.
// -32601 Method not found The method does not exist / is not available.
// -32602 Invalid params Invalid method parameter(s).
@ -361,7 +349,6 @@ protected:
};
class InvalidRequestException : public RequestException
{
public:
@ -372,7 +359,6 @@ public:
};
class MethodNotFoundException : public RequestException
{
public:
@ -383,7 +369,6 @@ public:
};
class InvalidParamsException : public RequestException
{
public:
@ -394,7 +379,6 @@ public:
};
class InternalErrorException : public RequestException
{
public:
@ -405,7 +389,6 @@ public:
};
class Response : public Entity
{
public:
@ -441,7 +424,6 @@ protected:
};
class Notification : public Entity
{
public:
@ -468,11 +450,9 @@ protected:
};
typedef std::function<void(const Parameter& params)> notification_callback;
typedef std::function<jsonrpcpp::response_ptr(const Id& id, const Parameter& params)> request_callback;
class Parser
{
public:
@ -502,7 +482,6 @@ private:
};
class Batch : public Entity
{
public:
@ -533,49 +512,41 @@ inline Entity::Entity(entity_t type) : entity(type)
{
}
inline bool Entity::is_exception()
inline bool Entity::is_exception() const
{
return (entity == entity_t::exception);
}
inline bool Entity::is_id()
inline bool Entity::is_id() const
{
return (entity == entity_t::id);
}
inline bool Entity::is_error()
inline bool Entity::is_error() const
{
return (entity == entity_t::error);
}
inline bool Entity::is_response()
inline bool Entity::is_response() const
{
return (entity == entity_t::response);
}
inline bool Entity::is_request()
inline bool Entity::is_request() const
{
return (entity == entity_t::request);
}
inline bool Entity::is_notification()
inline bool Entity::is_notification() const
{
return (entity == entity_t::notification);
}
inline bool Entity::is_batch()
inline bool Entity::is_batch() const
{
return (entity == entity_t::batch);
}
inline void Entity::parse(const char* json_str)
{
// http://www.jsonrpc.org/specification
@ -600,13 +571,11 @@ inline void Entity::parse(const char* json_str)
}
}
inline void Entity::parse(const std::string& json_str)
{
parse(json_str.c_str());
}
inline std::string Entity::type_str() const
{
switch (entity)
@ -633,48 +602,40 @@ inline std::string Entity::type_str() const
}
/////////////////////////// NullableEntity implementation /////////////////////
inline NullableEntity::NullableEntity(entity_t type) : Entity(type), isNull(false)
{
}
inline NullableEntity::NullableEntity(entity_t type, std::nullptr_t) : Entity(type), isNull(true)
{
}
/////////////////////////// Id implementation /////////////////////////////////
inline Id::Id() : Entity(entity_t::id), type_(value_t::null), int_id_(0), string_id_("")
{
}
inline Id::Id(int id) : Entity(entity_t::id), type_(value_t::integer), int_id_(id), string_id_("")
{
}
inline Id::Id(const char* id) : Entity(entity_t::id), type_(value_t::string), int_id_(0), string_id_(id)
{
}
inline Id::Id(const std::string& id) : Id(id.c_str())
{
}
inline Id::Id(const Json& json_id) : Entity(entity_t::id), type_(value_t::null)
{
Id::parse_json(json_id);
}
inline void Id::parse_json(const Json& json)
{
if (json.is_null())
@ -695,7 +656,6 @@ inline void Id::parse_json(const Json& json)
throw std::invalid_argument("id must be integer, string or null");
}
inline Json Id::to_json() const
{
if (type_ == value_t::null)
@ -709,21 +669,18 @@ inline Json Id::to_json() const
}
//////////////////////// Error implementation /////////////////////////////////
inline Parameter::Parameter(std::nullptr_t) : NullableEntity(entity_t::id, nullptr), type(value_t::null)
{
}
inline Parameter::Parameter(const Json& json) : NullableEntity(entity_t::id), type(value_t::null)
{
if (json != nullptr)
Parameter::parse_json(json);
}
inline Parameter::Parameter(const std::string& key1, const Json& value1, const std::string& key2, const Json& value2, const std::string& key3,
const Json& value3, const std::string& key4, const Json& value4)
: NullableEntity(entity_t::id), type(value_t::map)
@ -737,7 +694,6 @@ inline Parameter::Parameter(const std::string& key1, const Json& value1, const s
param_map[key4] = value4;
}
inline void Parameter::parse_json(const Json& json)
{
if (json.is_array())
@ -754,7 +710,6 @@ inline void Parameter::parse_json(const Json& json)
}
}
inline Json Parameter::to_json() const
{
if (type == value_t::array)
@ -765,25 +720,21 @@ inline Json Parameter::to_json() const
return nullptr;
}
inline bool Parameter::is_array() const
{
return type == value_t::array;
}
inline bool Parameter::is_map() const
{
return type == value_t::map;
}
inline bool Parameter::is_null() const
{
return isNull;
}
inline bool Parameter::has(const std::string& key) const
{
if (type != value_t::map)
@ -791,13 +742,11 @@ inline bool Parameter::has(const std::string& key) const
return (param_map.find(key) != param_map.end());
}
inline Json Parameter::get(const std::string& key) const
{
return param_map.at(key);
}
inline bool Parameter::has(size_t idx) const
{
if (type != value_t::array)
@ -805,14 +754,12 @@ inline bool Parameter::has(size_t idx) const
return (param_array.size() > idx);
}
inline Json Parameter::get(size_t idx) const
{
return param_array.at(idx);
}
//////////////////////// Error implementation /////////////////////////////////
inline Error::Error(const Json& json) : Error("Internal error", -32603, nullptr)
@ -821,17 +768,14 @@ inline Error::Error(const Json& json) : Error("Internal error", -32603, nullptr)
Error::parse_json(json);
}
inline Error::Error(std::nullptr_t) : NullableEntity(entity_t::error, nullptr), code_(0), message_(""), data_(nullptr)
{
}
inline Error::Error(const std::string& message, int code, const Json& data) : NullableEntity(entity_t::error), code_(code), message_(message), data_(data)
{
}
inline void Error::parse_json(const Json& json)
{
try
@ -857,7 +801,6 @@ inline void Error::parse_json(const Json& json)
}
}
inline Json Error::to_json() const
{
Json j = {
@ -870,7 +813,6 @@ inline Json Error::to_json() const
}
////////////////////// Request implementation /////////////////////////////////
inline Request::Request(const Json& json) : Entity(entity_t::request), method_(""), id_()
@ -879,12 +821,10 @@ inline Request::Request(const Json& json) : Entity(entity_t::request), method_("
Request::parse_json(json);
}
inline Request::Request(const Id& id, const std::string& method, const Parameter& params) : Entity(entity_t::request), method_(method), params_(params), id_(id)
{
}
inline void Request::parse_json(const Json& json)
{
try
@ -930,7 +870,6 @@ inline void Request::parse_json(const Json& json)
}
}
inline Json Request::to_json() const
{
Json json = {{"jsonrpc", "2.0"}, {"method", method_}, {"id", id_.to_json()}};
@ -942,7 +881,6 @@ inline Json Request::to_json() const
}
inline RpcException::RpcException(const char* text) : m_(text)
{
}
@ -957,7 +895,6 @@ inline const char* RpcException::what() const noexcept
}
inline RpcEntityException::RpcEntityException(const Error& error) : RpcException(error.message()), Entity(entity_t::exception), error_(error)
{
}
@ -967,7 +904,6 @@ inline void RpcEntityException::parse_json(const Json& /*json*/)
}
inline ParseErrorException::ParseErrorException(const Error& error) : RpcEntityException(error)
{
}
@ -984,7 +920,6 @@ inline Json ParseErrorException::to_json() const
}
inline RequestException::RequestException(const Error& error, const Id& requestId) : RpcEntityException(error), id_(requestId)
{
}
@ -997,7 +932,6 @@ inline Json RequestException::to_json() const
}
inline InvalidRequestException::InvalidRequestException(const Id& requestId) : RequestException(Error("Invalid request", -32600), requestId)
{
}
@ -1016,7 +950,6 @@ inline InvalidRequestException::InvalidRequestException(const std::string& data,
}
inline MethodNotFoundException::MethodNotFoundException(const Id& requestId) : RequestException(Error("Method not found", -32601), requestId)
{
}
@ -1035,7 +968,6 @@ inline MethodNotFoundException::MethodNotFoundException(const std::string& data,
}
inline InvalidParamsException::InvalidParamsException(const Id& requestId) : RequestException(Error("Invalid params", -32602), requestId)
{
}
@ -1054,7 +986,6 @@ inline InvalidParamsException::InvalidParamsException(const std::string& data, c
}
inline InternalErrorException::InternalErrorException(const Id& requestId) : RequestException(Error("Internal error", -32603), requestId)
{
}
@ -1073,7 +1004,6 @@ inline InternalErrorException::InternalErrorException(const std::string& data, c
}
///////////////////// Response implementation /////////////////////////////////
inline Response::Response(const Json& json) : Entity(entity_t::response)
@ -1082,32 +1012,26 @@ inline Response::Response(const Json& json) : Entity(entity_t::response)
Response::parse_json(json);
}
inline Response::Response(const Id& id, const Json& result) : Entity(entity_t::response), id_(id), result_(result), error_(nullptr)
{
}
inline Response::Response(const Id& id, const Error& error) : Entity(entity_t::response), id_(id), result_(), error_(error)
{
}
inline Response::Response(const Request& request, const Json& result) : Response(request.id(), result)
{
}
inline Response::Response(const Request& request, const Error& error) : Response(request.id(), error)
{
}
inline Response::Response(const RequestException& exception) : Response(exception.id(), exception.error())
{
}
inline void Response::parse_json(const Json& json)
{
try
@ -1125,7 +1049,7 @@ inline void Response::parse_json(const Json& json)
if (json.count("result") != 0u)
result_ = json["result"];
else if (json.count("error") != 0u)
error_.parse_json(json["error"]);
error_ = json["error"];
else
throw RpcException("response must contain result or error");
}
@ -1139,7 +1063,6 @@ inline void Response::parse_json(const Json& json)
}
}
inline Json Response::to_json() const
{
Json j = {
@ -1155,7 +1078,6 @@ inline Json Response::to_json() const
}
///////////////// Notification implementation /////////////////////////////////
inline Notification::Notification(const Json& json) : Entity(entity_t::notification)
@ -1164,17 +1086,14 @@ inline Notification::Notification(const Json& json) : Entity(entity_t::notificat
Notification::parse_json(json);
}
inline Notification::Notification(const char* method, const Parameter& params) : Entity(entity_t::notification), method_(method), params_(params)
{
}
inline Notification::Notification(const std::string& method, const Parameter& params) : Notification(method.c_str(), params)
{
}
inline void Notification::parse_json(const Json& json)
{
try
@ -1208,7 +1127,6 @@ inline void Notification::parse_json(const Json& json)
}
}
inline Json Notification::to_json() const
{
Json json = {
@ -1222,7 +1140,6 @@ inline Json Notification::to_json() const
}
//////////////////////// Batch implementation /////////////////////////////////
inline Batch::Batch(const Json& json) : Entity(entity_t::batch)
@ -1231,7 +1148,6 @@ inline Batch::Batch(const Json& json) : Entity(entity_t::batch)
Batch::parse_json(json);
}
inline void Batch::parse_json(const Json& json)
{
// cout << "Batch::parse: " << json.dump() << "\n";
@ -1260,7 +1176,6 @@ inline void Batch::parse_json(const Json& json)
throw InvalidRequestException();
}
inline Json Batch::to_json() const
{
Json result;
@ -1270,14 +1185,6 @@ inline Json Batch::to_json() const
}
/*void Batch::add(const entity_ptr entity)
{
entities.push_back(entity);
}
*/
//////////////////////// Parser implementation ////////////////////////////////
inline void Parser::register_notification_callback(const std::string& notification, notification_callback callback)
@ -1286,14 +1193,12 @@ inline void Parser::register_notification_callback(const std::string& notificati
notification_callbacks_[notification] = callback;
}
inline void Parser::register_request_callback(const std::string& request, request_callback callback)
{
if (callback)
request_callbacks_[request] = callback;
}
inline entity_ptr Parser::parse(const std::string& json_str)
{
// std::cout << "parse: " << json_str << "\n";
@ -1325,13 +1230,11 @@ inline entity_ptr Parser::parse(const std::string& json_str)
return entity;
}
inline entity_ptr Parser::parse_json(const Json& json)
{
return do_parse_json(json);
}
inline entity_ptr Parser::do_parse(const std::string& json_str)
{
try
@ -1350,7 +1253,6 @@ inline entity_ptr Parser::do_parse(const std::string& json_str)
return nullptr;
}
inline entity_ptr Parser::do_parse_json(const Json& json)
{
try
@ -1376,7 +1278,6 @@ inline entity_ptr Parser::do_parse_json(const Json& json)
return nullptr;
}
inline bool Parser::is_request(const std::string& json_str)
{
try
@ -1389,13 +1290,11 @@ inline bool Parser::is_request(const std::string& json_str)
}
}
inline bool Parser::is_request(const Json& json)
{
return ((json.count("method") != 0u) && (json.count("id") != 0u));
}
inline bool Parser::is_notification(const std::string& json_str)
{
try
@ -1408,13 +1307,11 @@ inline bool Parser::is_notification(const std::string& json_str)
}
}
inline bool Parser::is_notification(const Json& json)
{
return ((json.count("method") != 0u) && (json.count("id") == 0));
}
inline bool Parser::is_response(const std::string& json_str)
{
try
@ -1427,13 +1324,11 @@ inline bool Parser::is_response(const std::string& json_str)
}
}
inline bool Parser::is_response(const Json& json)
{
return ((json.count("result") != 0u) && (json.count("id") != 0u));
return (((json.count("result") != 0u) || (json.count("error") != 0u)) && (json.count("id") != 0u));
}
inline bool Parser::is_batch(const std::string& json_str)
{
try
@ -1446,15 +1341,11 @@ inline bool Parser::is_batch(const std::string& json_str)
}
}
inline bool Parser::is_batch(const Json& json)
{
return (json.is_array());
}
} // namespace jsonrpcpp
#endif