diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs index 24f86ce0b4..e6d03bf707 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Reflection; using Newtonsoft.Json; diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs index 3862dd01da..b86df6b6c8 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs @@ -16,12 +16,29 @@ namespace Volo.Abp.Auditing public string Serialize(object obj) { - var options = new JsonSerializerSettings + return JsonConvert.SerializeObject(obj, GetSharedJsonSerializerSettings()); + } + + private static readonly object SyncObj = new object(); + private static JsonSerializerSettings _sharedJsonSerializerSettings; + + private JsonSerializerSettings GetSharedJsonSerializerSettings() + { + if (_sharedJsonSerializerSettings == null) { - ContractResolver = new AuditingContractResolver(Options.IgnoredTypes) - }; + lock (SyncObj) + { + if (_sharedJsonSerializerSettings == null) + { + _sharedJsonSerializerSettings = new JsonSerializerSettings + { + ContractResolver = new AuditingContractResolver(Options.IgnoredTypes) + }; + } + } + } - return JsonConvert.SerializeObject(obj, options); + return _sharedJsonSerializerSettings; } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs index 4114b89892..882bf94cb0 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs @@ -18,6 +18,11 @@ namespace Volo.Abp.Http.Client.DynamicProxying protected IApiDescriptionCache Cache { get; } + private static readonly JsonSerializerSettings SharedJsonSerializerSettings = new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }; + public ApiDescriptionFinder( IApiDescriptionCache cache, IDynamicProxyHttpClientFactory httpClientFactory) @@ -94,11 +99,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying var result = JsonConvert.DeserializeObject( content, - typeof(ApplicationApiDescriptionModel), - new JsonSerializerSettings - { - ContractResolver = new CamelCasePropertyNamesContractResolver() - }); + typeof(ApplicationApiDescriptionModel), SharedJsonSerializerSettings); return (ApplicationApiDescriptionModel)result; } diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs index 262c2b04d7..99252b8448 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/Newtonsoft/NewtonsoftJsonSerializer.cs @@ -9,6 +9,9 @@ namespace Volo.Abp.Json.Newtonsoft { private readonly AbpJsonIsoDateTimeConverter _dateTimeConverter; + private static readonly CamelCaseExceptDictionaryKeysResolver SharedCamelCaseExceptDictionaryKeysResolver = + new CamelCaseExceptDictionaryKeysResolver(); + public NewtonsoftJsonSerializer(AbpJsonIsoDateTimeConverter dateTimeConverter) { _dateTimeConverter = dateTimeConverter; @@ -37,7 +40,7 @@ namespace Volo.Abp.Json.Newtonsoft if (camelCase) { - settings.ContractResolver = new CamelCaseExceptDictionaryKeysResolver(); + settings.ContractResolver = SharedCamelCaseExceptDictionaryKeysResolver; } if (indented) diff --git a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs b/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs index 325d67800d..9b1f1617f6 100644 --- a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs +++ b/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/Json/JsonLocalizationDictionaryBuilder.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using Microsoft.Extensions.Localization; @@ -9,6 +9,11 @@ namespace Volo.Abp.Localization.Json { public static class JsonLocalizationDictionaryBuilder { + private static readonly JsonSerializerSettings SharedJsonSerializerSettings = new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }; + /// /// Builds an from given file. /// @@ -35,11 +40,7 @@ namespace Volo.Abp.Localization.Json try { jsonFile = JsonConvert.DeserializeObject( - jsonString, - new JsonSerializerSettings - { - ContractResolver = new CamelCasePropertyNamesContractResolver() - }); + jsonString, SharedJsonSerializerSettings); } catch (JsonException ex) { diff --git a/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreTestBase.cs b/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreTestBase.cs index 6bb82c3656..33beb9d62c 100644 --- a/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreTestBase.cs +++ b/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreTestBase.cs @@ -11,13 +11,16 @@ namespace Volo.Abp.AspNetCore public abstract class AbpAspNetCoreTestBase : AbpAspNetCoreIntegratedTestBase where TStartup : class { + private static readonly JsonSerializerSettings SharedJsonSerializerSettings = + new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }; + protected virtual async Task GetResponseAsObjectAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) { var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode); - return JsonConvert.DeserializeObject(strResponse, new JsonSerializerSettings - { - ContractResolver = new CamelCasePropertyNamesContractResolver() - }); + return JsonConvert.DeserializeObject(strResponse, SharedJsonSerializerSettings); } protected virtual async Task GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK)