mirror of https://github.com/abpframework/abp.git
10 changed files with 92 additions and 30 deletions
@ -0,0 +1,20 @@ |
|||
using System; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.Json.SystemTextJson.Modifiers; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson; |
|||
|
|||
public class AbpSystemTextJsonSerializerModifiersOptionsSetup : IConfigureOptions<AbpSystemTextJsonSerializerModifiersOptions> |
|||
{ |
|||
protected IServiceProvider ServiceProvider { get; } |
|||
|
|||
public AbpSystemTextJsonSerializerModifiersOptionsSetup(IServiceProvider serviceProvider) |
|||
{ |
|||
ServiceProvider = serviceProvider; |
|||
} |
|||
|
|||
public void Configure(AbpSystemTextJsonSerializerModifiersOptions options) |
|||
{ |
|||
options.Modifiers.Add(new AbpDateTimeConverterModifier().CreateModifyAction(ServiceProvider)); |
|||
} |
|||
} |
|||
@ -0,0 +1,39 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Text.Json.Serialization.Metadata; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Json.SystemTextJson.JsonConverters; |
|||
using Volo.Abp.Reflection; |
|||
using Volo.Abp.Timing; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.Modifiers; |
|||
|
|||
public class AbpDateTimeConverterModifier |
|||
{ |
|||
private IServiceProvider _serviceProvider; |
|||
|
|||
public Action<JsonTypeInfo> CreateModifyAction(IServiceProvider serviceProvider) |
|||
{ |
|||
_serviceProvider = serviceProvider; |
|||
return Modify; |
|||
} |
|||
|
|||
private void Modify(JsonTypeInfo jsonTypeInfo) |
|||
{ |
|||
if (ReflectionHelper.GetAttributesOfMemberOrDeclaringType<DisableDateTimeNormalizationAttribute>(jsonTypeInfo.Type).Any()) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
foreach (var property in jsonTypeInfo.Properties.Where(x => x.PropertyType == typeof(DateTime) || x.PropertyType == typeof(DateTime?))) |
|||
{ |
|||
if (property.AttributeProvider == null || |
|||
!property.AttributeProvider.GetCustomAttributes(typeof(DisableDateTimeNormalizationAttribute), false).Any()) |
|||
{ |
|||
property.CustomConverter = property.PropertyType == typeof(DateTime) |
|||
? _serviceProvider.GetRequiredService<AbpDateTimeConverter>() |
|||
: _serviceProvider.GetRequiredService<AbpNullableDateTimeConverter>(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Text.Json.Serialization.Metadata; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.ObjectExtending; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.Modifiers; |
|||
|
|||
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); |
|||
if (propertyJsonInfo != null) |
|||
{ |
|||
var propertyInfo = jsonTypeInfo.Type.GetProperty(nameof(ExtensibleObject.ExtraProperties)); |
|||
if (propertyInfo != null) |
|||
{ |
|||
var jsonPropertyInfo = jsonTypeInfo.CreateJsonPropertyInfo(typeof(ExtraPropertyDictionary), propertyJsonInfo.Name); |
|||
jsonPropertyInfo.Get = propertyInfo.GetValue; |
|||
jsonPropertyInfo.Set = propertyInfo.SetValue; |
|||
jsonTypeInfo.Properties.Remove(propertyJsonInfo); |
|||
jsonTypeInfo.Properties.Add(jsonPropertyInfo); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue