From e5607f09f184b23e6c33330c568c9082fd49c8d9 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Tue, 15 Dec 2020 17:45:24 +0800 Subject: [PATCH] Replace Newtonsoft.Json to System.Text.Json. #740 --- .../AmazonSQSConsumerClient.cs | 4 +-- .../DotNetCore.CAP.Dashboard.csproj | 2 -- .../JsonDispatcher.cs | 21 +++--------- src/DotNetCore.CAP.Dashboard/JsonStats.cs | 19 ++--------- .../Pages/HomePage.cshtml | 10 +++--- .../Pages/HomePage.generated.cs | 10 +++--- src/DotNetCore.CAP/DotNetCore.CAP.csproj | 2 +- .../Serialization/ISerializer.JsonUtf8.cs | 32 +++++++++++-------- 8 files changed, 39 insertions(+), 61 deletions(-) diff --git a/src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs b/src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs index 75b933a..82595d9 100644 --- a/src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs +++ b/src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Amazon.SimpleNotificationService; @@ -14,7 +15,6 @@ using Amazon.SQS.Model; using DotNetCore.CAP.Messages; using DotNetCore.CAP.Transport; using Microsoft.Extensions.Options; -using Newtonsoft.Json; using Headers = DotNetCore.CAP.Messages.Headers; namespace DotNetCore.CAP.AmazonSQS @@ -83,7 +83,7 @@ namespace DotNetCore.CAP.AmazonSQS if (response.Messages.Count == 1) { - var messageObj = JsonConvert.DeserializeObject(response.Messages[0].Body); + var messageObj = JsonSerializer.Deserialize(response.Messages[0].Body); var header = messageObj.MessageAttributes.ToDictionary(x => x.Key, x => x.Value.Value); var body = messageObj.Message; diff --git a/src/DotNetCore.CAP.Dashboard/DotNetCore.CAP.Dashboard.csproj b/src/DotNetCore.CAP.Dashboard/DotNetCore.CAP.Dashboard.csproj index 1db04ea..d66e95d 100644 --- a/src/DotNetCore.CAP.Dashboard/DotNetCore.CAP.Dashboard.csproj +++ b/src/DotNetCore.CAP.Dashboard/DotNetCore.CAP.Dashboard.csproj @@ -40,8 +40,6 @@ - - diff --git a/src/DotNetCore.CAP.Dashboard/JsonDispatcher.cs b/src/DotNetCore.CAP.Dashboard/JsonDispatcher.cs index aad6d42..6f45a33 100644 --- a/src/DotNetCore.CAP.Dashboard/JsonDispatcher.cs +++ b/src/DotNetCore.CAP.Dashboard/JsonDispatcher.cs @@ -2,10 +2,8 @@ // Licensed under the MIT License. See License.txt in the project root for license information. using System; -using System.Threading.Tasks; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Newtonsoft.Json.Serialization; +using System.Text.Json; +using System.Threading.Tasks; namespace DotNetCore.CAP.Dashboard { @@ -30,19 +28,8 @@ namespace DotNetCore.CAP.Dashboard if (_command != null) { var result = _command(context); - - var settings = new JsonSerializerSettings - { - ContractResolver = new CamelCasePropertyNamesContractResolver(), - Converters = new JsonConverter[] - { - new StringEnumConverter - { - NamingStrategy = new CamelCaseNamingStrategy() - } - } - }; - serialized = JsonConvert.SerializeObject(result, settings); + + serialized = JsonSerializer.Serialize(result); } if (_jsonCommand != null) diff --git a/src/DotNetCore.CAP.Dashboard/JsonStats.cs b/src/DotNetCore.CAP.Dashboard/JsonStats.cs index 35dbf0a..de42c33 100644 --- a/src/DotNetCore.CAP.Dashboard/JsonStats.cs +++ b/src/DotNetCore.CAP.Dashboard/JsonStats.cs @@ -3,10 +3,8 @@ using System.Collections.Generic; using System.Linq; +using System.Text.Json; using System.Threading.Tasks; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; -using Newtonsoft.Json.Serialization; namespace DotNetCore.CAP.Dashboard { @@ -26,19 +24,8 @@ namespace DotNetCore.CAP.Dashboard var value = metric.Func(page); result.Add(metric.Name, value); } - - var settings = new JsonSerializerSettings - { - ContractResolver = new CamelCasePropertyNamesContractResolver(), - Converters = new JsonConverter[] - { - new StringEnumConverter - { - NamingStrategy = new CamelCaseNamingStrategy() - } - } - }; - var serialized = JsonConvert.SerializeObject(result, settings); + + var serialized = JsonSerializer.Serialize(result); context.Response.ContentType = "application/json"; await context.Response.WriteAsync(serialized); diff --git a/src/DotNetCore.CAP.Dashboard/Pages/HomePage.cshtml b/src/DotNetCore.CAP.Dashboard/Pages/HomePage.cshtml index 91b840d..8c40ab3 100644 --- a/src/DotNetCore.CAP.Dashboard/Pages/HomePage.cshtml +++ b/src/DotNetCore.CAP.Dashboard/Pages/HomePage.cshtml @@ -1,8 +1,8 @@ @* Generator: Template TypeVisibility: Internal GeneratePrettyNames: True *@ +@using System.Text.Json @using DotNetCore.CAP.Dashboard.Pages @using DotNetCore.CAP.Dashboard.Resources @using DotNetCore.CAP.Messages -@using Newtonsoft.Json @inherits DotNetCore.CAP.Dashboard.RazorPage @{ Layout = new LayoutPage(Strings.HomePage_Title); @@ -52,12 +52,12 @@
diff --git a/src/DotNetCore.CAP.Dashboard/Pages/HomePage.generated.cs b/src/DotNetCore.CAP.Dashboard/Pages/HomePage.generated.cs index 060844e..6ee090e 100644 --- a/src/DotNetCore.CAP.Dashboard/Pages/HomePage.generated.cs +++ b/src/DotNetCore.CAP.Dashboard/Pages/HomePage.generated.cs @@ -35,7 +35,7 @@ namespace DotNetCore.CAP.Dashboard.Pages #line hidden #line 5 "..\..\Pages\HomePage.cshtml" - using Newtonsoft.Json; + using System.Text.Json; #line default #line hidden @@ -252,7 +252,7 @@ WriteLiteral("\r\n \r\n\r\n
- + \ No newline at end of file diff --git a/src/DotNetCore.CAP/Serialization/ISerializer.JsonUtf8.cs b/src/DotNetCore.CAP/Serialization/ISerializer.JsonUtf8.cs index 70ab10e..ec02bfc 100644 --- a/src/DotNetCore.CAP/Serialization/ISerializer.JsonUtf8.cs +++ b/src/DotNetCore.CAP/Serialization/ISerializer.JsonUtf8.cs @@ -2,11 +2,10 @@ // Licensed under the MIT License. See License.txt in the project root for license information. using System; -using System.Text; +using System.Buffers; +using System.Text.Json; using System.Threading.Tasks; using DotNetCore.CAP.Messages; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace DotNetCore.CAP.Serialization { @@ -24,8 +23,8 @@ namespace DotNetCore.CAP.Serialization return Task.FromResult(new TransportMessage(message.Headers, null)); } - var json = JsonConvert.SerializeObject(message.Value); - return Task.FromResult(new TransportMessage(message.Headers, Encoding.UTF8.GetBytes(json))); + var jsonBytes = JsonSerializer.SerializeToUtf8Bytes(message.Value); + return Task.FromResult(new TransportMessage(message.Headers, jsonBytes)); } public Task DeserializeAsync(TransportMessage transportMessage, Type valueType) @@ -35,32 +34,39 @@ namespace DotNetCore.CAP.Serialization return Task.FromResult(new Message(transportMessage.Headers, null)); } - var json = Encoding.UTF8.GetString(transportMessage.Body); - return Task.FromResult(new Message(transportMessage.Headers, JsonConvert.DeserializeObject(json, valueType))); + var obj = JsonSerializer.Deserialize(transportMessage.Body, valueType); + + return Task.FromResult(new Message(transportMessage.Headers, obj)); } public string Serialize(Message message) { - return JsonConvert.SerializeObject(message); + return JsonSerializer.Serialize(message); } public Message Deserialize(string json) { - return JsonConvert.DeserializeObject(json); + return JsonSerializer.Deserialize(json); } public object Deserialize(object value, Type valueType) { - if (value is JToken jToken) + if (value is JsonElement jToken) { - return jToken.ToObject(valueType); + var bufferWriter = new ArrayBufferWriter(); + using (var writer = new Utf8JsonWriter(bufferWriter)) + { + jToken.WriteTo(writer); + } + return JsonSerializer.Deserialize(bufferWriter.WrittenSpan, valueType); } throw new NotSupportedException("Type is not of type JToken"); } public bool IsJsonType(object jsonObject) { - return jsonObject is JToken; + return jsonObject is JsonElement; } - } + + } } \ No newline at end of file