mirror of https://github.com/abpframework/abp
parent
e2748ce82a
commit
ff2ecc004f
@ -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<AbpAuditingOptions> options)
|
||||
{
|
||||
Options = options.Value;
|
||||
}
|
||||
|
||||
public string Serialize(object obj)
|
||||
{
|
||||
return JsonSerializer.Serialize(obj, CreateJsonSerializerOptions());
|
||||
}
|
||||
|
||||
private static readonly ConcurrentDictionary<string, JsonSerializerOptions> JsonSerializerOptionsCache =
|
||||
new ConcurrentDictionary<string, JsonSerializerOptions>();
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -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<AbpAuditingOptions> 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;
|
||||
}
|
||||
}
|
||||
@ -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<AbpJsonIsoDateTimeConverter> _dateTimeConverter;
|
||||
|
||||
public AbpDefaultContractResolver(IServiceProvider serviceProvider)
|
||||
{
|
||||
_dateTimeConverter = new Lazy<AbpJsonIsoDateTimeConverter>(
|
||||
serviceProvider.GetRequiredService<AbpJsonIsoDateTimeConverter>,
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue