From 9cd73ee329bdeca8e50151ef820e015d2ee2cbcf Mon Sep 17 00:00:00 2001 From: Nokecy Date: Tue, 28 May 2019 09:32:53 +0800 Subject: [PATCH] DateTimeConverter with MvcJsonOptions --- .../AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs | 33 ++++++++------ .../Mvc/Json/AbpMvcJsonContractResolver.cs | 43 +++++++++++++++++++ .../DisableDateTimeNormalizationAttribute.cs | 10 +++++ 3 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpMvcJsonContractResolver.cs create mode 100644 framework/src/Volo.Abp.Timing/Volo/Abp/Timing/DisableDateTimeNormalizationAttribute.cs 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 9729476f2f..fa317d7211 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 @@ -1,34 +1,36 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ApplicationParts; -using Microsoft.AspNetCore.Mvc.Razor; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Modularity; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.AspNetCore.Mvc.ViewComponents; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Options; +using Newtonsoft.Json.Serialization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; using Volo.Abp.ApiVersioning; using Volo.Abp.AspNetCore.Mvc.Conventions; using Volo.Abp.AspNetCore.Mvc.DependencyInjection; +using Volo.Abp.AspNetCore.Mvc.Json; using Volo.Abp.AspNetCore.Mvc.Localization; using Volo.Abp.AspNetCore.VirtualFileSystem; +using Volo.Abp.DependencyInjection; using Volo.Abp.Http.Modeling; using Volo.Abp.Localization; +using Volo.Abp.Modularity; using Volo.Abp.UI; namespace Volo.Abp.AspNetCore.Mvc { [DependsOn( - typeof(AbpAspNetCoreModule), - typeof(AbpLocalizationModule), - typeof(AbpApiVersioningAbstractionsModule), + typeof(AbpAspNetCoreModule), + typeof(AbpLocalizationModule), + typeof(AbpApiVersioningAbstractionsModule), typeof(AbpAspNetCoreMvcContractsModule), typeof(AbpUiModule) )] @@ -109,6 +111,11 @@ namespace Volo.Abp.AspNetCore.Mvc { mvcOptions.AddAbp(context.Services); }); + + Configure(jsonOptions => + { + jsonOptions.SerializerSettings.ContractResolver = new AbpMvcJsonContractResolver(context.Services); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpMvcJsonContractResolver.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpMvcJsonContractResolver.cs new file mode 100644 index 0000000000..55174481a0 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpMvcJsonContractResolver.cs @@ -0,0 +1,43 @@ +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using System; +using System.Reflection; +using Volo.Abp.Json.Newtonsoft; +using Volo.Abp.Reflection; +using Volo.Abp.Timing; + +namespace Volo.Abp.AspNetCore.Mvc.Json +{ + public class AbpMvcJsonContractResolver : DefaultContractResolver + { + private readonly Lazy _dateTimeConverter; + public AbpMvcJsonContractResolver(IServiceCollection services) + { + _dateTimeConverter = services.GetServiceLazy(); + } + + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) + { + JsonProperty 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) + { + property.Converter = _dateTimeConverter.Value; + } + + } + } +} diff --git a/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/DisableDateTimeNormalizationAttribute.cs b/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/DisableDateTimeNormalizationAttribute.cs new file mode 100644 index 0000000000..6e9811b538 --- /dev/null +++ b/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/DisableDateTimeNormalizationAttribute.cs @@ -0,0 +1,10 @@ +using System; + +namespace Volo.Abp.Timing +{ + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Parameter)] + public class DisableDateTimeNormalizationAttribute : Attribute + { + + } +}