Browse Source

Replace Newtonsoft.Json to System.Text.Json. #740

master
Savorboard 4 years ago
parent
commit
e5607f09f1
8 changed files with 39 additions and 61 deletions
  1. +2
    -2
      src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs
  2. +0
    -2
      src/DotNetCore.CAP.Dashboard/DotNetCore.CAP.Dashboard.csproj
  3. +4
    -17
      src/DotNetCore.CAP.Dashboard/JsonDispatcher.cs
  4. +3
    -16
      src/DotNetCore.CAP.Dashboard/JsonStats.cs
  5. +5
    -5
      src/DotNetCore.CAP.Dashboard/Pages/HomePage.cshtml
  6. +5
    -5
      src/DotNetCore.CAP.Dashboard/Pages/HomePage.generated.cs
  7. +1
    -1
      src/DotNetCore.CAP/DotNetCore.CAP.csproj
  8. +19
    -13
      src/DotNetCore.CAP/Serialization/ISerializer.JsonUtf8.cs

+ 2
- 2
src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs View File

@@ -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<SQSReceivedMessage>(response.Messages[0].Body);
var messageObj = JsonSerializer.Deserialize<SQSReceivedMessage>(response.Messages[0].Body);

var header = messageObj.MessageAttributes.ToDictionary(x => x.Key, x => x.Value.Value);
var body = messageObj.Message;


+ 0
- 2
src/DotNetCore.CAP.Dashboard/DotNetCore.CAP.Dashboard.csproj View File

@@ -40,8 +40,6 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Consul" Version="1.6.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
</ItemGroup>

<ItemGroup>


+ 4
- 17
src/DotNetCore.CAP.Dashboard/JsonDispatcher.cs View File

@@ -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)


+ 3
- 16
src/DotNetCore.CAP.Dashboard/JsonStats.cs View File

@@ -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);


+ 5
- 5
src/DotNetCore.CAP.Dashboard/Pages/HomePage.cshtml View File

@@ -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 @@
</h3>

<div id="historyGraph"
data-published-succeeded="@JsonConvert.SerializeObject(publishedSucceeded)"
data-published-failed="@JsonConvert.SerializeObject(publishedFailed)"
data-published-succeeded="@JsonSerializer.Serialize(publishedSucceeded)"
data-published-failed="@JsonSerializer.Serialize(publishedFailed)"
data-published-succeeded-string="@Strings.HomePage_GraphHover_PSucceeded"
data-published-failed-string="@Strings.HomePage_GraphHover_PFailed"
data-received-succeeded="@JsonConvert.SerializeObject(receivedSucceeded)"
data-received-failed="@JsonConvert.SerializeObject(receivedFailed)"
data-received-succeeded="@JsonSerializer.Serialize(receivedSucceeded)"
data-received-failed="@JsonSerializer.Serialize(receivedFailed)"
data-received-succeeded-string="@Strings.HomePage_GraphHover_RSucceeded"
data-received-failed-string="@Strings.HomePage_GraphHover_RFailed">
</div>


+ 5
- 5
src/DotNetCore.CAP.Dashboard/Pages/HomePage.generated.cs View File

@@ -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 </h3>\r\n\r\n <div id=\"historyGraph\"\r\n

#line 55 "..\..\Pages\HomePage.cshtml"
Write(JsonConvert.SerializeObject(publishedSucceeded));
Write(JsonSerializer.Serialize(publishedSucceeded));

#line default
@@ -262,7 +262,7 @@ WriteLiteral("\"\r\n data-published-failed=\"");

#line 56 "..\..\Pages\HomePage.cshtml"
Write(JsonConvert.SerializeObject(publishedFailed));
Write(JsonSerializer.Serialize(publishedFailed));

#line default
@@ -292,7 +292,7 @@ WriteLiteral("\"\r\n data-received-succeeded=\"");

#line 59 "..\..\Pages\HomePage.cshtml"
Write(JsonConvert.SerializeObject(receivedSucceeded));
Write(JsonSerializer.Serialize(receivedSucceeded));

#line default
@@ -302,7 +302,7 @@ WriteLiteral("\"\r\n data-received-failed=\"");

#line 60 "..\..\Pages\HomePage.cshtml"
Write(JsonConvert.SerializeObject(receivedFailed));
Write(JsonSerializer.Serialize(receivedFailed));

#line default


+ 1
- 1
src/DotNetCore.CAP/DotNetCore.CAP.csproj View File

@@ -13,9 +13,9 @@
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.0" />
<PackageReference Include="System.Threading.Channels" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="5.0.0" />
</ItemGroup>
</Project>

+ 19
- 13
src/DotNetCore.CAP/Serialization/ISerializer.JsonUtf8.cs View File

@@ -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<Message> 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<Message>(json);
return JsonSerializer.Deserialize<Message>(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<byte>();
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;
}
}
}
}

Loading…
Cancel
Save