From 5650f6461d857bbabd6b189c4b4bbb3ead2879ad Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 2 Nov 2020 11:26:30 +0800 Subject: [PATCH] Add AbpHybridJsonSerializer_Tests. --- .../AbpSystemTextJsonSerializerOptions.cs | 5 + .../AbpSystemTextJsonSupportTypeMatcher.cs | 4 +- ...SystemTextJsonSupportTypeMatcherOptions.cs | 14 -- .../ModelBindingController_Tests.cs | 2 +- .../Abp/Json/AbpHybridJsonSerializer_Tests.cs | 125 ++++++++++++++++++ ...pSystemTextJsonSupportTypeMatcher_Tests.cs | 2 +- 6 files changed, 134 insertions(+), 18 deletions(-) delete mode 100644 framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcherOptions.cs create mode 100644 framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs index 1f986e688b..17a4ba661a 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptions.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using Volo.Abp.Collections; namespace Volo.Abp.Json.SystemTextJson { @@ -6,6 +7,8 @@ namespace Volo.Abp.Json.SystemTextJson { public JsonSerializerOptions JsonSerializerOptions { get; } + public ITypeList UnsupportedTypes { get; } + public AbpSystemTextJsonSerializerOptions() { //TODO:Defaults? @@ -13,6 +16,8 @@ namespace Volo.Abp.Json.SystemTextJson //https://github.com/dotnet/runtime/blob/master/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerDefaults.cs JsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web); + + UnsupportedTypes = new TypeList(); } } } diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcher.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcher.cs index e53a4a1b1e..6b991f051a 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcher.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcher.cs @@ -6,9 +6,9 @@ namespace Volo.Abp.Json.SystemTextJson { public class AbpSystemTextJsonSupportTypeMatcher : ITransientDependency { - protected AbpSystemTextJsonSupportTypeMatcherOptions Options { get; } + protected AbpSystemTextJsonSerializerOptions Options { get; } - public AbpSystemTextJsonSupportTypeMatcher(IOptions options) + public AbpSystemTextJsonSupportTypeMatcher(IOptions options) { Options = options.Value; } diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcherOptions.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcherOptions.cs deleted file mode 100644 index be9b62352f..0000000000 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSupportTypeMatcherOptions.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Volo.Abp.Collections; - -namespace Volo.Abp.Json.SystemTextJson -{ - public class AbpSystemTextJsonSupportTypeMatcherOptions - { - public ITypeList UnsupportedTypes { get; } - - public AbpSystemTextJsonSupportTypeMatcherOptions() - { - UnsupportedTypes = new TypeList(); - } - } -} diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs index 556521d82c..fcce316772 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs @@ -20,7 +20,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ModelBinding protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) { - services.Configure(options => + services.Configure(options => { options.UnsupportedTypes.Add(); options.UnsupportedTypes.Add(); diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs new file mode 100644 index 0000000000..98f8acc7b4 --- /dev/null +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpHybridJsonSerializer_Tests.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Text.Json; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using Shouldly; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Json.Newtonsoft; +using Volo.Abp.Json.SystemTextJson; +using Xunit; +using JsonSerializer = Newtonsoft.Json.JsonSerializer; + +namespace Volo.Abp.Json +{ + public class AbpHybridJsonSerializer_Tests : AbpJsonTestBase + { + private readonly IJsonSerializer _jsonSerializer; + + public AbpHybridJsonSerializer_Tests() + { + _jsonSerializer = GetRequiredService(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + services.Configure(options => + { + options.UnsupportedTypes.Add(); + + options.JsonSerializerOptions.Converters.Add(new SystemTextJsonConverter()); + }); + + services.Configure(options => + { + options.Converters.Add(); + }); + } + + [Fact] + public void NewtonsoftSerialize_Test() + { + var json = _jsonSerializer.Serialize(new MyClass1 + { + Providers = new List + { + new MyClass3() + } + }); + + json.ShouldContain("Newtonsoft"); + } + + [Fact] + public void SystemTextJsonSerialize_Test() + { + var json = _jsonSerializer.Serialize(new MyClass2 + { + Providers = new List + { + new MyClass3() + } + }); + + json.ShouldContain("SystemTextJson"); + } + + public class MyClass1 + { + public string Provider { get; set; } + + public List Providers { get; set; } + } + + public class MyClass2 + { + public string Provider { get; set; } + + public List Providers { get; set; } + } + + public class MyClass3 + { + public string Provider { get; set; } + } + + class NewtonsoftJsonConverter : JsonConverter, ITransientDependency + { + public override void WriteJson(JsonWriter writer, MyClass1 value, JsonSerializer serializer) + { + value.Provider = "Newtonsoft"; + foreach (var provider in value.Providers) + { + provider.Provider = "Newtonsoft"; + } + + writer.WriteRawValue(JsonConvert.SerializeObject(value)); + } + + public override MyClass1 ReadJson(JsonReader reader, Type objectType, MyClass1 existingValue, bool hasExistingValue, JsonSerializer serializer) + { + return (MyClass1)serializer.Deserialize(reader, objectType); + } + } + + class SystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter, ITransientDependency + { + public override void Write(Utf8JsonWriter writer, MyClass2 value, JsonSerializerOptions options) + { + value.Provider = "SystemTextJson"; + foreach (var provider in value.Providers) + { + provider.Provider = "SystemTextJson"; + } + System.Text.Json.JsonSerializer.Serialize(writer, value); + } + + public override MyClass2 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return (MyClass2)System.Text.Json.JsonSerializer.Deserialize(ref reader, typeToConvert); + } + } + } +} diff --git a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSupportTypeMatcher_Tests.cs b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSupportTypeMatcher_Tests.cs index 0e49181fab..929e892af3 100644 --- a/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSupportTypeMatcher_Tests.cs +++ b/framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/AbpSystemTextJsonSupportTypeMatcher_Tests.cs @@ -18,7 +18,7 @@ namespace Volo.Abp.Json protected override void AfterAddApplication(IServiceCollection services) { - services.Configure(options => + services.Configure(options => { options.UnsupportedTypes.Add(); options.UnsupportedTypes.Add();