Skip to content

Commit 19baeb7

Browse files
author
Sumit Ghosh
authored
Migrates from Newtonsoft.Json to System.Text.Json (dotnet-architecture#1658)
* Included System.Text.Json related changes * Fixed order details summary in WebMVC * Updated custom JsonConverter
1 parent c4d40f1 commit 19baeb7

40 files changed

Lines changed: 206 additions & 133 deletions

File tree

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
<PackageReference Include="Grpc.Tools" Version="2.34.0" PrivateAssets="All" />
2525
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.2" />
2626
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
27-
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.2" />
2827
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="5.0.2" />
2928
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
3029
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00834" />

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/OrderApiClient.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Models;
33
using Microsoft.Extensions.Logging;
44
using Microsoft.Extensions.Options;
5-
using Newtonsoft.Json;
65
using System.Net.Http;
76
using System.Threading.Tasks;
7+
using System.Text.Json;
88

99
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
1010
{
@@ -24,14 +24,17 @@ public OrderApiClient(HttpClient httpClient, ILogger<OrderApiClient> logger, IOp
2424
public async Task<OrderData> GetOrderDraftFromBasketAsync(BasketData basket)
2525
{
2626
var uri = _urls.Orders + UrlsConfig.OrdersOperations.GetOrderDraft();
27-
var content = new StringContent(JsonConvert.SerializeObject(basket), System.Text.Encoding.UTF8, "application/json");
27+
var content = new StringContent(JsonSerializer.Serialize(basket), System.Text.Encoding.UTF8, "application/json");
2828
var response = await _apiClient.PostAsync(uri, content);
2929

3030
response.EnsureSuccessStatusCode();
3131

3232
var ordersDraftResponse = await response.Content.ReadAsStringAsync();
3333

34-
return JsonConvert.DeserializeObject<OrderData>(ordersDraftResponse);
34+
return JsonSerializer.Deserialize<OrderData>(ordersDraftResponse, new JsonSerializerOptions
35+
{
36+
PropertyNameCaseInsensitive = true
37+
});
3538
}
3639
}
3740
}

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static IServiceCollection AddCustomMvc(this IServiceCollection services,
107107
services.Configure<UrlsConfig>(configuration.GetSection("urls"));
108108

109109
services.AddControllers()
110-
.AddNewtonsoftJson();
110+
.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true);
111111

112112
services.AddSwaggerGen(options =>
113113
{

src/ApiGateways/Web.Bff.Shopping/aggregator/Services/OrderApiClient.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
33
using Microsoft.Extensions.Logging;
44
using Microsoft.Extensions.Options;
5-
using Newtonsoft.Json;
65
using System.Net.Http;
76
using System.Threading.Tasks;
7+
using System.Text.Json;
88

99
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
1010
{
@@ -24,14 +24,17 @@ public OrderApiClient(HttpClient httpClient, ILogger<OrderApiClient> logger, IOp
2424
public async Task<OrderData> GetOrderDraftFromBasketAsync(BasketData basket)
2525
{
2626
var url = _urls.Orders + UrlsConfig.OrdersOperations.GetOrderDraft();
27-
var content = new StringContent(JsonConvert.SerializeObject(basket), System.Text.Encoding.UTF8, "application/json");
27+
var content = new StringContent(JsonSerializer.Serialize(basket), System.Text.Encoding.UTF8, "application/json");
2828
var response = await _apiClient.PostAsync(url, content);
2929

3030
response.EnsureSuccessStatusCode();
3131

3232
var ordersDraftResponse = await response.Content.ReadAsStringAsync();
3333

34-
return JsonConvert.DeserializeObject<OrderData>(ordersDraftResponse);
34+
return JsonSerializer.Deserialize<OrderData>(ordersDraftResponse, new JsonSerializerOptions
35+
{
36+
PropertyNameCaseInsensitive = true
37+
});
3538
}
3639
}
3740
}

src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static IServiceCollection AddCustomMvc(this IServiceCollection services,
130130
services.Configure<UrlsConfig>(configuration.GetSection("urls"));
131131

132132
services.AddControllers()
133-
.AddNewtonsoftJson();
133+
.AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true);
134134

135135
services.AddSwaggerGen(options =>
136136
{

src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
<PackageReference Include="Grpc.Tools" Version="2.34.0" PrivateAssets="All" />
2626
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.2" />
2727
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
28-
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.2" />
2928
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="5.0.2" />
3029
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
3130
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0-dev-00834" />

src/BuildingBlocks/EventBus/EventBus/EventBus.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<RootNamespace>Microsoft.eShopOnContainers.BuildingBlocks.EventBus</RootNamespace>
66
</PropertyGroup>
77

8-
<ItemGroup>
9-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
8+
<ItemGroup>
109
</ItemGroup>
1110

1211
</Project>

src/BuildingBlocks/EventBus/EventBus/Events/IntegrationEvent.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using Newtonsoft.Json;
2-
using System;
1+
using System;
2+
using System.Text.Json.Serialization;
33

44
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events
55
{
66
public record IntegrationEvent
7-
{
7+
{
88
public IntegrationEvent()
99
{
1010
Id = Guid.NewGuid();
@@ -18,10 +18,10 @@ public IntegrationEvent(Guid id, DateTime createDate)
1818
CreationDate = createDate;
1919
}
2020

21-
[JsonProperty]
21+
[JsonInclude]
2222
public Guid Id { get; private init; }
2323

24-
[JsonProperty]
24+
[JsonInclude]
2525
public DateTime CreationDate { get; private init; }
2626
}
2727
}

src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
55
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
66
using Microsoft.Extensions.Logging;
7-
using Newtonsoft.Json;
8-
using Newtonsoft.Json.Linq;
97
using Polly;
108
using Polly.Retry;
119
using RabbitMQ.Client;
@@ -15,6 +13,7 @@
1513
using System.Net.Sockets;
1614
using System.Text;
1715
using System.Threading.Tasks;
16+
using System.Text.Json;
1817

1918
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
2019
{
@@ -89,9 +88,11 @@ public void Publish(IntegrationEvent @event)
8988
_logger.LogTrace("Declaring RabbitMQ exchange to publish event: {EventId}", @event.Id);
9089

9190
channel.ExchangeDeclare(exchange: BROKER_NAME, type: "direct");
92-
93-
var message = JsonConvert.SerializeObject(@event);
94-
var body = Encoding.UTF8.GetBytes(message);
91+
92+
var body = JsonSerializer.SerializeToUtf8Bytes(@event, @event.GetType(), new JsonSerializerOptions
93+
{
94+
WriteIndented = true
95+
});
9596

9697
policy.Execute(() =>
9798
{
@@ -272,8 +273,7 @@ private async Task ProcessEvent(string eventName, string message)
272273
{
273274
var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler;
274275
if (handler == null) continue;
275-
dynamic eventData = JObject.Parse(message);
276-
276+
using dynamic eventData = JsonDocument.Parse(message);
277277
await Task.Yield();
278278
await handler.Handle(eventData);
279279
}
@@ -282,7 +282,7 @@ private async Task ProcessEvent(string eventName, string message)
282282
var handler = scope.ResolveOptional(subscription.HandlerType);
283283
if (handler == null) continue;
284284
var eventType = _subsManager.GetEventTypeByName(eventName);
285-
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
285+
var integrationEvent = JsonSerializer.Deserialize(message, eventType, new JsonSerializerOptions() { PropertyNameCaseInsensitive= true});
286286
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
287287

288288
await Task.Yield();

src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
<ItemGroup>
99
<PackageReference Include="Autofac" Version="6.1.0" />
1010
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
11-
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
12-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
11+
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
1312
<PackageReference Include="Polly" Version="7.2.1" />
1413
<PackageReference Include="RabbitMQ.Client" Version="6.2.1" />
1514
</ItemGroup>

0 commit comments

Comments
 (0)