mirror of https://github.com/abpframework/abp.git
7 changed files with 179 additions and 13 deletions
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq.Expressions; |
|||
using System.Text.Json.Serialization.Metadata; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.Modifiers; |
|||
|
|||
public class AbpIgnorePropertiesModifiers<TClass, TProperty> |
|||
where TClass : class |
|||
{ |
|||
private Expression<Func<TClass, TProperty>> _propertySelector; |
|||
|
|||
public Action<JsonTypeInfo> CreateModifyAction(Expression<Func<TClass, TProperty>> propertySelector) |
|||
{ |
|||
_propertySelector = propertySelector; |
|||
return Modify; |
|||
} |
|||
|
|||
public void Modify(JsonTypeInfo jsonTypeInfo) |
|||
{ |
|||
if (jsonTypeInfo.Type == typeof(TClass)) |
|||
{ |
|||
jsonTypeInfo.Properties.RemoveAll(x => x.Name == _propertySelector.Body.As<MemberExpression>().Member.Name); |
|||
} |
|||
} |
|||
} |
|||
@ -1,19 +1,22 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
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 class AbpIncludeExtraPropertiesModifiers |
|||
{ |
|||
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 |
|||
.Where(x => x.AttributeProvider != null && x.AttributeProvider is MemberInfo) |
|||
.FirstOrDefault(x => |
|||
x.PropertyType == typeof(ExtraPropertyDictionary) && |
|||
x.AttributeProvider.As<MemberInfo>().Name == nameof(ExtensibleObject.ExtraProperties) && |
|||
x.Set == null); |
|||
|
|||
if (propertyJsonInfo != null) |
|||
{ |
|||
@ -0,0 +1,73 @@ |
|||
using System.Collections.Generic; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Json.SystemTextJson; |
|||
using Volo.Abp.Json.SystemTextJson.Modifiers; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Json; |
|||
|
|||
public class AbpIgnorePropertiesModifiers_Tests : AbpJsonTestBase |
|||
{ |
|||
private readonly IJsonSerializer _jsonSerializer; |
|||
|
|||
public AbpIgnorePropertiesModifiers_Tests() |
|||
{ |
|||
_jsonSerializer = GetRequiredService<IJsonSerializer>(); |
|||
} |
|||
|
|||
protected override void AfterAddApplication(IServiceCollection services) |
|||
{ |
|||
services.Configure<AbpSystemTextJsonSerializerModifiersOptions>(options => |
|||
{ |
|||
options.Modifiers.Add(new AbpIgnorePropertiesModifiers<FooDto, List<BarDto>>().CreateModifyAction(x => x.BarDtos)); |
|||
options.Modifiers.Add(new AbpIgnorePropertiesModifiers<BarDto, string>().CreateModifyAction(x => x.Id)); |
|||
}); |
|||
|
|||
base.AfterAddApplication(services); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Test() |
|||
{ |
|||
var json = _jsonSerializer.Serialize(new FooDto() |
|||
{ |
|||
Name = "foo", |
|||
BarDtos = new List<BarDto> |
|||
{ |
|||
new BarDto |
|||
{ |
|||
Name = "bar1" |
|||
}, |
|||
new BarDto |
|||
{ |
|||
Name = "bar2" |
|||
} |
|||
} |
|||
}); |
|||
|
|||
json.ShouldNotContain("bar"); |
|||
|
|||
json = _jsonSerializer.Serialize(new BarDto() |
|||
{ |
|||
Id = "id", |
|||
Name = "bar" |
|||
}); |
|||
|
|||
json.ShouldNotContain("id"); |
|||
} |
|||
|
|||
class FooDto |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public List<BarDto> BarDtos { get; set; } |
|||
} |
|||
|
|||
class BarDto |
|||
{ |
|||
public string Id { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Json.SystemTextJson; |
|||
using Volo.Abp.Json.SystemTextJson.Modifiers; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Json; |
|||
|
|||
public class AbpIncludeNonPublicPropertiesModifiers_Tests : AbpJsonTestBase |
|||
{ |
|||
private readonly IJsonSerializer _jsonSerializer; |
|||
|
|||
public AbpIncludeNonPublicPropertiesModifiers_Tests() |
|||
{ |
|||
_jsonSerializer = GetRequiredService<IJsonSerializer>(); |
|||
} |
|||
|
|||
protected override void AfterAddApplication(IServiceCollection services) |
|||
{ |
|||
services.Configure<AbpSystemTextJsonSerializerModifiersOptions>(options => |
|||
{ |
|||
options.Modifiers.Add(new AbpIncludeNonPublicPropertiesModifiers<NonPublicPropertiesClass, string>().CreateModifyAction(x => x.Name)); |
|||
options.Modifiers.Add(new AbpIncludeNonPublicPropertiesModifiers<NonPublicPropertiesClass, string>().CreateModifyAction(x => x.Age)); |
|||
}); |
|||
|
|||
base.AfterAddApplication(services); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Test() |
|||
{ |
|||
var json = _jsonSerializer.Serialize(new NonPublicPropertiesClass() |
|||
{ |
|||
Id = "id" |
|||
}); |
|||
|
|||
json.ShouldContain("id"); |
|||
json.ShouldContain("name"); |
|||
json.ShouldContain("age"); |
|||
|
|||
var obj = _jsonSerializer.Deserialize<NonPublicPropertiesClass>(json); |
|||
obj.Id.ShouldBe("id"); |
|||
obj.Name.ShouldBe("name"); |
|||
obj.Age.ShouldBe("age"); |
|||
} |
|||
|
|||
class NonPublicPropertiesClass |
|||
{ |
|||
public string Id { get; set; } |
|||
|
|||
public string Name { get; private set; } |
|||
|
|||
public string Age { get; protected set; } |
|||
|
|||
public NonPublicPropertiesClass() |
|||
{ |
|||
Name = "name"; |
|||
Age = "age"; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue