From 1e9c0871a3dfb53a553e72c38d24ed6d9b23394a Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 16 Aug 2022 16:21:17 +0800 Subject: [PATCH] Introduce `IncludeNonPublicPropertiesModifiers`. --- .../AbpMvcNewtonsoftJsonOptionsSetup.cs | 16 ++++- .../Mvc/Json/AbpJsonOptionsSetup.cs | 3 + .../Mvc/Json/AbpSystemTextJsonFormatter.cs | 11 ++-- ...CamelCasePropertyNamesContractResolver.cs} | 19 ++++-- .../AbpNewtonsoftJsonSerializerProvider.cs | 59 ++++++++--------- .../Volo.Abp.Json.SystemTextJson.csproj | 1 + .../AbpDefaultJsonTypeInfoResolver.cs | 16 +++++ ...ystemTextJsonSerializerModifiersOptions.cs | 21 ++++++ ...AbpSystemTextJsonSerializerOptionsSetup.cs | 2 + .../AbpSystemTextJsonSerializerProvider.cs | 9 +-- ...AbpSystemTextJsonUnsupportedTypeMatcher.cs | 21 ------ .../IncludeNonPublicPropertiesModifiers.cs | 37 +++++++++++ .../Abp/ObjectExtending/ExtensibleObject.cs | 2 - ...temTextJsonUnsupportedTypeMatcher_Tests.cs | 65 ------------------- .../Json/ExtensibleObjectModifiers_Tests.cs | 19 ++++++ 15 files changed, 161 insertions(+), 140 deletions(-) rename framework/src/{Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpNewtonsoftJsonContractResolver.cs => Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs} (65%) create mode 100644 framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpDefaultJsonTypeInfoResolver.cs create mode 100644 framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs delete mode 100644 framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonUnsupportedTypeMatcher.cs create mode 100644 framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeNonPublicPropertiesModifiers.cs delete mode 100644 framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonUnsupportedTypeMatcher_Tests.cs create mode 100644 framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpMvcNewtonsoftJsonOptionsSetup.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpMvcNewtonsoftJsonOptionsSetup.cs index 1d1dc6a373..ab7eaf2244 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpMvcNewtonsoftJsonOptionsSetup.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpMvcNewtonsoftJsonOptionsSetup.cs @@ -1,21 +1,31 @@ using System; +using System.Collections.Generic; +using System.Linq; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using Volo.Abp.Json.Newtonsoft; namespace Volo.Abp.AspNetCore.Mvc.NewtonsoftJson; public class AbpMvcNewtonsoftJsonOptionsSetup : IConfigureOptions { - protected IServiceProvider ServiceProvider { get; } + private readonly IServiceProvider _serviceProvider; public AbpMvcNewtonsoftJsonOptionsSetup(IServiceProvider serviceProvider) { - ServiceProvider = serviceProvider; + _serviceProvider = serviceProvider; } public void Configure(MvcNewtonsoftJsonOptions options) { - options.SerializerSettings.ContractResolver = ServiceProvider.GetRequiredService(); + options.SerializerSettings.ContractResolver = _serviceProvider.GetRequiredService(); + + var converters = _serviceProvider.GetRequiredService>().Value + .Converters.Select(converterType => _serviceProvider.GetRequiredService(converterType).As()) + .ToList(); + + options.SerializerSettings.Converters.InsertRange(0, converters); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs index 116adca4e8..650d3890c1 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs @@ -4,6 +4,7 @@ using System.Text.Json.Serialization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Json.SystemTextJson.JsonConverters; namespace Volo.Abp.AspNetCore.Mvc.Json; @@ -29,5 +30,7 @@ public class AbpJsonOptionsSetup : IConfigureOptions options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter()); options.JsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter()); + + options.JsonSerializerOptions.TypeInfoResolver = new AbpDefaultJsonTypeInfoResolver(ServiceProvider.GetRequiredService>()); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpSystemTextJsonFormatter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpSystemTextJsonFormatter.cs index e680d54fa5..fb8c509748 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpSystemTextJsonFormatter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpSystemTextJsonFormatter.cs @@ -13,22 +13,23 @@ namespace Volo.Abp.AspNetCore.Mvc.Json; public class AbpSystemTextJsonFormatter : IAbpHybridJsonInputFormatter, IAbpHybridJsonOutputFormatter, ITransientDependency { - private readonly AbpSystemTextJsonUnsupportedTypeMatcher _unsupportedTypeMatcher; private readonly IOptions _jsonOptions; + private readonly IOptions _systemTextJsonSerializerOptions; private readonly ILoggerFactory _loggerFactory; - public AbpSystemTextJsonFormatter(AbpSystemTextJsonUnsupportedTypeMatcher unsupportedTypeMatcher, + public AbpSystemTextJsonFormatter( IOptions jsonOptions, + IOptions systemTextJsonSerializerOptions, ILoggerFactory loggerFactory) { - _unsupportedTypeMatcher = unsupportedTypeMatcher; _jsonOptions = jsonOptions; + _systemTextJsonSerializerOptions = systemTextJsonSerializerOptions; _loggerFactory = loggerFactory; } Task IAbpHybridJsonInputFormatter.CanHandleAsync(Type type) { - return Task.FromResult(!_unsupportedTypeMatcher.Match(type)); + return Task.FromResult(!_systemTextJsonSerializerOptions.Value.UnsupportedTypes.Contains(type)); } public virtual Task GetTextInputFormatterAsync() @@ -40,7 +41,7 @@ public class AbpSystemTextJsonFormatter : IAbpHybridJsonInputFormatter, IAbpHybr Task IAbpHybridJsonOutputFormatter.CanHandleAsync(Type type) { - return Task.FromResult(!_unsupportedTypeMatcher.Match(type)); + return Task.FromResult(!_systemTextJsonSerializerOptions.Value.UnsupportedTypes.Contains(type)); } public Task GetTextOutputFormatterAsync() diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpNewtonsoftJsonContractResolver.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs similarity index 65% rename from framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpNewtonsoftJsonContractResolver.cs rename to framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs index f33c9abbd4..502265db84 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.NewtonsoftJson/Volo/Abp/AspNetCore/Mvc/NewtonsoftJson/AbpNewtonsoftJsonContractResolver.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpCamelCasePropertyNamesContractResolver.cs @@ -4,17 +4,16 @@ using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Volo.Abp.DependencyInjection; -using Volo.Abp.Json.Newtonsoft; using Volo.Abp.Reflection; using Volo.Abp.Timing; -namespace Volo.Abp.AspNetCore.Mvc.NewtonsoftJson; +namespace Volo.Abp.Json.Newtonsoft; -public class AbpNewtonsoftJsonContractResolver : DefaultContractResolver, ITransientDependency +public class AbpCamelCasePropertyNamesContractResolver : CamelCasePropertyNamesContractResolver, ITransientDependency { private readonly Lazy _dateTimeConverter; - public AbpNewtonsoftJsonContractResolver(IServiceProvider serviceProvider) + public AbpCamelCasePropertyNamesContractResolver(IServiceProvider serviceProvider) { _dateTimeConverter = new Lazy( serviceProvider.GetRequiredService, @@ -35,7 +34,8 @@ public class AbpNewtonsoftJsonContractResolver : DefaultContractResolver, ITrans protected virtual void ModifyProperty(MemberInfo member, JsonProperty property) { - if (property.PropertyType != typeof(DateTime) && property.PropertyType != typeof(DateTime?)) + if (property.PropertyType != typeof(DateTime) && + property.PropertyType != typeof(DateTime?)) { return; } @@ -45,4 +45,13 @@ public class AbpNewtonsoftJsonContractResolver : DefaultContractResolver, ITrans property.Converter = _dateTimeConverter.Value; } } + + protected override JsonDictionaryContract CreateDictionaryContract(Type objectType) + { + var contract = base.CreateDictionaryContract(objectType); + + contract.DictionaryKeyResolver = propertyName => propertyName; + + return contract; + } } diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerProvider.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerProvider.cs index 14ae0556f3..01c7c1be14 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerProvider.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpNewtonsoftJsonSerializerProvider.cs @@ -1,25 +1,23 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; using Volo.Abp.DependencyInjection; - namespace Volo.Abp.Json.Newtonsoft; public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITransientDependency { - private static readonly CamelCaseExceptDictionaryKeysResolver SharedCamelCaseExceptDictionaryKeysResolver = - new CamelCaseExceptDictionaryKeysResolver(); - + protected IServiceProvider ServiceProvider{ get; } protected List Converters { get; } public AbpNewtonsoftJsonSerializerProvider( - IOptions options, - IServiceProvider serviceProvider) + IServiceProvider serviceProvider, + IOptions options) { + ServiceProvider = serviceProvider; Converters = options.Value .Converters .Select(c => (JsonConverter)serviceProvider.GetRequiredService(c)) @@ -33,47 +31,44 @@ public class AbpNewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ITra public string Serialize(object obj, bool camelCase = true, bool indented = false) { - return JsonConvert.SerializeObject(obj, CreateSerializerSettings(camelCase, indented)); + return JsonConvert.SerializeObject(obj, CreateJsonSerializerOptions(camelCase, indented)); } public T Deserialize(string jsonString, bool camelCase = true) { - return JsonConvert.DeserializeObject(jsonString, CreateSerializerSettings(camelCase)); + return JsonConvert.DeserializeObject(jsonString, CreateJsonSerializerOptions(camelCase)); } public object Deserialize(Type type, string jsonString, bool camelCase = true) { - return JsonConvert.DeserializeObject(jsonString, type, CreateSerializerSettings(camelCase)); + return JsonConvert.DeserializeObject(jsonString, type, CreateJsonSerializerOptions(camelCase)); } - protected virtual JsonSerializerSettings CreateSerializerSettings(bool camelCase = true, bool indented = false) - { - var settings = new JsonSerializerSettings(); + private readonly static ConcurrentDictionary JsonSerializerOptionsCache = new ConcurrentDictionary(); - settings.Converters.InsertRange(0, Converters); - - if (camelCase) + protected virtual JsonSerializerSettings CreateJsonSerializerOptions(bool camelCase = true, bool indented = false) + { + return JsonSerializerOptionsCache.GetOrAdd(new { - settings.ContractResolver = SharedCamelCaseExceptDictionaryKeysResolver; - } - - if (indented) + camelCase, + indented + }, _ => { - settings.Formatting = Formatting.Indented; - } + var settings = new JsonSerializerSettings(); - return settings; - } + settings.Converters.InsertRange(0, Converters); - private class CamelCaseExceptDictionaryKeysResolver : CamelCasePropertyNamesContractResolver - { - protected override JsonDictionaryContract CreateDictionaryContract(Type objectType) - { - var contract = base.CreateDictionaryContract(objectType); + if (camelCase) + { + settings.ContractResolver = ServiceProvider.GetRequiredService(); + } - contract.DictionaryKeyResolver = propertyName => propertyName; + if (indented) + { + settings.Formatting = Formatting.Indented; + } - return contract; - } + return settings; + }); } } diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo.Abp.Json.SystemTextJson.csproj b/framework/src/Volo.Abp.Json.SystemTextJson/Volo.Abp.Json.SystemTextJson.csproj index c9aa014c82..5402dd3c37 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo.Abp.Json.SystemTextJson.csproj +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo.Abp.Json.SystemTextJson.csproj @@ -17,6 +17,7 @@ + diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpDefaultJsonTypeInfoResolver.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpDefaultJsonTypeInfoResolver.cs new file mode 100644 index 0000000000..385544dc0b --- /dev/null +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpDefaultJsonTypeInfoResolver.cs @@ -0,0 +1,16 @@ +using System.Text.Json.Serialization.Metadata; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Json.SystemTextJson; + +public class AbpDefaultJsonTypeInfoResolver : DefaultJsonTypeInfoResolver, ITransientDependency +{ + public AbpDefaultJsonTypeInfoResolver(IOptions options) + { + foreach (var modifier in options.Value.Modifiers) + { + Modifiers.Add(modifier); + } + } +} 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 new file mode 100644 index 0000000000..4f4f8d1bac --- /dev/null +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text.Json.Serialization.Metadata; +using Volo.Abp.Data; +using Volo.Abp.Json.SystemTextJson.Modifiers; +using Volo.Abp.ObjectExtending; + +namespace Volo.Abp.Json.SystemTextJson; + +public class AbpSystemTextJsonSerializerModifiersOptions +{ + public List> Modifiers { get; } + + public AbpSystemTextJsonSerializerModifiersOptions() + { + Modifiers = new List> + { + new IncludeNonPublicPropertiesModifiers().CreateModifyAction("ExtraProperties") + }; + } +} diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs index 1615581a9d..a5806b28be 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs @@ -28,5 +28,7 @@ public class AbpSystemTextJsonSerializerOptionsSetup : IConfigureOptions>()); } } diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerProvider.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerProvider.cs index 802dd3f528..43aad3383d 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerProvider.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerProvider.cs @@ -10,19 +10,14 @@ public class AbpSystemTextJsonSerializerProvider : IJsonSerializerProvider, ITra { protected AbpSystemTextJsonSerializerOptions Options { get; } - protected AbpSystemTextJsonUnsupportedTypeMatcher AbpSystemTextJsonUnsupportedTypeMatcher { get; } - - public AbpSystemTextJsonSerializerProvider( - IOptions options, - AbpSystemTextJsonUnsupportedTypeMatcher abpSystemTextJsonUnsupportedTypeMatcher) + public AbpSystemTextJsonSerializerProvider(IOptions options) { - AbpSystemTextJsonUnsupportedTypeMatcher = abpSystemTextJsonUnsupportedTypeMatcher; Options = options.Value; } public bool CanHandle(Type type) { - return !AbpSystemTextJsonUnsupportedTypeMatcher.Match(type); + return !Options.UnsupportedTypes.Contains(type); } public string Serialize(object obj, bool camelCase = true, bool indented = false) diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonUnsupportedTypeMatcher.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonUnsupportedTypeMatcher.cs deleted file mode 100644 index eecc20c555..0000000000 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonUnsupportedTypeMatcher.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using JetBrains.Annotations; -using Microsoft.Extensions.Options; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Json.SystemTextJson; - -public class AbpSystemTextJsonUnsupportedTypeMatcher : ITransientDependency -{ - protected AbpSystemTextJsonSerializerOptions Options { get; } - - public AbpSystemTextJsonUnsupportedTypeMatcher(IOptions options) - { - Options = options.Value; - } - - public virtual bool Match([CanBeNull] Type type) - { - return Options.UnsupportedTypes.Contains(type); - } -} 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/IncludeNonPublicPropertiesModifiers.cs new file mode 100644 index 0000000000..fcca1d672c --- /dev/null +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeNonPublicPropertiesModifiers.cs @@ -0,0 +1,37 @@ +using System; +using System.Linq; +using System.Text.Json.Serialization.Metadata; + +namespace Volo.Abp.Json.SystemTextJson.Modifiers; + +public class IncludeNonPublicPropertiesModifiers + where TClass : class +{ + private string _propertyName; + + public Action CreateModifyAction(string propertyName) + { + _propertyName = propertyName; + return Modify; + } + + public void Modify(JsonTypeInfo jsonTypeInfo) + { + if (jsonTypeInfo.Type == typeof(TClass)) + { + var propertyJsonInfo = jsonTypeInfo.Properties.FirstOrDefault(x => x.Name.Equals(_propertyName, StringComparison.OrdinalIgnoreCase) && x.Set == null); + if (propertyJsonInfo != null) + { + var propertyInfo = typeof(TClass).GetProperty(_propertyName); + if (propertyInfo != null) + { + var jsonPropertyInfo = jsonTypeInfo.CreateJsonPropertyInfo(typeof(TProperty), propertyJsonInfo.Name); + jsonPropertyInfo.Get = propertyInfo.GetValue; + jsonPropertyInfo.Set = propertyInfo.SetValue; + jsonTypeInfo.Properties.Remove(propertyJsonInfo); + jsonTypeInfo.Properties.Add(jsonPropertyInfo); + } + } + } + } +} diff --git a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObject.cs b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObject.cs index 4479eeb648..611176b570 100644 --- a/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObject.cs +++ b/framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObject.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.Text.Json.Serialization; using Volo.Abp.Data; using Volo.Abp.DynamicProxy; @@ -10,7 +9,6 @@ namespace Volo.Abp.ObjectExtending; [Serializable] public class ExtensibleObject : IHasExtraProperties, IValidatableObject { - [JsonInclude] public ExtraPropertyDictionary ExtraProperties { get; protected set; } public ExtensibleObject() diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonUnsupportedTypeMatcher_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonUnsupportedTypeMatcher_Tests.cs deleted file mode 100644 index 1c621f3930..0000000000 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonUnsupportedTypeMatcher_Tests.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.Extensions.DependencyInjection; -using Shouldly; -using Volo.Abp.Json.SystemTextJson; -using Xunit; - -namespace Volo.Abp.Json; - -public class AbpSystemTextJsonUnsupportedTypeMatcher_Tests : AbpJsonTestBase -{ - private readonly AbpSystemTextJsonUnsupportedTypeMatcher _abpSystemTextJsonUnsupportedTypeMatcher; - - public AbpSystemTextJsonUnsupportedTypeMatcher_Tests() - { - _abpSystemTextJsonUnsupportedTypeMatcher = GetRequiredService(); - } - - protected override void AfterAddApplication(IServiceCollection services) - { - services.Configure(options => - { - options.UnsupportedTypes.Add(); - options.UnsupportedTypes.Add(); - options.UnsupportedTypes.Add>(); - }); - } - - [Fact] - public void Match_Test() - { - _abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(MyClass)).ShouldBeTrue(); - _abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(byte[])).ShouldBeTrue(); - - _abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(MyClass2)).ShouldBeFalse(); - _abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(MyClass3)).ShouldBeFalse(); - _abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(MyClass4)).ShouldBeFalse(); - - _abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(string)).ShouldBeFalse(); - _abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(string[])).ShouldBeFalse(); - - _abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(Dictionary)).ShouldBeTrue(); - _abpSystemTextJsonUnsupportedTypeMatcher.Match(typeof(IDictionary)).ShouldBeFalse(); - } - - class MyClass - { - public DateTime Prop1 { get; set; } - } - - class MyClass2 - { - public DateTime Prop1 { get; set; } - } - - class MyClass3 - { - public MyClass4 Prop1 { get; set; } - } - - class MyClass4 - { - public DateTime Prop1 { get; set; } - } -} diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs new file mode 100644 index 0000000000..617301bd6c --- /dev/null +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs @@ -0,0 +1,19 @@ +using Shouldly; +using Volo.Abp.Json.SystemTextJson; +using Volo.Abp.ObjectExtending; +using Xunit; + +namespace Volo.Abp.Json; + +public class ExtensibleObjectModifiers_Tests : AbpJsonTestBase +{ + [Fact] + public void Should_Modify_Object() + { + var jsonSerializer = GetRequiredService(); + + var extensibleObject = jsonSerializer.Deserialize("{\"ExtraProperties\": {\"Test-Key\":\"Test-Value\"}}"); + + extensibleObject.ExtraProperties.ShouldContainKeyAndValue("Test-Key", "Test-Value"); + } +}