Skip to content

Commit

Permalink
Use Mile.Json utility functions to simplify NanaGet.JsonRpc2.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouriNaruto committed Apr 26, 2024
1 parent 5d0a5b4 commit 12892dc
Showing 1 changed file with 20 additions and 51 deletions.
71 changes: 20 additions & 51 deletions NanaGet/NanaGet.JsonRpc2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,16 @@ NanaGet::JsonRpc2::ErrorMessage NanaGet::JsonRpc2::ToErrorMessage(
{
NanaGet::JsonRpc2::ErrorMessage Result;

try
{
Result.Code = Value.at("code").get<std::int64_t>();
}
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<std::string>();
}
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;
Expand All @@ -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<std::string>())
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<std::string>();
}
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;
}

0 comments on commit 12892dc

Please sign in to comment.