From ff2ecc004f8c57ff8d59c468c2390b70fea66e25 Mon Sep 17 00:00:00 2001 From: maliming Date: Sun, 21 Aug 2022 15:34:06 +0800 Subject: [PATCH] Remove `HybridJson`. --- framework/Volo.Abp.sln | 2 +- ...o.Abp.AspNetCore.Mvc.NewtonsoftJson.csproj | 1 + .../AbpAspNetCoreMvcNewtonsoftModule.cs | 36 +---- .../AbpNewtonsoftJsonFormatter.cs | 65 -------- .../AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs | 4 +- ...tCoreApiDescriptionModelProviderOptions.cs | 11 +- .../Mvc/Json/AbpHybridJsonFormatterOptions.cs | 17 -- .../Mvc/Json/AbpHybridJsonInputFormatter.cs | 44 ------ .../Mvc/Json/AbpHybridJsonOutputFormatter.cs | 42 ----- .../Mvc/Json/AbpSystemTextJsonFormatter.cs | 61 ------- .../Mvc/Json/IAbpHybridJsonInputFormatter.cs | 12 -- .../Mvc/Json/IAbpHybridJsonOutputFormatter.cs | 12 -- .../Mvc/Json/MediaTypeHeaderValues.cs | 21 --- .../Mvc/Json/MvcCoreBuilderExtensions.cs | 16 -- .../Abp/Auditing/AuditingContractResolver.cs | 42 ----- .../Volo/Abp/Auditing/JsonAuditSerializer.cs | 78 +++++++++ .../Abp/Auditing/JsonNetAuditSerializer.cs | 43 ----- .../Volo.Abp.Cli.Core.csproj | 1 + .../Volo/Abp/Cli/AbpCliCoreModule.cs | 8 - .../Volo/Abp/Cli/LIbs/InstallLibsService.cs | 11 +- .../AbpApiProxyScriptingConfiguration.cs | 18 +-- .../FodyWeavers.xml | 0 .../FodyWeavers.xsd | 0 .../Volo.Abp.Json.Abstractions.csproj} | 2 +- .../Abp/Json/AbpJsonAbstractionsModule.cs} | 2 +- .../Volo/Abp/Json/AbpJsonOptions.cs | 9 ++ .../Volo/Abp/Json/IJsonSerializer.cs | 0 .../Volo/Abp/Json/AbpHybridJsonSerializer.cs | 66 -------- .../Volo/Abp/Json/AbpJsonOptions.cs | 18 --- .../Volo/Abp/Json/IJsonSerializerProvider.cs | 15 -- .../Volo.Abp.Json.Newtonsoft.csproj | 2 +- ...pCamelCasePropertyNamesContractResolver.cs | 31 +--- .../Newtonsoft/AbpDefaultContractResolver.cs | 33 ++++ .../Newtonsoft/AbpJsonIsoDateTimeConverter.cs | 14 ++ .../Newtonsoft/AbpJsonNewtonsoftModule.cs | 7 +- ...ider.cs => AbpNewtonsoftJsonSerializer.cs} | 33 ++-- .../Volo.Abp.Json.SystemTextJson.csproj | 2 +- .../AbpJsonSystemTextJsonModule.cs | 7 +- ...ider.cs => AbpSystemTextJsonSerializer.cs} | 12 +- .../AbpSystemTextJsonSerializerOptions.cs | 4 - .../IncludeExtraPropertiesModifiers.cs | 19 +-- .../src/Volo.Abp.Json/Volo.Abp.Json.csproj | 1 - .../Volo/Abp/Json/AbpJsonModule.cs | 5 +- .../Volo.Abp.AspNetCore.Mvc.Tests.csproj | 1 - .../Mvc/AbpAspNetCoreMvcTestModule.cs | 4 +- .../ModelBindingController_Tests.cs | 47 +----- .../Volo/Abp/AspNetCore/Mvc/Startup.cs | 33 +++- ...er_Test.cs => JsonAuditSerializer_Test.cs} | 13 +- .../Abp/Json/AbpHybridJsonSerializer_Tests.cs | 149 ------------------ ...pSystemTextJsonSerializerProvider_Tests.cs | 4 +- .../Json/ExtensibleObjectModifiers_Tests.cs | 2 +- nupkg/common.ps1 | 2 +- 52 files changed, 237 insertions(+), 845 deletions(-) delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpNewtonsoftJsonFormatter.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonFormatterOptions.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonInputFormatter.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpSystemTextJsonFormatter.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/IAbpHybridJsonInputFormatter.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/IAbpHybridJsonOutputFormatter.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MediaTypeHeaderValues.cs delete mode 100644 framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs create mode 100644 framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonAuditSerializer.cs delete mode 100644 framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs rename framework/src/{Volo.Abp.Json.Core => Volo.Abp.Json.Abstractions}/FodyWeavers.xml (100%) rename framework/src/{Volo.Abp.Json.Core => Volo.Abp.Json.Abstractions}/FodyWeavers.xsd (100%) rename framework/src/{Volo.Abp.Json.Core/Volo.Abp.Json.Core.csproj => Volo.Abp.Json.Abstractions/Volo.Abp.Json.Abstractions.csproj} (92%) rename framework/src/{Volo.Abp.Json.Core/Volo/Abp/Json/AbpJsonCoreModule.cs => Volo.Abp.Json.Abstractions/Volo/Abp/Json/AbpJsonAbstractionsModule.cs} (54%) create mode 100644 framework/src/Volo.Abp.Json.Abstractions/Volo/Abp/Json/AbpJsonOptions.cs rename framework/src/{Volo.Abp.Json.Core => Volo.Abp.Json.Abstractions}/Volo/Abp/Json/IJsonSerializer.cs (100%) delete mode 100644 framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpHybridJsonSerializer.cs delete mode 100644 framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpJsonOptions.cs delete mode 100644 framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/IJsonSerializerProvider.cs create mode 100644 framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDefaultContractResolver.cs rename framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/{AbpNewtonsoftJsonSerializerProvider.cs => AbpNewtonsoftJsonSerializer.cs} (80%) rename framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/{AbpSystemTextJsonSerializerProvider.cs => AbpSystemTextJsonSerializer.cs} (76%) rename framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/{JsonNetAuditSerializer_Test.cs => JsonAuditSerializer_Test.cs} (78%) delete mode 100644 framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs diff --git a/framework/Volo.Abp.sln b/framework/Volo.Abp.sln index b646c5f9e6..27f68c5410 100644 --- a/framework/Volo.Abp.sln +++ b/framework/Volo.Abp.sln @@ -413,7 +413,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Json.Newtonsoft", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Json.SystemTextJson", "src\Volo.Abp.Json.SystemTextJson\Volo.Abp.Json.SystemTextJson.csproj", "{0AD06E14-CBFE-4551-8D18-9E921D8F2A87}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Json.Core", "src\Volo.Abp.Json.Core\Volo.Abp.Json.Core.csproj", "{08531C5D-0436-4721-986D-96446CF54316}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Json.Abstractions", "src\Volo.Abp.Json.Abstractions\Volo.Abp.Json.Abstractions.csproj", "{08531C5D-0436-4721-986D-96446CF54316}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.AspNetCore.Mvc.NewtonsoftJson", "src\Volo.Abp.AspNetCore.Mvc.NewtonsoftJson\Volo.Abp.AspNetCore.Mvc.NewtonsoftJson.csproj", "{0CFC9D4F-F12F-4B44-ABCF-AB4A0E9E85B2}" EndProject diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson.csproj b/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson.csproj index 83ac5bc666..026fb2f4d4 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson.csproj +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson.csproj @@ -19,6 +19,7 @@ + diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpAspNetCoreMvcNewtonsoftModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpAspNetCoreMvcNewtonsoftModule.cs index f195f98bf4..6e71fe7b5f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpAspNetCoreMvcNewtonsoftModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpAspNetCoreMvcNewtonsoftModule.cs @@ -1,47 +1,21 @@ -using System; -using System.Collections.Generic; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Formatters; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.ObjectPool; -using Microsoft.Extensions.Options; -using Newtonsoft.Json; -using Volo.Abp.AspNetCore.Mvc.Json; using Volo.Abp.Json.Newtonsoft; using Volo.Abp.Modularity; namespace Volo.Abp.AspNetCore.Mvc.NewtonsoftJson; -[DependsOn(typeof(AbpAspNetCoreMvcModule))] +[DependsOn(typeof(AbpJsonNewtonsoftModule), typeof(AbpAspNetCoreMvcModule))] public class AbpAspNetCoreMvcNewtonsoftModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - context.Services.TryAddSingleton(); - - Configure(mvcOptions => - { - mvcOptions.InputFormatters.RemoveType(); - mvcOptions.OutputFormatters.RemoveType(); - }); - - Configure(formatterOptions => - { - formatterOptions.TextInputFormatters.Add(); - formatterOptions.TextOutputFormatters.Add(); - }); + context.Services.AddMvcCore().AddNewtonsoftJson(); context.Services.AddOptions() - .Configure((options, serviceProvider) => + .Configure((options, contractResolver) => { - options.SerializerSettings.ContractResolver = serviceProvider.GetRequiredService(); - - var converters = serviceProvider.GetRequiredService>().Value - .Converters.Select(converterType => serviceProvider.GetRequiredService(converterType).As()) - .ToList(); - - options.SerializerSettings.Converters.InsertRange(0, converters); + options.SerializerSettings.ContractResolver = contractResolver; }); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpNewtonsoftJsonFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpNewtonsoftJsonFormatter.cs deleted file mode 100644 index aa411bfb61..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpNewtonsoftJsonFormatter.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using System.Buffers; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Formatters; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.ObjectPool; -using Microsoft.Extensions.Options; -using Volo.Abp.AspNetCore.Mvc.Json; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.AspNetCore.Mvc.NewtonsoftJson; - -public class AbpNewtonsoftJsonFormatter : IAbpHybridJsonInputFormatter, IAbpHybridJsonOutputFormatter, ITransientDependency -{ - private readonly ILoggerFactory _loggerFactory; - private readonly ArrayPool _charPool; - private readonly ObjectPoolProvider _objectPoolProvider; - private readonly IOptions _mvcOptions; - private readonly IOptions _mvcNewtonsoftJsonOptions; - - public AbpNewtonsoftJsonFormatter( - ILoggerFactory loggerFactory, - ArrayPool charPool, - ObjectPoolProvider objectPoolProvider, - IOptions mvcOptions, - IOptions mvcNewtonsoftJsonOptions) - { - _loggerFactory = loggerFactory; - _charPool = charPool; - _objectPoolProvider = objectPoolProvider; - _mvcOptions = mvcOptions; - _mvcNewtonsoftJsonOptions = mvcNewtonsoftJsonOptions; - } - - Task IAbpHybridJsonInputFormatter.CanHandleAsync(Type type) - { - return Task.FromResult(true); - } - - public Task GetTextInputFormatterAsync() - { - return Task.FromResult(new NewtonsoftJsonInputFormatter( - _loggerFactory.CreateLogger(), - _mvcNewtonsoftJsonOptions.Value.SerializerSettings, - _charPool, - _objectPoolProvider, - _mvcOptions.Value, - _mvcNewtonsoftJsonOptions.Value)); - } - - Task IAbpHybridJsonOutputFormatter.CanHandleAsync(Type type) - { - return Task.FromResult(true); - } - - public Task GetTextOutputFormatterAsync() - { - return Task.FromResult(new NewtonsoftJsonOutputFormatter( - _mvcNewtonsoftJsonOptions.Value.SerializerSettings, - _charPool, - _mvcOptions.Value, - _mvcNewtonsoftJsonOptions.Value)); - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs index 22b919c91c..20922527bf 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs @@ -38,6 +38,7 @@ using Volo.Abp.GlobalFeatures; using Volo.Abp.Http.Modeling; using Volo.Abp.Http.ProxyScripting.Generators.JQuery; using Volo.Abp.Json; +using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.UI; @@ -52,7 +53,8 @@ namespace Volo.Abp.AspNetCore.Mvc; typeof(AbpAspNetCoreMvcContractsModule), typeof(AbpUiNavigationModule), typeof(AbpGlobalFeaturesModule), - typeof(AbpDddApplicationModule) + typeof(AbpDddApplicationModule), + typeof(AbpJsonSystemTextJsonModule) )] public class AbpAspNetCoreMvcModule : AbpModule { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs index 2b9e231874..4dc4a9719a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs @@ -62,19 +62,12 @@ public class AspNetCoreApiDescriptionModelProviderOptions { if (apiParameterDescription.ModelMetadata is DefaultModelMetadata defaultModelMetadata) { - var jsonPropertyNameAttribute = (System.Text.Json.Serialization.JsonPropertyNameAttribute) - defaultModelMetadata?.Attributes?.PropertyAttributes?.FirstOrDefault(x => x is System.Text.Json.Serialization.JsonPropertyNameAttribute); + var jsonPropertyNameAttribute = (JsonPropertyNameAttribute) + defaultModelMetadata?.Attributes?.PropertyAttributes?.FirstOrDefault(x => x is JsonPropertyNameAttribute); if (jsonPropertyNameAttribute != null) { return jsonPropertyNameAttribute.Name; } - - var jsonPropertyAttribute = (Newtonsoft.Json.JsonPropertyAttribute) - defaultModelMetadata?.Attributes?.PropertyAttributes?.FirstOrDefault(x => x is Newtonsoft.Json.JsonPropertyAttribute); - if (jsonPropertyAttribute != null) - { - return jsonPropertyAttribute.PropertyName; - } } return null; diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonFormatterOptions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonFormatterOptions.cs deleted file mode 100644 index 066ebe4413..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonFormatterOptions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.AspNetCore.Mvc.Formatters; -using Volo.Abp.Collections; - -namespace Volo.Abp.AspNetCore.Mvc.Json; - -public class AbpHybridJsonFormatterOptions -{ - public ITypeList TextInputFormatters { get; } - - public ITypeList TextOutputFormatters { get; } - - public AbpHybridJsonFormatterOptions() - { - TextInputFormatters = new TypeList(); - TextOutputFormatters = new TypeList(); - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonInputFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonInputFormatter.cs deleted file mode 100644 index 874508ac1f..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonInputFormatter.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Text; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Formatters; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; - -namespace Volo.Abp.AspNetCore.Mvc.Json; - -public class AbpHybridJsonInputFormatter : TextInputFormatter, IInputFormatterExceptionPolicy -{ - public AbpHybridJsonInputFormatter() - { - SupportedEncodings.Add(UTF8EncodingWithoutBOM); - SupportedEncodings.Add(UTF16EncodingLittleEndian); - - SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationJson); - SupportedMediaTypes.Add(MediaTypeHeaderValues.TextJson); - SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationAnyJsonSyntax); - } - - public async override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) - { - return await (await GetTextInputFormatterAsync(context)).ReadRequestBodyAsync(context, encoding); - } - - protected virtual async Task GetTextInputFormatterAsync(InputFormatterContext context) - { - var options = context.HttpContext.RequestServices.GetRequiredService>().Value; - - foreach (var inputFormatterType in options.TextInputFormatters) - { - var inputFormatter = context.HttpContext.RequestServices.GetRequiredService(inputFormatterType).As(); - if (await inputFormatter.CanHandleAsync(context.ModelType)) - { - return await inputFormatter.GetTextInputFormatterAsync(); - } - } - - throw new AbpException($"The {nameof(AbpHybridJsonInputFormatter)} can't handle '{context.ModelType.GetFullNameWithAssemblyName()}'!"); - } - - public virtual InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.MalformedInputExceptions; -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs deleted file mode 100644 index 2960ff1635..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpHybridJsonOutputFormatter.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Text; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Formatters; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; - -namespace Volo.Abp.AspNetCore.Mvc.Json; - -public class AbpHybridJsonOutputFormatter : TextOutputFormatter -{ - public AbpHybridJsonOutputFormatter() - { - SupportedEncodings.Add(Encoding.UTF8); - SupportedEncodings.Add(Encoding.Unicode); - - SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationJson); - SupportedMediaTypes.Add(MediaTypeHeaderValues.TextJson); - SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationAnyJsonSyntax); - } - - public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) - { - await (await GetTextInputFormatter(context)).WriteResponseBodyAsync(context, selectedEncoding); - } - - protected virtual async Task GetTextInputFormatter(OutputFormatterWriteContext context) - { - var options = context.HttpContext.RequestServices.GetRequiredService>().Value; - - foreach (var outputFormatterType in options.TextOutputFormatters) - { - var outputFormatter = context.HttpContext.RequestServices.GetRequiredService(outputFormatterType).As(); - if (await outputFormatter.CanHandleAsync(context.ObjectType)) - { - return await outputFormatter.GetTextOutputFormatterAsync(); - } - } - - throw new AbpException($"The {nameof(AbpHybridJsonOutputFormatter)} can't handle '{context.ObjectType.GetFullNameWithAssemblyName()}'!"); - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpSystemTextJsonFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpSystemTextJsonFormatter.cs deleted file mode 100644 index fb8c509748..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpSystemTextJsonFormatter.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Text.Encodings.Web; -using System.Text.Json; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Formatters; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Json.SystemTextJson; - -namespace Volo.Abp.AspNetCore.Mvc.Json; - -public class AbpSystemTextJsonFormatter : IAbpHybridJsonInputFormatter, IAbpHybridJsonOutputFormatter, ITransientDependency -{ - private readonly IOptions _jsonOptions; - private readonly IOptions _systemTextJsonSerializerOptions; - private readonly ILoggerFactory _loggerFactory; - - public AbpSystemTextJsonFormatter( - IOptions jsonOptions, - IOptions systemTextJsonSerializerOptions, - ILoggerFactory loggerFactory) - { - _jsonOptions = jsonOptions; - _systemTextJsonSerializerOptions = systemTextJsonSerializerOptions; - _loggerFactory = loggerFactory; - } - - Task IAbpHybridJsonInputFormatter.CanHandleAsync(Type type) - { - return Task.FromResult(!_systemTextJsonSerializerOptions.Value.UnsupportedTypes.Contains(type)); - } - - public virtual Task GetTextInputFormatterAsync() - { - return Task.FromResult(new SystemTextJsonInputFormatter( - _jsonOptions.Value, - _loggerFactory.CreateLogger())); - } - - Task IAbpHybridJsonOutputFormatter.CanHandleAsync(Type type) - { - return Task.FromResult(!_systemTextJsonSerializerOptions.Value.UnsupportedTypes.Contains(type)); - } - - public Task GetTextOutputFormatterAsync() - { - var jsonSerializerOptions = _jsonOptions.Value.JsonSerializerOptions; - if (jsonSerializerOptions.Encoder is null) - { - // If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters. - jsonSerializerOptions = new JsonSerializerOptions(jsonSerializerOptions) - { - Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, - }; - } - - return Task.FromResult(new SystemTextJsonOutputFormatter(jsonSerializerOptions)); - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/IAbpHybridJsonInputFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/IAbpHybridJsonInputFormatter.cs deleted file mode 100644 index 0a167e58d4..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/IAbpHybridJsonInputFormatter.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Formatters; - -namespace Volo.Abp.AspNetCore.Mvc.Json; - -public interface IAbpHybridJsonInputFormatter -{ - Task CanHandleAsync(Type type); - - Task GetTextInputFormatterAsync(); -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/IAbpHybridJsonOutputFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/IAbpHybridJsonOutputFormatter.cs deleted file mode 100644 index 06418b88d4..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/IAbpHybridJsonOutputFormatter.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Formatters; - -namespace Volo.Abp.AspNetCore.Mvc.Json; - -public interface IAbpHybridJsonOutputFormatter -{ - Task CanHandleAsync(Type type); - - Task GetTextOutputFormatterAsync(); -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MediaTypeHeaderValues.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MediaTypeHeaderValues.cs deleted file mode 100644 index 23d039ef30..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MediaTypeHeaderValues.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.Net.Http.Headers; - -namespace Volo.Abp.AspNetCore.Mvc.Json; - -/// -/// https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.NewtonsoftJson/src/MediaTypeHeaderValues.cs -/// -internal static class MediaTypeHeaderValues -{ - public static readonly MediaTypeHeaderValue ApplicationJson = MediaTypeHeaderValue.Parse("application/json").CopyAsReadOnly(); - - public static readonly MediaTypeHeaderValue TextJson = MediaTypeHeaderValue.Parse("text/json").CopyAsReadOnly(); - - public static readonly MediaTypeHeaderValue ApplicationAnyJsonSyntax = MediaTypeHeaderValue.Parse("application/*+json").CopyAsReadOnly(); - - public static readonly MediaTypeHeaderValue ApplicationXml = MediaTypeHeaderValue.Parse("application/xml").CopyAsReadOnly(); - - public static readonly MediaTypeHeaderValue TextXml = MediaTypeHeaderValue.Parse("text/xml").CopyAsReadOnly(); - - public static readonly MediaTypeHeaderValue ApplicationAnyXmlSyntax = MediaTypeHeaderValue.Parse("application/*+xml").CopyAsReadOnly(); -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs index ea3f5ea156..66af7b060c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/MvcCoreBuilderExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Text.Json; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.Json.SystemTextJson; @@ -13,21 +12,6 @@ public static class MvcCoreBuilderExtensions { public static IMvcCoreBuilder AddAbpHybridJson(this IMvcCoreBuilder builder) { - builder.Services.Configure(options => - { - options.InputFormatters.RemoveType(); - options.InputFormatters.Add(new AbpHybridJsonInputFormatter()); - - options.OutputFormatters.RemoveType(); - options.OutputFormatters.Add(new AbpHybridJsonOutputFormatter()); - }); - - builder.Services.Configure(options => - { - options.TextInputFormatters.Add(); - options.TextOutputFormatters.Add(); - }); - builder.Services.AddOptions() .Configure((options, serviceProvider) => { diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs deleted file mode 100644 index 77d1b7ddb9..0000000000 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingContractResolver.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; - -namespace Volo.Abp.Auditing; - -public class AuditingContractResolver : CamelCasePropertyNamesContractResolver -{ - private readonly List _ignoredTypes; - - public AuditingContractResolver(List ignoredTypes) - { - _ignoredTypes = ignoredTypes; - } - - protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) - { - var property = base.CreateProperty(member, memberSerialization); - - if (_ignoredTypes.Any(ignoredType => ignoredType.GetTypeInfo().IsAssignableFrom(property.PropertyType))) - { - property.ShouldSerialize = instance => false; - return property; - } - - if (member.DeclaringType != null && (member.DeclaringType.IsDefined(typeof(DisableAuditingAttribute)) || member.DeclaringType.IsDefined(typeof(JsonIgnoreAttribute)))) - { - property.ShouldSerialize = instance => false; - return property; - } - - if (member.IsDefined(typeof(DisableAuditingAttribute)) || member.IsDefined(typeof(JsonIgnoreAttribute))) - { - property.ShouldSerialize = instance => false; - } - - return property; - } -} diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonAuditSerializer.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonAuditSerializer.cs new file mode 100644 index 0000000000..9d8c4248e6 --- /dev/null +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonAuditSerializer.cs @@ -0,0 +1,78 @@ +using System.Collections.Concurrent; +using System.Linq; +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization.Metadata; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Auditing; + +public class JsonAuditSerializer : IAuditSerializer, ITransientDependency +{ + protected AbpAuditingOptions Options; + + public JsonAuditSerializer(IOptions options) + { + Options = options.Value; + } + + public string Serialize(object obj) + { + return JsonSerializer.Serialize(obj, CreateJsonSerializerOptions()); + } + + private static readonly ConcurrentDictionary JsonSerializerOptionsCache = + new ConcurrentDictionary(); + + protected virtual JsonSerializerOptions CreateJsonSerializerOptions() + { + return JsonSerializerOptionsCache.GetOrAdd(nameof(JsonAuditSerializer), _ => + { + var settings = new JsonSerializerOptions() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + TypeInfoResolver = new DefaultJsonTypeInfoResolver() + { + Modifiers = + { + jsonTypeInfo => + { + if (Options.IgnoredTypes.Any(ignoredType => ignoredType.IsAssignableFrom(jsonTypeInfo.Type))) + { + jsonTypeInfo.Properties.Clear(); + } + + if (jsonTypeInfo.Type.GetCustomAttributes(typeof(DisableAuditingAttribute), false).Any()) + { + jsonTypeInfo.Properties.Clear(); + } + + foreach (var property in jsonTypeInfo.Properties) + { + if (Options.IgnoredTypes.Any(ignoredType => ignoredType.IsAssignableFrom(property.PropertyType))) + { + property.ShouldSerialize = (_, _) => false; + } + + if (property.AttributeProvider != null && + property.AttributeProvider.GetCustomAttributes(typeof(DisableAuditingAttribute), false).Any()) + { + property.ShouldSerialize = (_, _) => false; + } + + if (property.PropertyType.DeclaringType != null && + property.PropertyType.DeclaringType.IsDefined(typeof(DisableAuditingAttribute))) + { + property.ShouldSerialize = (_, _) => false; + } + } + } + } + } + }; + + return settings; + }); + } +} diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs deleted file mode 100644 index d5df2b2644..0000000000 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/JsonNetAuditSerializer.cs +++ /dev/null @@ -1,43 +0,0 @@ -using Microsoft.Extensions.Options; -using Newtonsoft.Json; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Auditing; - -//TODO: Rename to JsonAuditSerializer -public class JsonNetAuditSerializer : IAuditSerializer, ITransientDependency -{ - protected AbpAuditingOptions Options; - - public JsonNetAuditSerializer(IOptions options) - { - Options = options.Value; - } - - public string Serialize(object obj) - { - return JsonConvert.SerializeObject(obj, GetSharedJsonSerializerSettings()); - } - - private static readonly object SyncObj = new object(); - private static JsonSerializerSettings _sharedJsonSerializerSettings; - - private JsonSerializerSettings GetSharedJsonSerializerSettings() - { - if (_sharedJsonSerializerSettings == null) - { - lock (SyncObj) - { - if (_sharedJsonSerializerSettings == null) - { - _sharedJsonSerializerSettings = new JsonSerializerSettings - { - ContractResolver = new AuditingContractResolver(Options.IgnoredTypes) - }; - } - } - } - - return _sharedJsonSerializerSettings; - } -} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj b/framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj index 5c83874fa0..1688a4246f 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj +++ b/framework/src/Volo.Abp.Cli.Core/Volo.Abp.Cli.Core.csproj @@ -30,5 +30,6 @@ + diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs index 7aba9ae849..28ab693ba1 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs @@ -2,17 +2,14 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Cli.Commands; using Volo.Abp.Cli.Http; -using Volo.Abp.Cli.LIbs; using Volo.Abp.Cli.ServiceProxying; using Volo.Abp.Cli.ServiceProxying.Angular; using Volo.Abp.Cli.ServiceProxying.CSharp; using Volo.Abp.Cli.ServiceProxying.JavaScript; using Volo.Abp.Domain; using Volo.Abp.Http; -using Volo.Abp.Http.ProxyScripting.Generators.JQuery; using Volo.Abp.IdentityModel; using Volo.Abp.Json; -using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Minify; using Volo.Abp.Modularity; @@ -34,11 +31,6 @@ public class AbpCliCoreModule : AbpModule Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); - Configure(options => - { - options.UnsupportedTypes.Add(typeof(ResourceMapping)); - }); - Configure(options => { options.Commands[HelpCommand.Name] = typeof(HelpCommand); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/LIbs/InstallLibsService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/LIbs/InstallLibsService.cs index 654feb117b..b925a4220c 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/LIbs/InstallLibsService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/LIbs/InstallLibsService.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.FileSystemGlobbing; using Microsoft.Extensions.FileSystemGlobbing.Abstractions; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Newtonsoft.Json; using NuGet.Versioning; using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; @@ -30,12 +31,9 @@ public class InstallLibsService : IInstallLibsService, ITransientDependency public ILogger Logger { get; set; } - private readonly IJsonSerializer _jsonSerializer; - - public InstallLibsService(IJsonSerializer jsonSerializer, NpmHelper npmHelper) + public InstallLibsService(NpmHelper npmHelper) { NpmHelper = npmHelper; - _jsonSerializer = jsonSerializer; } public async Task InstallLibsAsync(string directory) @@ -119,7 +117,7 @@ public class InstallLibsService : IInstallLibsService, ITransientDependency { return false; } - + using (var reader = File.OpenText(file)) { return reader.ReadToEnd().Contains("Microsoft.NET.Sdk.Web"); @@ -145,7 +143,8 @@ public class InstallLibsService : IInstallLibsService, ITransientDependency { var mappingFileContent = await reader.ReadToEndAsync(); - var mapping = _jsonSerializer.Deserialize(mappingFileContent + // System.Text.Json doesn't support the property name without quotes. + var mapping = Newtonsoft.Json.JsonConvert.DeserializeObject(mappingFileContent .Replace("module.exports", string.Empty) .Replace("=", string.Empty).Trim().TrimEnd(';')); diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingConfiguration.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingConfiguration.cs index e8da5693f6..86449c8971 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingConfiguration.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingConfiguration.cs @@ -10,22 +10,6 @@ public static class AbpApiProxyScriptingConfiguration static AbpApiProxyScriptingConfiguration() { PropertyNameGenerator = propertyInfo => - { - var jsonPropertyNameAttribute = propertyInfo.GetSingleAttributeOrNull(true); - - if (jsonPropertyNameAttribute != null) - { - return jsonPropertyNameAttribute.Name; - } - - var jsonPropertyAttribute = propertyInfo.GetSingleAttributeOrNull(true); - - if (jsonPropertyAttribute != null) - { - return jsonPropertyAttribute.PropertyName; - } - - return null; - }; + propertyInfo.GetSingleAttributeOrNull()?.Name; } } diff --git a/framework/src/Volo.Abp.Json.Core/FodyWeavers.xml b/framework/src/Volo.Abp.Json.Abstractions/FodyWeavers.xml similarity index 100% rename from framework/src/Volo.Abp.Json.Core/FodyWeavers.xml rename to framework/src/Volo.Abp.Json.Abstractions/FodyWeavers.xml diff --git a/framework/src/Volo.Abp.Json.Core/FodyWeavers.xsd b/framework/src/Volo.Abp.Json.Abstractions/FodyWeavers.xsd similarity index 100% rename from framework/src/Volo.Abp.Json.Core/FodyWeavers.xsd rename to framework/src/Volo.Abp.Json.Abstractions/FodyWeavers.xsd diff --git a/framework/src/Volo.Abp.Json.Core/Volo.Abp.Json.Core.csproj b/framework/src/Volo.Abp.Json.Abstractions/Volo.Abp.Json.Abstractions.csproj similarity index 92% rename from framework/src/Volo.Abp.Json.Core/Volo.Abp.Json.Core.csproj rename to framework/src/Volo.Abp.Json.Abstractions/Volo.Abp.Json.Abstractions.csproj index b7a88c22f6..15130c129c 100644 --- a/framework/src/Volo.Abp.Json.Core/Volo.Abp.Json.Core.csproj +++ b/framework/src/Volo.Abp.Json.Abstractions/Volo.Abp.Json.Abstractions.csproj @@ -5,7 +5,7 @@ netstandard2.0 - Volo.Abp.Json.Core + Volo.Abp.Json.Abstractions $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; false false diff --git a/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpJsonCoreModule.cs b/framework/src/Volo.Abp.Json.Abstractions/Volo/Abp/Json/AbpJsonAbstractionsModule.cs similarity index 54% rename from framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpJsonCoreModule.cs rename to framework/src/Volo.Abp.Json.Abstractions/Volo/Abp/Json/AbpJsonAbstractionsModule.cs index 68de7be35e..422d34745d 100644 --- a/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpJsonCoreModule.cs +++ b/framework/src/Volo.Abp.Json.Abstractions/Volo/Abp/Json/AbpJsonAbstractionsModule.cs @@ -2,7 +2,7 @@ namespace Volo.Abp.Json; -public class AbpJsonCoreModule : AbpModule +public class AbpJsonAbstractionsModule : AbpModule { } diff --git a/framework/src/Volo.Abp.Json.Abstractions/Volo/Abp/Json/AbpJsonOptions.cs b/framework/src/Volo.Abp.Json.Abstractions/Volo/Abp/Json/AbpJsonOptions.cs new file mode 100644 index 0000000000..f7f5370255 --- /dev/null +++ b/framework/src/Volo.Abp.Json.Abstractions/Volo/Abp/Json/AbpJsonOptions.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.Json; + +public class AbpJsonOptions +{ + /// + /// Used to set default value for the DateTimeFormat. + /// + public string DefaultDateTimeFormat { get; set; } +} diff --git a/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/IJsonSerializer.cs b/framework/src/Volo.Abp.Json.Abstractions/Volo/Abp/Json/IJsonSerializer.cs similarity index 100% rename from framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/IJsonSerializer.cs rename to framework/src/Volo.Abp.Json.Abstractions/Volo/Abp/Json/IJsonSerializer.cs diff --git a/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpHybridJsonSerializer.cs b/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpHybridJsonSerializer.cs deleted file mode 100644 index f61f8b78ce..0000000000 --- a/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpHybridJsonSerializer.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Linq; -using JetBrains.Annotations; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Json; - -public class AbpHybridJsonSerializer : IJsonSerializer, ITransientDependency -{ - protected AbpJsonOptions Options { get; } - - protected IServiceScopeFactory ServiceScopeFactory { get; } - - public AbpHybridJsonSerializer(IOptions options, IServiceScopeFactory serviceScopeFactory) - { - Options = options.Value; - ServiceScopeFactory = serviceScopeFactory; - } - - public string Serialize([CanBeNull] object obj, bool camelCase = true, bool indented = false) - { - using (var scope = ServiceScopeFactory.CreateScope()) - { - var serializerProvider = GetSerializerProvider(scope.ServiceProvider, obj?.GetType()); - return serializerProvider.Serialize(obj, camelCase, indented); - } - } - - public T Deserialize([NotNull] string jsonString, bool camelCase = true) - { - Check.NotNull(jsonString, nameof(jsonString)); - - using (var scope = ServiceScopeFactory.CreateScope()) - { - var serializerProvider = GetSerializerProvider(scope.ServiceProvider, typeof(T)); - return serializerProvider.Deserialize(jsonString, camelCase); - } - } - - public object Deserialize(Type type, [NotNull] string jsonString, bool camelCase = true) - { - Check.NotNull(jsonString, nameof(jsonString)); - - using (var scope = ServiceScopeFactory.CreateScope()) - { - var serializerProvider = GetSerializerProvider(scope.ServiceProvider, type); - return serializerProvider.Deserialize(type, jsonString, camelCase); - } - } - - protected virtual IJsonSerializerProvider GetSerializerProvider(IServiceProvider serviceProvider, [CanBeNull] Type type) - { - foreach (var providerType in Options.Providers.Reverse()) - { - var provider = serviceProvider.GetRequiredService(providerType) as IJsonSerializerProvider; - if (provider.CanHandle(type)) - { - return provider; - } - } - - throw new AbpException($"There is no IJsonSerializerProvider that can handle '{type.GetFullNameWithAssemblyName()}'!"); - } -} diff --git a/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpJsonOptions.cs b/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpJsonOptions.cs deleted file mode 100644 index ffbd510b93..0000000000 --- a/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/AbpJsonOptions.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Volo.Abp.Collections; - -namespace Volo.Abp.Json; - -public class AbpJsonOptions -{ - /// - /// Used to set default value for the DateTimeFormat. - /// - public string DefaultDateTimeFormat { get; set; } - - public ITypeList Providers { get; } - - public AbpJsonOptions() - { - Providers = new TypeList(); - } -} diff --git a/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/IJsonSerializerProvider.cs b/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/IJsonSerializerProvider.cs deleted file mode 100644 index c3ae179d45..0000000000 --- a/framework/src/Volo.Abp.Json.Core/Volo/Abp/Json/IJsonSerializerProvider.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using JetBrains.Annotations; - -namespace Volo.Abp.Json; - -public interface IJsonSerializerProvider -{ - bool CanHandle([CanBeNull] Type type); - - string Serialize(object obj, bool camelCase = true, bool indented = false); - - T Deserialize(string jsonString, bool camelCase = true); - - object Deserialize(Type type, string jsonString, bool camelCase = true); -} diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo.Abp.Json.Newtonsoft.csproj b/framework/src/Volo.Abp.Json.Newtonsoft/Volo.Abp.Json.Newtonsoft.csproj index d53a9e2b33..6927cff0db 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo.Abp.Json.Newtonsoft.csproj +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo.Abp.Json.Newtonsoft.csproj @@ -15,7 +15,7 @@ - + diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs index 502265db84..a73213af6c 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs @@ -4,8 +4,6 @@ using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Volo.Abp.DependencyInjection; -using Volo.Abp.Reflection; -using Volo.Abp.Timing; namespace Volo.Abp.Json.Newtonsoft; @@ -20,38 +18,21 @@ public class AbpCamelCasePropertyNamesContractResolver : CamelCasePropertyNamesC true ); - NamingStrategy = new CamelCaseNamingStrategy(); + NamingStrategy = new CamelCaseNamingStrategy + { + ProcessDictionaryKeys = false + }; } protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { var property = base.CreateProperty(member, memberSerialization); - ModifyProperty(member, property); - - return property; - } - - protected virtual void ModifyProperty(MemberInfo member, JsonProperty property) - { - if (property.PropertyType != typeof(DateTime) && - property.PropertyType != typeof(DateTime?)) - { - return; - } - - if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault(member) == null) + if (AbpJsonIsoDateTimeConverter.ShouldNormalize(member, property)) { property.Converter = _dateTimeConverter.Value; } - } - protected override JsonDictionaryContract CreateDictionaryContract(Type objectType) - { - var contract = base.CreateDictionaryContract(objectType); - - contract.DictionaryKeyResolver = propertyName => propertyName; - - return contract; + return property; } } diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDefaultContractResolver.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDefaultContractResolver.cs new file mode 100644 index 0000000000..f39f6af578 --- /dev/null +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDefaultContractResolver.cs @@ -0,0 +1,33 @@ +using System; +using System.Reflection; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Json.Newtonsoft; + +public class AbpDefaultContractResolver : DefaultContractResolver, ITransientDependency +{ + private readonly Lazy _dateTimeConverter; + + public AbpDefaultContractResolver(IServiceProvider serviceProvider) + { + _dateTimeConverter = new Lazy( + serviceProvider.GetRequiredService, + true + ); + } + + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) + { + var property = base.CreateProperty(member, memberSerialization); + + if (AbpJsonIsoDateTimeConverter.ShouldNormalize(member, property)) + { + property.Converter = _dateTimeConverter.Value; + } + + return property; + } +} diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs index 605f06e9a6..b859688a0d 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs @@ -1,8 +1,11 @@ using System; +using System.Reflection; using Microsoft.Extensions.Options; using Newtonsoft.Json; using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Serialization; using Volo.Abp.DependencyInjection; +using Volo.Abp.Reflection; using Volo.Abp.Timing; namespace Volo.Abp.Json.Newtonsoft; @@ -43,4 +46,15 @@ public class AbpJsonIsoDateTimeConverter : IsoDateTimeConverter, ITransientDepen var date = value as DateTime?; base.WriteJson(writer, date.HasValue ? _clock.Normalize(date.Value) : value, serializer); } + + internal static bool ShouldNormalize(MemberInfo member, JsonProperty property) + { + if (property.PropertyType != typeof(DateTime) && + property.PropertyType != typeof(DateTime?)) + { + return false; + } + + return ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault(member) == null; + } } diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs index 0989dfdd03..c1f1f3a0cf 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs @@ -4,16 +4,11 @@ using Volo.Abp.Timing; namespace Volo.Abp.Json.Newtonsoft; -[DependsOn(typeof(AbpJsonCoreModule), typeof(AbpTimingModule))] +[DependsOn(typeof(AbpJsonAbstractionsModule), typeof(AbpTimingModule))] public class AbpJsonNewtonsoftModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - Configure(options => - { - options.Providers.Add(); - }); - context.Services.AddOptions() .Configure((options, contractResolver) => { diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerProvider.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializer.cs similarity index 80% rename from framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerProvider.cs rename to framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializer.cs index a23580bf6c..36ba95e74c 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerProvider.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializer.cs @@ -1,25 +1,24 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Newtonsoft.Json; using Volo.Abp.DependencyInjection; + namespace Volo.Abp.Json.Newtonsoft; -public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITransientDependency +[Dependency(ReplaceServices = true)] +public class AbpNewtonsoftJsonSerializer : IJsonSerializer, ITransientDependency { + protected IServiceProvider ServiceProvider { get; } protected IOptions Options { get; } - public AbpNewtonsoftJsonSerializerProvider(IOptions options) + public AbpNewtonsoftJsonSerializer(IServiceProvider serviceProvider, IOptions options) { + ServiceProvider = serviceProvider; Options = options; } - public bool CanHandle(Type type) - { - return true; - } - public string Serialize(object obj, bool camelCase = true, bool indented = false) { return JsonConvert.SerializeObject(obj, CreateJsonSerializerOptions(camelCase, indented)); @@ -35,8 +34,8 @@ public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITra return JsonConvert.DeserializeObject(jsonString, type, CreateJsonSerializerOptions(camelCase)); } - private readonly static ConcurrentDictionary JsonSerializerOptionsCache = new ConcurrentDictionary(); - + private static readonly ConcurrentDictionary JsonSerializerOptionsCache = + new ConcurrentDictionary(); protected virtual JsonSerializerSettings CreateJsonSerializerOptions(bool camelCase = true, bool indented = false) { @@ -46,8 +45,10 @@ public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITra indented }, _ => { - var settings = new JsonSerializerSettings { - Binder = Options.Value.JsonSerializerSettings.Binder, CheckAdditionalContent = Options.Value.JsonSerializerSettings.CheckAdditionalContent, + var settings = new JsonSerializerSettings + { + Binder = Options.Value.JsonSerializerSettings.Binder, + CheckAdditionalContent = Options.Value.JsonSerializerSettings.CheckAdditionalContent, Context = Options.Value.JsonSerializerSettings.Context, ContractResolver = Options.Value.JsonSerializerSettings.ContractResolver, ConstructorHandling = Options.Value.JsonSerializerSettings.ConstructorHandling, @@ -80,11 +81,9 @@ public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITra TypeNameAssemblyFormatHandling = Options.Value.JsonSerializerSettings.TypeNameAssemblyFormatHandling }; - - // if (camelCase) - // { - // settings.ContractResolver = ServiceProvider.GetRequiredService(); - // } + settings.ContractResolver = camelCase + ? ServiceProvider.GetRequiredService() + : ServiceProvider.GetRequiredService(); if (indented) { diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo.Abp.Json.SystemTextJson.csproj b/framework/src/Volo.Abp.Json.SystemTextJson/Volo.Abp.Json.SystemTextJson.csproj index 5402dd3c37..b3aba746a3 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo.Abp.Json.SystemTextJson.csproj +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo.Abp.Json.SystemTextJson.csproj @@ -15,7 +15,7 @@ - + diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs index 178d0aa6a1..50dd47d9e2 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs @@ -9,16 +9,11 @@ using Volo.Abp.Timing; namespace Volo.Abp.Json.SystemTextJson; -[DependsOn(typeof(AbpJsonCoreModule), typeof(AbpTimingModule))] +[DependsOn(typeof(AbpJsonAbstractionsModule), typeof(AbpTimingModule))] public class AbpJsonSystemTextJsonModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - Configure(options => - { - options.Providers.Add(); - }); - context.Services.AddOptions() .Configure((options, serviceProvider) => { diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerProvider.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializer.cs similarity index 76% rename from framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerProvider.cs rename to framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializer.cs index 43aad3383d..8313f6ee99 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerProvider.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializer.cs @@ -6,20 +6,15 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.Json.SystemTextJson; -public class AbpSystemTextJsonSerializerProvider : IJsonSerializerProvider, ITransientDependency +public class AbpSystemTextJsonSerializer : IJsonSerializer, ITransientDependency { protected AbpSystemTextJsonSerializerOptions Options { get; } - public AbpSystemTextJsonSerializerProvider(IOptions options) + public AbpSystemTextJsonSerializer(IOptions options) { Options = options.Value; } - public bool CanHandle(Type type) - { - return !Options.UnsupportedTypes.Contains(type); - } - public string Serialize(object obj, bool camelCase = true, bool indented = false) { return JsonSerializer.Serialize(obj, CreateJsonSerializerOptions(camelCase, indented)); @@ -35,7 +30,8 @@ public class AbpSystemTextJsonSerializerProvider : IJsonSerializerProvider, ITra return JsonSerializer.Deserialize(jsonString, type, CreateJsonSerializerOptions(camelCase)); } - private readonly static ConcurrentDictionary JsonSerializerOptionsCache = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary JsonSerializerOptionsCache = + new ConcurrentDictionary(); protected virtual JsonSerializerOptions CreateJsonSerializerOptions(bool camelCase = true, bool indented = false) { diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs index f9d6e1a5d9..7288be03c0 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs @@ -7,8 +7,6 @@ public class AbpSystemTextJsonSerializerOptions { public JsonSerializerOptions JsonSerializerOptions { get; } - public ITypeList UnsupportedTypes { get; } - public AbpSystemTextJsonSerializerOptions() { JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web) @@ -16,7 +14,5 @@ public class AbpSystemTextJsonSerializerOptions ReadCommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true }; - - UnsupportedTypes = new TypeList(); } } diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs index b96a73e1f7..a3168fa76f 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs @@ -10,20 +10,17 @@ public static class IncludeExtraPropertiesModifiers { public static void Modify(JsonTypeInfo jsonTypeInfo) { - var propertyJsonInfo = jsonTypeInfo.Properties.FirstOrDefault(x => x.PropertyType == typeof(ExtraPropertyDictionary) && - x.Name.Equals(nameof(ExtensibleObject.ExtraProperties), StringComparison.OrdinalIgnoreCase) && - x.Set == null); + var propertyJsonInfo = jsonTypeInfo.Properties.FirstOrDefault(x => + x.PropertyType == typeof(ExtraPropertyDictionary) && + x.Name.Equals(nameof(ExtensibleObject.ExtraProperties), StringComparison.OrdinalIgnoreCase) && + x.Set == null); + if (propertyJsonInfo != null) { - var propertyInfo = jsonTypeInfo.Type.GetProperty(nameof(ExtensibleObject.ExtraProperties)); - if (propertyInfo != null) + propertyJsonInfo.Set = (extraProperties, value) => { - var jsonPropertyInfo = jsonTypeInfo.CreateJsonPropertyInfo(typeof(ExtraPropertyDictionary), propertyJsonInfo.Name); - jsonPropertyInfo.Get = propertyInfo.GetValue; - jsonPropertyInfo.Set = propertyInfo.SetValue; - jsonTypeInfo.Properties.Remove(propertyJsonInfo); - jsonTypeInfo.Properties.Add(jsonPropertyInfo); - } + ObjectHelper.TrySetProperty(extraProperties.As(), x => x.ExtraProperties, () => (ExtraPropertyDictionary)value); + }; } } } diff --git a/framework/src/Volo.Abp.Json/Volo.Abp.Json.csproj b/framework/src/Volo.Abp.Json/Volo.Abp.Json.csproj index fd0408acba..eee618b18d 100644 --- a/framework/src/Volo.Abp.Json/Volo.Abp.Json.csproj +++ b/framework/src/Volo.Abp.Json/Volo.Abp.Json.csproj @@ -15,7 +15,6 @@ - diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs index ef9419a784..b3b9c459b6 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs @@ -1,10 +1,9 @@ -using Volo.Abp.Json.Newtonsoft; -using Volo.Abp.Json.SystemTextJson; +using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Modularity; namespace Volo.Abp.Json; -[DependsOn(typeof(AbpJsonNewtonsoftModule), typeof(AbpJsonSystemTextJsonModule))] +[DependsOn(typeof(AbpJsonSystemTextJsonModule))] public class AbpJsonModule : AbpModule { diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj index be8f77271d..930cdb5855 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo.Abp.AspNetCore.Mvc.Tests.csproj @@ -25,7 +25,6 @@ - diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs index 76a95a09cf..d8a49abf23 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs @@ -7,11 +7,9 @@ using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; -using Volo.Abp.AspNetCore.Mvc.Authorization; using Volo.Abp.AspNetCore.Mvc.GlobalFeatures; using Volo.Abp.AspNetCore.Mvc.Localization; using Volo.Abp.AspNetCore.Mvc.Localization.Resource; -using Volo.Abp.AspNetCore.Mvc.NewtonsoftJson; using Volo.Abp.AspNetCore.Security.Claims; using Volo.Abp.AspNetCore.TestBase; using Volo.Abp.Authorization; @@ -30,7 +28,7 @@ namespace Volo.Abp.AspNetCore.Mvc; [DependsOn( typeof(AbpAspNetCoreTestBaseModule), typeof(AbpMemoryDbTestModule), - typeof(AbpAspNetCoreMvcNewtonsoftModule), + typeof(AbpAspNetCoreMvcModule), typeof(AbpAutofacModule) )] public class AbpAspNetCoreMvcTestModule : AbpModule diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs index b2270adc42..befbd169fa 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs @@ -108,7 +108,7 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase } } -public abstract class ModelBindingController_Utc_Tests : ModelBindingController_Tests +public class ModelBindingController_Utc_Tests : ModelBindingController_Tests { protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) { @@ -119,7 +119,7 @@ public abstract class ModelBindingController_Utc_Tests : ModelBindingController_ } } -public abstract class ModelBindingController_Local_Tests : ModelBindingController_Tests +public class ModelBindingController_Local_Tests : ModelBindingController_Tests { protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) { @@ -129,46 +129,3 @@ public abstract class ModelBindingController_Local_Tests : ModelBindingControlle base.ConfigureServices(context, services); } } - -public class SystemTextJson_ModelBindingController_Utc_Tests : ModelBindingController_Utc_Tests -{ - -} - -public class SystemTextJson_ModelBindingController_Local_Tests : ModelBindingController_Local_Tests -{ - -} - -public class Newtonsoft_ModelBindingController_Utc_Tests : ModelBindingController_Utc_Tests -{ - protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) - { - services.Configure(options => - { - options.UnsupportedTypes.Add(); - options.UnsupportedTypes.Add(); - }); - - services.Configure>(x => - { - - }); - - base.ConfigureServices(context, services); - } -} - -public class Newtonsoft_ModelBindingController_Local_Tests : ModelBindingController_Local_Tests -{ - protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) - { - services.Configure(options => - { - options.UnsupportedTypes.Add(); - options.UnsupportedTypes.Add(); - }); - - base.ConfigureServices(context, services); - } -} diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Startup.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Startup.cs index ad4a222c33..df5a8b7b9c 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Startup.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Startup.cs @@ -1,4 +1,6 @@ -using System.IO; +using System; +using System.IO; +using System.Linq; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; @@ -14,13 +16,34 @@ public class Startup services.AddApplication(options => { var hostEnvironment = services.GetHostingEnvironment(); + var currentDirectory = hostEnvironment.ContentRootPath; + var plugDllInPath = ""; + + for (var i = 0; i < 10; i++) + { + var parentDirectory = new DirectoryInfo(currentDirectory).Parent; + if (parentDirectory == null) + { + break; + } + + if (parentDirectory.Name == "test") + { #if DEBUG - var plugDllInPath = Path.Combine(hostEnvironment.ContentRootPath, - @"..\..\..\..\..\Volo.Abp.AspNetCore.Mvc.PlugIn\bin\Debug\net7.0\"); + plugDllInPath = Path.Combine(parentDirectory.FullName, "Volo.Abp.AspNetCore.Mvc.PlugIn", "bin", "Debug", "net7.0"); #else - plugDllInPath = Path.Combine(_env.ContentRootPath, - @"..\..\..\..\..\Volo.Abp.AspNetCore.Mvc.PlugIn\bin\Release\net7.0\"); + plugDllInPath = Path.Combine(parentDirectory.FullName, "Volo.Abp.AspNetCore.Mvc.PlugIn", "bin", "Release", "net7.0"); #endif + break; + } + + currentDirectory = parentDirectory.FullName; + } + + if (plugDllInPath.IsNullOrWhiteSpace()) + { + throw new AbpException("Could not find the plug DLL path!"); + } options.PlugInSources.AddFolder(plugDllInPath); }); diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/JsonNetAuditSerializer_Test.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/JsonAuditSerializer_Test.cs similarity index 78% rename from framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/JsonNetAuditSerializer_Test.cs rename to framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/JsonAuditSerializer_Test.cs index 1dc3fcf839..cc20805947 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/JsonNetAuditSerializer_Test.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/JsonAuditSerializer_Test.cs @@ -1,18 +1,19 @@ using System; using System.Collections.Generic; +using System.Text.Json.Serialization; using Microsoft.Extensions.DependencyInjection; using Shouldly; using Xunit; namespace Volo.Abp.Auditing; -public class JsonNetAuditSerializer_Test : AbpAuditingTestBase +public class JsonAuditSerializer_Test : AbpAuditingTestBase { - private readonly JsonNetAuditSerializer _jsonNetAuditSerializer; + private readonly JsonAuditSerializer _jsonAuditSerializer; - public JsonNetAuditSerializer_Test() + public JsonAuditSerializer_Test() { - _jsonNetAuditSerializer = GetRequiredService(); + _jsonAuditSerializer = GetRequiredService(); } protected override void AfterAddApplication(IServiceCollection services) @@ -34,7 +35,7 @@ public class JsonNetAuditSerializer_Test : AbpAuditingTestBase {"input2", new Input2Dto {UserName = "admin", Password = "1q2w3E*", Birthday = DateTime.Now}} }; - var str = _jsonNetAuditSerializer.Serialize(arguments); + var str = _jsonAuditSerializer.Serialize(arguments); str.ShouldNotContain("IdCard"); str.ShouldNotContain("1q2w3E*"); @@ -58,7 +59,7 @@ public class JsonNetAuditSerializer_Test : AbpAuditingTestBase [DisableAuditing] public string Password { get; set; } - [Newtonsoft.Json.JsonIgnore] + [JsonIgnore] public string PrivateEmail { get; set; } public DateTime Birthday { get; set; } diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs deleted file mode 100644 index 5733d739d2..0000000000 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs +++ /dev/null @@ -1,149 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text.Json; -using Microsoft.Extensions.DependencyInjection; -using Newtonsoft.Json; -using Shouldly; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Json.Newtonsoft; -using Volo.Abp.Json.SystemTextJson; -using Xunit; -using JsonSerializer = Newtonsoft.Json.JsonSerializer; - -namespace Volo.Abp.Json; - -public class AbpHybridJsonSerializer_Tests : AbpJsonTestBase -{ - private readonly IJsonSerializer _jsonSerializer; - - public AbpHybridJsonSerializer_Tests() - { - _jsonSerializer = GetRequiredService(); - } - - protected override void AfterAddApplication(IServiceCollection services) - { - services.Configure(options => - { - options.UnsupportedTypes.Add(); - - options.JsonSerializerOptions.Converters.Add(new SystemTextJsonConverter()); - }); - - services.Configure(options => - { - options.Converters.Add(); - }); - } - - [Fact] - public void NewtonsoftSerialize_Test() - { - var json = _jsonSerializer.Serialize(new MyClass1 - { - Providers = new List - { - new MyClass3() - } - }); - - json.ShouldContain("Newtonsoft"); - } - - [Fact] - public void SystemTextJsonSerialize_Test() - { - var json = _jsonSerializer.Serialize(new MyClass2 - { - Providers = new List - { - new MyClass3() - } - }); - - json.ShouldContain("SystemTextJson"); - } - - [Fact] - public void SystemTextJsonSerialize_With_Dictionary_Test() - { - var json = _jsonSerializer.Serialize(new MyClassWithDictionary - { - Properties = - { - {"A", "AV"}, - {"B", "BV"} - } - }); - - var deserialized = _jsonSerializer.Deserialize(json); - deserialized.Properties.ShouldContain(p => p.Key == "A" && p.Value == "AV"); - deserialized.Properties.ShouldContain(p => p.Key == "B" && p.Value == "BV"); - } - - public class MyClass1 - { - public string Provider { get; set; } - - public List Providers { get; set; } - } - - public class MyClass2 - { - public string Provider { get; set; } - - public List Providers { get; set; } - } - - public class MyClass3 - { - public string Provider { get; set; } - } - - public class MyClassWithDictionary - { - public Dictionary Properties { get; set; } - - public MyClassWithDictionary() - { - Properties = new Dictionary(); - } - } - - class NewtonsoftJsonConverter : JsonConverter, ITransientDependency - { - public override void WriteJson(JsonWriter writer, MyClass1 value, JsonSerializer serializer) - { - value.Provider = "Newtonsoft"; - foreach (var provider in value.Providers) - { - provider.Provider = "Newtonsoft"; - } - - writer.WriteRawValue(JsonConvert.SerializeObject(value)); - } - - public override MyClass1 ReadJson(JsonReader reader, Type objectType, MyClass1 existingValue, bool hasExistingValue, JsonSerializer serializer) - { - return (MyClass1)serializer.Deserialize(reader, objectType); - } - } - - class SystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter, ITransientDependency - { - public override void Write(Utf8JsonWriter writer, MyClass2 value, JsonSerializerOptions options) - { - value.Provider = "SystemTextJson"; - foreach (var provider in value.Providers) - { - provider.Provider = "SystemTextJson"; - } - System.Text.Json.JsonSerializer.Serialize(writer, value); - } - - public override MyClass2 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return (MyClass2)System.Text.Json.JsonSerializer.Deserialize(ref reader, typeToConvert); - } - } -} diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs index 21b3fd51bb..f9823e7d65 100644 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSerializerProvider_Tests.cs @@ -11,11 +11,11 @@ namespace Volo.Abp.Json; public abstract class AbpSystemTextJsonSerializerProvider_TestBase : AbpJsonTestBase { - protected AbpSystemTextJsonSerializerProvider JsonSerializer; + protected AbpSystemTextJsonSerializer JsonSerializer; public AbpSystemTextJsonSerializerProvider_TestBase() { - JsonSerializer = GetRequiredService(); + JsonSerializer = GetRequiredService(); } public class TestExtensibleObjectClass : ExtensibleObject diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs index 617301bd6c..820f18b1a2 100644 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs @@ -10,7 +10,7 @@ public class ExtensibleObjectModifiers_Tests : AbpJsonTestBase [Fact] public void Should_Modify_Object() { - var jsonSerializer = GetRequiredService(); + var jsonSerializer = GetRequiredService(); var extensibleObject = jsonSerializer.Deserialize("{\"ExtraProperties\": {\"Test-Key\":\"Test-Value\"}}"); diff --git a/nupkg/common.ps1 b/nupkg/common.ps1 index 9f44b11c6d..a15a8e6b5d 100644 --- a/nupkg/common.ps1 +++ b/nupkg/common.ps1 @@ -192,7 +192,7 @@ $projects = ( "framework/src/Volo.Abp.Http", "framework/src/Volo.Abp.IdentityModel", "framework/src/Volo.Abp.Json", - "framework/src/Volo.Abp.Json.Core", + "framework/src/Volo.Abp.Json.Abstractions", "framework/src/Volo.Abp.Json.Newtonsoft", "framework/src/Volo.Abp.Json.SystemTextJson", "framework/src/Volo.Abp.Ldap",