Skip to content

Commit

Permalink
Add NanaGet::JsonRpc2::ToNotificationMessage.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouriNaruto committed Jul 5, 2024
1 parent 5eff0fb commit c9377ad
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
27 changes: 27 additions & 0 deletions NanaGet/NanaGet.JsonRpc2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ std::string NanaGet::JsonRpc2::FromRequestMessage(
}
}

bool NanaGet::JsonRpc2::ToNotificationMessage(
std::string const& Source,
NanaGet::JsonRpc2::NotificationMessage& Destination)
{
nlohmann::json SourceJson;

try
{
SourceJson = nlohmann::json::parse(Source);
}
catch (...)
{
return false;
}

Destination.Method = Mile::Json::ToString(
Mile::Json::GetSubKey(SourceJson, "method"));

nlohmann::json Parameters = Mile::Json::GetSubKey(SourceJson, "params");
if (!Parameters.is_null())
{
Destination.Parameters = Parameters.dump(2);
}

return true;
}

std::string NanaGet::JsonRpc2::FromErrorMessage(
NanaGet::JsonRpc2::ErrorMessage const& Value)
{
Expand Down
25 changes: 25 additions & 0 deletions NanaGet/NanaGet.JsonRpc2.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ namespace NanaGet::JsonRpc2
std::string FromRequestMessage(
RequestMessage const& Value);

/**
* @brief The notification message of a JSON-RPC 2.0 call.
*/
struct NotificationMessage
{
/**
* @brief A string containing the name of the method to be invoked.
* method names that begin with the word rpc followed by a period
* character (U+002E or ASCII 46) are reserved for rpc-internal
* methods and extensions and MUST NOT be used for anything else.
*/
std::string Method;

/**
* @brief A structured JSON value that holds the parameter values to be
* used during the invocation of the method. This member MAY be
* omitted.
*/
std::string Parameters;
};

bool ToNotificationMessage(
std::string const& Source,
NotificationMessage& Destination);

/**
* @brief The message returned when a JSON-RPC 2.0 call encounters an error.
*/
Expand Down

0 comments on commit c9377ad

Please sign in to comment.