From a99c2aaa942813c3202a26e806b6460546205cc7 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 21 Jul 2021 18:56:20 +0800 Subject: [PATCH] Supports nested ExtensibleObject classes. Resolve #9581 --- .../AbpHasExtraPropertiesJsonConverter.cs | 1 + ...pHasExtraPropertiesJsonConverterFactory.cs | 13 ++++ ...bpHasExtraPropertiesJsonConverter_Tests.cs | 60 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHasExtraPropertiesJsonConverter_Tests.cs diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverter.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverter.cs index 47640ec306..06cf0ae040 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverter.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverter.cs @@ -15,6 +15,7 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters x == this || x.GetType() == typeof(AbpHasExtraPropertiesJsonConverterFactory)); + newOptions.Converters.Add(new AbpHasExtraPropertiesJsonConverterFactory(typeToConvert)); var rootElement = JsonDocument.ParseValue(ref reader).RootElement; if (rootElement.ValueKind == JsonValueKind.Object) { diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverterFactory.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverterFactory.cs index f654504e05..0bc9cef11e 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverterFactory.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpHasExtraPropertiesJsonConverterFactory.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Reflection; using System.Text.Json; using System.Text.Json.Serialization; @@ -11,8 +12,20 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters { private static readonly ConcurrentDictionary CachedTypes = new ConcurrentDictionary(); + private static readonly List ExcludeTypes = new List(); + + public AbpHasExtraPropertiesJsonConverterFactory(params Type[] excludeTypes) + { + ExcludeTypes.AddIfNotContains(excludeTypes); + } + public override bool CanConvert(Type typeToConvert) { + if (ExcludeTypes.Contains(typeToConvert)) + { + return false; + } + //Only for private or protected ExtraProperties. if (typeof(IHasExtraProperties).IsAssignableFrom(typeToConvert)) { diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHasExtraPropertiesJsonConverter_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHasExtraPropertiesJsonConverter_Tests.cs new file mode 100644 index 0000000000..b80380086d --- /dev/null +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHasExtraPropertiesJsonConverter_Tests.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; +using System.Linq; +using Shouldly; +using Volo.Abp.Data; +using Volo.Abp.ObjectExtending; +using Xunit; + +namespace Volo.Abp.Json +{ + public class AbpHasExtraPropertiesJsonConverter_Tests: AbpJsonTestBase + { + private readonly IJsonSerializer _jsonSerializer; + + public AbpHasExtraPropertiesJsonConverter_Tests() + { + _jsonSerializer = GetRequiredService(); + } + + [Fact] + public void JsonConverter_Test() + { + var fooDto = new FooDto + { + Name = "foo-dto", + BarDtos = new List() + }; + fooDto.SetProperty("foo", "foo-value"); + + var barDto = new BarDto + { + Name = "bar-dto" + }; + barDto.SetProperty("bar", "bar-value"); + fooDto.BarDtos.Add(barDto); + + var json = _jsonSerializer.Serialize(fooDto); + + fooDto = _jsonSerializer.Deserialize(json); + fooDto.ShouldNotBeNull(); + fooDto.Name.ShouldBe("foo-dto"); + fooDto.GetProperty("foo").ShouldBe("foo-value"); + + fooDto.BarDtos.Count.ShouldBe(1); + fooDto.BarDtos.First().Name.ShouldBe("bar-dto"); + fooDto.BarDtos.First().GetProperty("bar").ShouldBe("bar-value"); + } + } + + public class FooDto : ExtensibleObject + { + public string Name { get; set; } + + public List BarDtos { get; set; } + } + + public class BarDto : ExtensibleObject + { + public string Name { get; set; } + } +}