139 lines
4.5 KiB
C#
139 lines
4.5 KiB
C#
using Flurl.Http;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Diagnostics;
|
|
|
|
namespace QuickTools
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private async void btnEncode_Click(object sender, EventArgs e)
|
|
{
|
|
if (txtProtoId.Text.Trim() == string.Empty)
|
|
{
|
|
MessageBox.Show("ProtocolID 不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
int protocolId;
|
|
try
|
|
{
|
|
protocolId = Convert.ToInt32(txtProtoId.Text.Trim());
|
|
}
|
|
catch
|
|
{
|
|
MessageBox.Show("ProtocolID 只能为数字!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
var url = "http://localhost:9999/api/v1/emoney/converter/request/encode";
|
|
try
|
|
{
|
|
string r = await url.PostJsonAsync(new
|
|
{
|
|
protocolId,
|
|
protocolBody = txtJson.Text,
|
|
}).Result.Content.ReadAsStringAsync();
|
|
if (r != null)
|
|
{
|
|
JObject jo = JObject.Parse(r);
|
|
if (jo["code"].Value<int>() == 0)
|
|
{
|
|
txtProto.Text = jo["result"].Value<string>();
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(jo["msg"].Value<string>(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine(ex.StackTrace);
|
|
MessageBox.Show($"转换 JSON 为 Protobuf 请求出现错误:{ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
}
|
|
|
|
private async void btnDecode_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
if (txtProtoId.Text.Trim() == string.Empty)
|
|
{
|
|
MessageBox.Show("ProtocolID 不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
int protocolId;
|
|
try
|
|
{
|
|
protocolId = Convert.ToInt32(txtProtoId.Text.Trim());
|
|
}
|
|
catch
|
|
{
|
|
MessageBox.Show("ProtocolID 只能为数字!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
var url = "http://localhost:9999/api/v1/emoney/converter/request/decode";
|
|
try
|
|
{
|
|
string r = await url.PostJsonAsync(new
|
|
{
|
|
protocolId,
|
|
protocolBody = txtProto.Text,
|
|
}).Result.Content.ReadAsStringAsync();
|
|
if (r != null)
|
|
{
|
|
JObject jo = JObject.Parse(r);
|
|
if (jo["code"].Value<int>() == 0)
|
|
{
|
|
txtJson.Text = ClearSizeInfo(jo["result"]).ToString();
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(jo["msg"].Value<string>(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine(ex.StackTrace);
|
|
MessageBox.Show($"转换 Protobuf 为 JSON 请求出现错误:{ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
}
|
|
|
|
public static JToken ClearSizeInfo(JToken j)
|
|
{
|
|
if (j == null) return null;
|
|
if (j is JArray ja)
|
|
{
|
|
for (int i = 0; i < ja.Count; i++)
|
|
{
|
|
ja[i] = ClearSizeInfo(ja[i]);
|
|
}
|
|
return ja;
|
|
}
|
|
if (j is JObject jo)
|
|
{
|
|
if (jo.ContainsKey("cachedSize"))
|
|
{
|
|
jo.Remove("cachedSize");
|
|
}
|
|
if (jo.ContainsKey("serializedSize"))
|
|
{
|
|
jo.Remove("serializedSize");
|
|
}
|
|
foreach (var p in jo)
|
|
{
|
|
string key = p.Key;
|
|
JToken val = p.Value;
|
|
jo[key] = ClearSizeInfo(val);
|
|
}
|
|
return jo;
|
|
}
|
|
return j;
|
|
}
|
|
}
|
|
|
|
|
|
} |