From c9377ad230c8756ba6df1277d8cb821aa6160a38 Mon Sep 17 00:00:00 2001 From: MouriNaruto Date: Sat, 6 Jul 2024 00:52:24 +0800 Subject: [PATCH] Add NanaGet::JsonRpc2::ToNotificationMessage. --- NanaGet/NanaGet.JsonRpc2.cpp | 27 +++++++++++++++++++++++++++ NanaGet/NanaGet.JsonRpc2.h | 25 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/NanaGet/NanaGet.JsonRpc2.cpp b/NanaGet/NanaGet.JsonRpc2.cpp index e4cbbf2..0dcc023 100644 --- a/NanaGet/NanaGet.JsonRpc2.cpp +++ b/NanaGet/NanaGet.JsonRpc2.cpp @@ -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) { diff --git a/NanaGet/NanaGet.JsonRpc2.h b/NanaGet/NanaGet.JsonRpc2.h index 3812ccf..8c2c47b 100644 --- a/NanaGet/NanaGet.JsonRpc2.h +++ b/NanaGet/NanaGet.JsonRpc2.h @@ -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. */