Skip to content

Commit 39aa77c

Browse files
committed
将Newtonsoft.Json库替换为System.Text.Json库。
1 parent cf50636 commit 39aa77c

File tree

16 files changed

+76
-58
lines changed

16 files changed

+76
-58
lines changed

QpTestClient/ConnectForm.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json;
22
using Quick.Protocol;
33
using Quick.Protocol.Utils;
44
using Quick.Xml;
@@ -58,8 +58,8 @@ private void cbConnectType_SelectedIndexChanged(object sender, EventArgs e)
5858
QpClientOptions options = null;
5959
if (ConnectionInfo != null && qpClientTypeInfo.QpClientType.FullName == ConnectionInfo.QpClientTypeName)
6060
{
61-
options = (QpClientOptions)JsonConvert.DeserializeObject(
62-
JsonConvert.SerializeObject(ConnectionInfo.QpClientOptions),
61+
options = (QpClientOptions)JsonSerializer.Deserialize(
62+
JsonSerializer.Serialize(ConnectionInfo.QpClientOptions),
6363
qpClientTypeInfo.QpClientOptionsType);
6464
}
6565
else

Quick.Protocol.Tcp/QpTcpServerOptions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using Newtonsoft.Json;
2-
using System;
3-
using System.Collections.Generic;
1+
using System;
42
using System.Net;
5-
using System.Text;
3+
using System.Text.Json.Serialization;
64

75
namespace Quick.Protocol.Tcp
86
{

Quick.Protocol.WebSocket.Server.AspNetCore/QpWebSocketServerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json;
22
using System;
33
using System.Collections.Generic;
44
using System.Net;

Quick.Protocol.WebSocket.Server.AspNetCore/QuickProtocolMiddlewareExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Microsoft.AspNetCore.Http;
2-
using Newtonsoft.Json;
2+
using System.Text.Json;
33
using Quick.Protocol;
44
using Quick.Protocol.WebSocket.Server.AspNetCore;
55
using System;

Quick.Protocol/Commands/Authenticate/Request.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using System.Text;
1+
using System.ComponentModel;
52

63
namespace Quick.Protocol.Commands.Authenticate
74
{

Quick.Protocol/QpChannel.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json;
22
using Quick.Protocol.Exceptions;
33
using Quick.Protocol.Utils;
44
using System;
@@ -507,7 +507,7 @@ public Task SendNoticePackage(string noticePackageTypeName, string noticePackage
507507
/// </summary>
508508
public Task SendNoticePackage(object package)
509509
{
510-
return SendNoticePackage(package.GetType().FullName, JsonConvert.SerializeObject(package));
510+
return SendNoticePackage(package.GetType().FullName, JsonSerializer.Serialize(package));
511511
}
512512

513513
/// <summary>
@@ -519,7 +519,7 @@ public async Task SendCommandRequest(object request)
519519
{
520520
var requestType = request.GetType();
521521
var typeName = requestType.FullName;
522-
var requestContent = JsonConvert.SerializeObject(request);
522+
var requestContent = JsonSerializer.Serialize(request);
523523
await SendCommandRequestPackage(CommandContext.GenerateNewId(), typeName, requestContent).ConfigureAwait(false);
524524
}
525525

@@ -840,7 +840,7 @@ protected void OnRawNoticePackageReceived(string typeName, string content)
840840
//如果在字典中未找到此类型名称,则直接返回
841841
if (!noticeTypeDict.ContainsKey(typeName))
842842
return;
843-
var contentModel = JsonConvert.DeserializeObject(content, noticeTypeDict[typeName]);
843+
var contentModel = JsonSerializer.Deserialize(content, noticeTypeDict[typeName]);
844844

845845
//处理通知
846846
var hasNoticeHandler = false;
@@ -895,7 +895,7 @@ private void OnCommandRequestReceived(string commandId, string typeName, string
895895
var cmdRequestType = commandRequestTypeDict[typeName];
896896
var cmdResponseType = commandRequestTypeResponseTypeDict[cmdRequestType];
897897

898-
var contentModel = JsonConvert.DeserializeObject(content, cmdRequestType);
898+
var contentModel = JsonSerializer.Deserialize(content, cmdRequestType);
899899
CommandRequestPackageReceived?.Invoke(this, new CommandRequestPackageReceivedEventArgs()
900900
{
901901
CommandId = commandId,
@@ -911,7 +911,7 @@ private void OnCommandRequestReceived(string commandId, string typeName, string
911911
{
912912
hasCommandExecuter = true;
913913
var responseModel = commandExecuterManager.ExecuteCommand(this, typeName, contentModel);
914-
SendCommandResponsePackage(commandId, 0, null, cmdResponseType.FullName, JsonConvert.SerializeObject(responseModel));
914+
SendCommandResponsePackage(commandId, 0, null, cmdResponseType.FullName, JsonSerializer.Serialize(responseModel));
915915
break;
916916
}
917917
}
@@ -1157,7 +1157,7 @@ public async Task<TCmdResponse> SendCommand<TCmdResponse>(IQpCommandRequest<TCmd
11571157
{
11581158
var requestType = request.GetType();
11591159
var typeName = requestType.FullName;
1160-
var requestContent = JsonConvert.SerializeObject(request);
1160+
var requestContent = JsonSerializer.Serialize(request, requestType);
11611161

11621162
var commandContext = new CommandContext(typeName);
11631163
commandDict.TryAdd(commandContext.Id, commandContext);
@@ -1192,7 +1192,7 @@ await Task.Run(() => SendCommandRequestPackage(commandContext.Id, typeName, requ
11921192
.WaitAsync(TimeSpan.FromMilliseconds(timeout))
11931193
.ConfigureAwait(false);
11941194
}
1195-
return JsonConvert.DeserializeObject<TCmdResponse>(ret.Content);
1195+
return JsonSerializer.Deserialize<TCmdResponse>(ret.Content);
11961196
}
11971197
}
11981198
}

Quick.Protocol/QpChannelOptions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using Newtonsoft.Json;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.ComponentModel;
54
using System.Linq;
6-
using System.Text;
5+
using System.Text.Json.Serialization;
76

87
namespace Quick.Protocol
98
{

Quick.Protocol/QpClientOptions.cs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using Newtonsoft.Json;
2-
using Newtonsoft.Json.Linq;
3-
using System;
1+
using System;
42
using System.Collections.Generic;
53
using System.ComponentModel;
64
using System.Text;
5+
using System.Text.Json;
6+
using System.Text.Json.Nodes;
77

88
namespace Quick.Protocol
99
{
@@ -71,12 +71,43 @@ protected virtual void LoadFromUri(Uri uri)
7171
if (string.IsNullOrEmpty(uri.Query))
7272
return;
7373
var queryString = System.Web.HttpUtility.ParseQueryString(uri.Query);
74-
JObject jObj = new JObject();
74+
var type = this.GetType();
7575
foreach (var key in queryString.AllKeys)
7676
{
77-
jObj.Add(key, queryString[key]);
77+
var pi = type.GetProperty(key);
78+
if (pi == null)
79+
continue;
80+
var propertyType = pi.PropertyType;
81+
var stringValue = queryString[key];
82+
object propertyValue = stringValue;
83+
if (propertyType == typeof(bool))
84+
propertyValue = Convert.ToBoolean(stringValue);
85+
else if (propertyType == typeof(byte))
86+
propertyValue = Convert.ToByte(stringValue);
87+
else if (propertyType == typeof(sbyte))
88+
propertyValue = Convert.ToSByte(stringValue);
89+
else if (propertyType == typeof(short))
90+
propertyValue = Convert.ToInt16(stringValue);
91+
else if (propertyType == typeof(ushort))
92+
propertyValue = Convert.ToUInt16(stringValue);
93+
else if (propertyType == typeof(int))
94+
propertyValue = Convert.ToInt32(stringValue);
95+
else if (propertyType == typeof(uint))
96+
propertyValue = Convert.ToUInt32(stringValue);
97+
else if (propertyType == typeof(long))
98+
propertyValue = Convert.ToInt64(stringValue);
99+
else if (propertyType == typeof(ulong))
100+
propertyValue = Convert.ToUInt64(stringValue);
101+
else if (propertyType == typeof(float))
102+
propertyValue = Convert.ToSingle(stringValue);
103+
else if (propertyType == typeof(double))
104+
propertyValue = Convert.ToDouble(stringValue);
105+
else if (propertyType == typeof(decimal))
106+
propertyValue = Convert.ToDecimal(stringValue);
107+
else if (propertyType == typeof(DateTime))
108+
propertyValue = Convert.ToDateTime(stringValue);
109+
pi.SetValue(this, propertyValue);
78110
}
79-
JsonConvert.PopulateObject(jObj.ToString(), this);
80111
}
81112

82113
protected abstract string ToUriBasic(HashSet<string> ignorePropertyNames);
@@ -93,26 +124,25 @@ public Uri ToUri(bool includePassword = false, bool includeOtherProperty = false
93124
{
94125
StringBuilder sb = new StringBuilder(baseUrl);
95126
int currentIndex = 0;
96-
97-
var jObj = JObject.FromObject(this);
98-
foreach (var property in jObj.Properties())
127+
var jObj = JsonNode.Parse(JsonSerializer.Serialize(this)).AsObject();
128+
foreach (var property in jObj)
99129
{
100-
var key = property.Name;
130+
var key = property.Key;
101131
if (ignorePropertyNames.Contains(key))
102132
continue;
103-
if (!includeOtherProperty && key!=nameof(Password))
133+
if (!includeOtherProperty && key != nameof(Password))
104134
continue;
105-
if (currentIndex==0)
135+
if (currentIndex == 0)
106136
sb.Append("?");
107-
if (currentIndex>0)
137+
if (currentIndex > 0)
108138
sb.Append("&");
109139
currentIndex++;
110140

111141
var value = property.Value.ToString();
112-
value=System.Web.HttpUtility.UrlEncode(value);
142+
value = System.Web.HttpUtility.UrlEncode(value);
113143
sb.Append($"{key}={value}");
114144
}
115-
baseUrl=sb.ToString();
145+
baseUrl = sb.ToString();
116146
}
117147
Uri uri = new Uri(baseUrl);
118148
return uri;

Quick.Protocol/QpCommandInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json;
22
using System;
33
using System.Collections.Generic;
44
using System.ComponentModel;
@@ -63,11 +63,11 @@ public QpCommandInfo(string name, string description,
6363

6464
this.requestType = requestType;
6565
RequestTypeName = requestType.FullName;
66-
RequestTypeSchemaSample = JsonConvert.SerializeObject(defaultRequestTypeInstance, Formatting.Indented);
66+
RequestTypeSchemaSample = JsonSerializer.Serialize(defaultRequestTypeInstance, new JsonSerializerOptions() { WriteIndented = true });
6767

6868
this.responseType = responseType;
6969
ResponseTypeName = responseType.FullName;
70-
ResponseTypeSchemaSample = JsonConvert.SerializeObject(defaultResponseTypeInstance, Formatting.Indented);
70+
ResponseTypeSchemaSample = JsonSerializer.Serialize(defaultResponseTypeInstance, new JsonSerializerOptions() { WriteIndented = true });
7171
}
7272

7373
/// <summary>

Quick.Protocol/QpInstruction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json;
22
using System;
33
using System.Collections.Generic;
44
using System.ComponentModel;

0 commit comments

Comments
 (0)