Skip to content

Commit ce60f4b

Browse files
committed
Using System.Text.Json to replace Newtonsoft.
1 parent 21ba2a8 commit ce60f4b

File tree

3 files changed

+23
-44
lines changed

3 files changed

+23
-44
lines changed

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/Startup/Startup.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
using AbpCompanyName.AbpProjectName.Configuration;
1515
using AbpCompanyName.AbpProjectName.Identity;
1616
using Abp.AspNetCore.SignalR.Hubs;
17-
using Abp.Dependency;
18-
using Abp.Json;
1917
using Microsoft.Extensions.Hosting;
2018
using Microsoft.OpenApi.Models;
21-
using Newtonsoft.Json.Serialization;
2219
using System.IO;
2320

2421
namespace AbpCompanyName.AbpProjectName.Web.Host.Startup
@@ -41,14 +38,9 @@ public Startup(IWebHostEnvironment env)
4138
public void ConfigureServices(IServiceCollection services)
4239
{
4340
//MVC
44-
services.AddControllersWithViews(
45-
options => { options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute()); }
46-
).AddNewtonsoftJson(options =>
41+
services.AddControllersWithViews(options =>
4742
{
48-
options.SerializerSettings.ContractResolver = new AbpMvcContractResolver(IocManager.Instance)
49-
{
50-
NamingStrategy = new CamelCaseNamingStrategy()
51-
};
43+
options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute());
5244
});
5345

5446
IdentityRegistrar.Register(services);
@@ -103,7 +95,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
10395
app.UseAuthorization();
10496

10597
app.UseAbpRequestLocalization();
106-
98+
10799
app.UseEndpoints(endpoints =>
108100
{
109101
endpoints.MapHub<AbpCommonHub>("/signalr");
@@ -121,10 +113,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
121113
options.SwaggerEndpoint($"/swagger/{_apiVersion}/swagger.json", $"AbpProjectName API {_apiVersion}");
122114
options.IndexStream = () => Assembly.GetExecutingAssembly()
123115
.GetManifestResourceStream("AbpCompanyName.AbpProjectName.Web.Host.wwwroot.swagger.ui.index.html");
124-
options.DisplayRequestDuration(); // Controls the display of the request duration (in milliseconds) for "Try it out" requests.
116+
options.DisplayRequestDuration(); // Controls the display of the request duration (in milliseconds) for "Try it out" requests.
125117
}); // URL: /swagger
126118
}
127-
119+
128120
private void ConfigureSwagger(IServiceCollection services)
129121
{
130122
services.AddSwaggerGen(options =>

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Mvc/Startup/Startup.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Text.Encodings.Web;
1+
using System.Text.Encodings.Web;
32
using System.Text.Unicode;
43
using Microsoft.AspNetCore.Builder;
54
using Microsoft.AspNetCore.Hosting;
@@ -16,12 +15,8 @@
1615
using AbpCompanyName.AbpProjectName.Identity;
1716
using AbpCompanyName.AbpProjectName.Web.Resources;
1817
using Abp.AspNetCore.SignalR.Hubs;
19-
using Abp.Dependency;
20-
using Abp.Json;
2118
using Microsoft.Extensions.Hosting;
2219
using Microsoft.Extensions.WebEncoders;
23-
using Newtonsoft.Json.Serialization;
24-
2520

2621
namespace AbpCompanyName.AbpProjectName.Web.Startup
2722
{
@@ -46,14 +41,7 @@ public void ConfigureServices(IServiceCollection services)
4641
options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute());
4742
}
4843
)
49-
.AddRazorRuntimeCompilation()
50-
.AddNewtonsoftJson(options =>
51-
{
52-
options.SerializerSettings.ContractResolver = new AbpMvcContractResolver(IocManager.Instance)
53-
{
54-
NamingStrategy = new CamelCaseNamingStrategy()
55-
};
56-
});
44+
.AddRazorRuntimeCompilation();
5745

5846
IdentityRegistrar.Register(services);
5947
AuthConfigurer.Configure(services, _appConfiguration);
@@ -62,7 +50,7 @@ public void ConfigureServices(IServiceCollection services)
6250
{
6351
options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
6452
});
65-
53+
6654
services.AddScoped<IWebResourceManager, WebResourceManager>();
6755

