diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs index efc0a6a883..5b59a7583d 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs @@ -14,7 +14,7 @@ public class AbpSystemTextJsonSerializerModifiersOptions { Modifiers = new List> { - IncludeExtraPropertiesModifiers.Modify, + AbpIncludeExtraPropertiesModifiers.Modify, }; } } diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIgnorePropertiesModifiers.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIgnorePropertiesModifiers.cs new file mode 100644 index 0000000000..6eff57bfbf --- /dev/null +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIgnorePropertiesModifiers.cs @@ -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 + where TClass : class +{ + private Expression> _propertySelector; + + public Action CreateModifyAction(Expression> propertySelector) + { + _propertySelector = propertySelector; + return Modify; + } + + public void Modify(JsonTypeInfo jsonTypeInfo) + { + if (jsonTypeInfo.Type == typeof(TClass)) + { + jsonTypeInfo.Properties.RemoveAll(x => x.Name == _propertySelector.Body.As().Member.Name); + } + } +} diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeExtraPropertiesModifiers.cs similarity index 54% rename from framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs rename to framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeExtraPropertiesModifiers.cs index a3168fa76f..edb0b121b7 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeExtraPropertiesModifiers.cs @@ -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().Name == nameof(ExtensibleObject.ExtraProperties) && + x.Set == null); if (propertyJsonInfo != null) { diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeNonPublicPropertiesModifiers.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeNonPublicPropertiesModifiers.cs similarity index 61% rename from framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeNonPublicPropertiesModifiers.cs rename to framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeNonPublicPropertiesModifiers.cs index fcca1d672c..2005bb4f5e 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeNonPublicPropertiesModifiers.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeNonPublicPropertiesModifiers.cs @@ -1,17 +1,19 @@ using System; using System.Linq; +using System.Linq.Expressions; +using System.Reflection; using System.Text.Json.Serialization.Metadata; namespace Volo.Abp.Json.SystemTextJson.Modifiers; -public class IncludeNonPublicPropertiesModifiers +public class AbpIncludeNonPublicPropertiesModifiers where TClass : class { - private string _propertyName; + private Expression> _propertySelector; - public Action CreateModifyAction(string propertyName) + public Action CreateModifyAction(Expression> propertySelector) { - _propertyName = propertyName; + _propertySelector = propertySelector; return Modify; } @@ -19,10 +21,11 @@ public class IncludeNonPublicPropertiesModifiers { if (jsonTypeInfo.Type == typeof(TClass)) { - var propertyJsonInfo = jsonTypeInfo.Properties.FirstOrDefault(x => x.Name.Equals(_propertyName, StringComparison.OrdinalIgnoreCase) && x.Set == null); + var propertyName = _propertySelector.Body.As().Member.Name; + var propertyJsonInfo = jsonTypeInfo.Properties.FirstOrDefault(x => x.Name == propertyName && x.Set == null); if (propertyJsonInfo != null) { - var propertyInfo = typeof(TClass).GetProperty(_propertyName); + var propertyInfo = typeof(TClass).GetProperty(propertyName, BindingFlags.NonPublic); if (propertyInfo != null) { var jsonPropertyInfo = jsonTypeInfo.CreateJsonPropertyInfo(typeof(TProperty), propertyJsonInfo.Name); diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIgnorePropertiesModifiers_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIgnorePropertiesModifiers_Tests.cs new file mode 100644 index 0000000000..138ffd46a7 --- /dev/null +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIgnorePropertiesModifiers_Tests.cs @@ -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(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + services.Configure(options => + { + options.Modifiers.Add(new AbpIgnorePropertiesModifiers>().CreateModifyAction(x => x.BarDtos)); + options.Modifiers.Add(new AbpIgnorePropertiesModifiers().CreateModifyAction(x => x.Id)); + }); + + base.AfterAddApplication(services); + } + + [Fact] + public void Test() + { + var json = _jsonSerializer.Serialize(new FooDto() + { + Name = "foo", + BarDtos = new List + { + 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 BarDtos { get; set; } + } + + class BarDto + { + public string Id { get; set; } + + public string Name { get; set; } + } +} diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIncludeNonPublicPropertiesModifiers_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIncludeNonPublicPropertiesModifiers_Tests.cs new file mode 100644 index 0000000000..d05ab2dfa0 --- /dev/null +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpIncludeNonPublicPropertiesModifiers_Tests.cs @@ -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(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + services.Configure(options => + { + options.Modifiers.Add(new AbpIncludeNonPublicPropertiesModifiers().CreateModifyAction(x => x.Name)); + options.Modifiers.Add(new AbpIncludeNonPublicPropertiesModifiers().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(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"; + } + } +} diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObject_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObject_Tests.cs index bdd41f20b6..a5bcb723db 100644 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObject_Tests.cs +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObject_Tests.cs @@ -7,7 +7,7 @@ using Xunit; namespace Volo.Abp.Json; -public class ExtensibleObject_Tests: AbpJsonTestBase +public class ExtensibleObject_Tests : AbpJsonTestBase { private readonly IJsonSerializer _jsonSerializer;