From 12892dcf90eae753a1cae44ad8cb574bc14d4f63 Mon Sep 17 00:00:00 2001 From: MouriNaruto Date: Fri, 26 Apr 2024 20:55:38 +0800 Subject: [PATCH] Use Mile.Json utility functions to simplify NanaGet.JsonRpc2. --- NanaGet/NanaGet.JsonRpc2.cpp | 71 ++++++++++-------------------------- 1 file changed, 20 insertions(+), 51 deletions(-) diff --git a/NanaGet/NanaGet.JsonRpc2.cpp b/NanaGet/NanaGet.JsonRpc2.cpp index 29bd420..e4cbbf2 100644 --- a/NanaGet/NanaGet.JsonRpc2.cpp +++ b/NanaGet/NanaGet.JsonRpc2.cpp @@ -53,31 +53,16 @@ NanaGet::JsonRpc2::ErrorMessage NanaGet::JsonRpc2::ToErrorMessage( { NanaGet::JsonRpc2::ErrorMessage Result; - try - { - Result.Code = Value.at("code").get(); - } - catch (...) - { + Result.Code = Mile::Json::ToInt64( + Mile::Json::GetSubKey(Value, "code")); - } + Result.Message = Mile::Json::ToString( + Mile::Json::GetSubKey(Value, "message")); - try - { - Result.Message = Value.at("message").get(); - } - catch (...) + nlohmann::json Data = Mile::Json::GetSubKey(Value, "data"); + if (!Data.is_null()) { - - } - - try - { - Result.Data = Value.at("data").dump(2); - } - catch (...) - { - + Result.Data = Data.dump(2); } return Result; @@ -98,48 +83,32 @@ bool NanaGet::JsonRpc2::ToResponseMessage( return false; } - if (!SourceJson.contains("jsonrpc")) + std::string JsonRpc = Mile::Json::ToString( + Mile::Json::GetSubKey(SourceJson, "jsonrpc")); + if ("2.0" != JsonRpc) { return false; } - if ("2.0" != SourceJson["jsonrpc"].get()) + nlohmann::json Identifier = Mile::Json::GetSubKey(SourceJson, "id"); + if (Identifier.is_null()) { return false; } + Destination.Identifier = Mile::Json::ToString(Identifier); - try - { - Destination.Identifier = SourceJson.at("id").get(); - } - catch (...) - { - return false; - } - - Destination.IsSucceeded = SourceJson.contains("result"); - if (Destination.IsSucceeded) - { - try - { - Destination.Message = SourceJson.at("result").dump(2); - } - catch (...) - { - - } - } - else + nlohmann::json Message = Mile::Json::GetSubKey(SourceJson, "result"); + Destination.IsSucceeded = !Message.is_null(); + if (!Destination.IsSucceeded) { - try - { - Destination.Message = SourceJson.at("error").dump(2); - } - catch (...) + Message = Mile::Json::GetSubKey(SourceJson, "error"); + if (Message.is_null()) { return false; } + } + Destination.Message = Message.dump(2); return true; }