6856
services.AddSignalR();

aspnet-core/test/AbpCompanyName.AbpProjectName.Web.Tests/AbpProjectNameWebTestBase.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net.Http;
55
using System.Net.Http.Headers;
66
using System.Text;
7+
using System.Text.Json;
78
using System.Threading.Tasks;
89
using Abp.AspNetCore.TestBase;
910
using Abp.Authorization.Users;
@@ -17,8 +18,6 @@
1718
using AngleSharp.Html.Dom;
1819
using AngleSharp.Html.Parser;
1920
using Microsoft.AspNetCore.Hosting;
20-
using Newtonsoft.Json;
21-
using Newtonsoft.Json.Serialization;
2221
using Shouldly;
2322

2423
namespace AbpCompanyName.AbpProjectName.Web.Tests
@@ -31,7 +30,7 @@ static AbpProjectNameWebTestBase()
3130
{
3231
ContentRootFolder = new Lazy<string>(WebContentDirectoryFinder.CalculateContentRootFolder, true);
3332
}
34-
33+
3534
protected override IWebHostBuilder CreateWebHostBuilder()
3635
{
3736
return base
@@ -46,9 +45,9 @@ protected async Task<T> GetResponseAsObjectAsync<T>(string url,
4645
HttpStatusCode expectedStatusCode = HttpStatusCode.OK)
4746
{
4847
var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode);
49-
return JsonConvert.DeserializeObject<T>(strResponse, new JsonSerializerSettings
48+
return JsonSerializer.Deserialize<T>(strResponse, new JsonSerializerOptions()
5049
{
51-
ContractResolver = new CamelCasePropertyNamesContractResolver()
50+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
5251
});
5352
}
5453

@@ -68,9 +67,9 @@ protected async Task<HttpResponseMessage> GetResponseAsync(string url,
6867
}
6968

7069
#endregion
71-
70+
7271
#region Authenticate
73-
72+
7473
/// <summary>
7574
/// /api/TokenAuth/Authenticate
7675
/// TokenAuthController
@@ -81,7 +80,7 @@ protected async Task<HttpResponseMessage> GetResponseAsync(string url,
8180
protected async Task AuthenticateAsync(string tenancyName, AuthenticateModel input)
8281
{
8382
if (tenancyName.IsNullOrWhiteSpace())
84-
{
83+
{
8584
var tenant = UsingDbContext(context => context.Tenants.FirstOrDefault(t => t.TenancyName == tenancyName));
8685
if (tenant != null)
8786
{
@@ -93,16 +92,17 @@ protected async Task AuthenticateAsync(string tenancyName, AuthenticateModel inp
9392
var response = await Client.PostAsync("/api/TokenAuth/Authenticate",
9493
new StringContent(input.ToJsonString(), Encoding.UTF8, "application/json"));
9594
response.StatusCode.ShouldBe(HttpStatusCode.OK);
96-
var result =
97-
JsonConvert.DeserializeObject<AjaxResponse<AuthenticateResultModel>>(
98-
await response.Content.ReadAsStringAsync());
95+
var result = JsonSerializer.Deserialize<AjaxResponse<AuthenticateResultModel>>(await response.Content.ReadAsStringAsync(), new JsonSerializerOptions()
96+
{
97+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
98+
});
9999
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.Result.AccessToken);
100-
100+
101101
AbpSession.UserId = result.Result.UserId;
102102
}
103-
103+
104104
#endregion
105-
105+
106106
#region Login
107107

108108
protected void LoginAsHostAdmin()
@@ -155,7 +155,6 @@ protected void LoginAsTenant(string tenancyName, string userName)
155155

156156
#endregion
157157

158-
159158
#region UsingDbContext
160159

161160
protected void UsingDbContext(Action<AbpProjectNameDbContext> action)
@@ -213,4 +212,4 @@ protected IHtmlDocument ParseHtml(string htmlString)
213212

214213
#endregion
215214
}
216-
}
215+
}

0 commit comments

Comments
 (0)