Revert to use Newtonsoft for IAuditSerializer.

Add JsonNetAuditSerializer_Test.
pull/5952/head
maliming 5 years ago
parent 7b06cfb5a2
commit 0bde769e11

@ -22,5 +22,9 @@
<ProjectReference Include="..\Volo.Abp.Threading\Volo.Abp.Threading.csproj" />
<ProjectReference Include="..\Volo.Abp.Timing\Volo.Abp.Timing.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

@ -0,0 +1,43 @@
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<Type> _ignoredTypes;
public AuditingContractResolver(List<Type> 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;
}
}
}

@ -1,45 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Volo.Abp.Auditing
{
public class AuditingRuntimeIgnoreConverter : JsonConverter<Dictionary<string, object>>
{
private readonly List<Type> _ignoredTypes;
public AuditingRuntimeIgnoreConverter(List<Type> ignoredTypes)
{
_ignoredTypes = ignoredTypes;
}
public override Dictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new JsonException();
}
public override void Write(Utf8JsonWriter writer, Dictionary<string, object> value, JsonSerializerOptions options)
{
var newDictionary = new Dictionary<string, object>();
foreach (var item in value.Where(x => x.Value != null))
{
if (item.GetType().IsDefined(typeof(DisableAuditingAttribute), true) ||
item.GetType().IsDefined(typeof(JsonIgnoreAttribute), true))
{
continue;
}
if (_ignoredTypes.Any(x => x.IsInstanceOfType(item.Value)))
{
continue;
}
newDictionary[item.Key] = item.Value;
}
JsonSerializer.Serialize(writer, newDictionary);
}
}
}

@ -1,5 +1,5 @@
using System.Text.Json;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Auditing
@ -16,14 +16,29 @@ namespace Volo.Abp.Auditing
public string Serialize(object obj)
{
return JsonSerializer.Serialize(obj, GetJsonSerializerOptions());
return JsonConvert.SerializeObject(obj, GetSharedJsonSerializerSettings());
}
private JsonSerializerOptions GetJsonSerializerOptions()
private static readonly object SyncObj = new object();
private static JsonSerializerSettings _sharedJsonSerializerSettings;
private JsonSerializerSettings GetSharedJsonSerializerSettings()
{
var options = new JsonSerializerOptions();
options.Converters.Add(new AuditingRuntimeIgnoreConverter(Options.IgnoredTypes));
return options;
if (_sharedJsonSerializerSettings == null)
{
lock (SyncObj)
{
if (_sharedJsonSerializerSettings == null)
{
_sharedJsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new AuditingContractResolver(Options.IgnoredTypes)
};
}
}
}
return _sharedJsonSerializerSettings;
}
}
}

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
namespace Volo.Abp.Auditing
{
public class JsonNetAuditSerializer_Test : AbpAuditingTestBase
{
private readonly JsonNetAuditSerializer _jsonNetAuditSerializer;
public JsonNetAuditSerializer_Test()
{
_jsonNetAuditSerializer = GetRequiredService<JsonNetAuditSerializer>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
services.Configure<AbpAuditingOptions>(options =>
{
options.IgnoredTypes.Add(typeof(DateTime));
});
base.AfterAddApplication(services);
}
[Fact]
public void Serialize_Test()
{
var arguments = new Dictionary<string, object>
{
{"input", new InputDto {PersonData = "IdCard:123123"}},
{"input2", new Input2Dto {UserName = "admin", Password = "1q2w3E*", Birthday = DateTime.Now}}
};
var str = _jsonNetAuditSerializer.Serialize(arguments);
str.ShouldNotContain("IdCard");
str.ShouldNotContain("1q2w3E*");
str.ShouldNotContain("Birthday");
str.ShouldContain("UserName");
str.ShouldContain("admin");
}
[DisableAuditing]
class InputDto
{
public string PersonData { get; set; }
}
class Input2Dto
{
public string UserName { get; set; }
[DisableAuditing]
public string Password { get; set; }
[Newtonsoft.Json.JsonIgnore]
public string PrivateEmail { get; set; }
public DateTime Birthday { get; set; }
}
}
}
Loading…
Cancel
Save