From 2cd2d3a1349a888393e93e941e7ff46f11950059 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 10 Sep 2021 10:27:37 +0800 Subject: [PATCH] Add NewtonsoftStringValueTypeJsonConverter. --- ...ureManagementApplicationContractsModule.cs | 6 ++ .../NewtonsoftStringValueTypeJsonConverter.cs | 90 +++++++++++++++++++ ...ewtonsoftStringValueJsonConverter_Tests.cs | 16 ++++ .../StringValueJsonConverter_Tests.cs | 3 +- ...mTextJsonStringValueJsonConverter_Tests.cs | 16 ++++ 5 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/NewtonsoftStringValueTypeJsonConverter.cs create mode 100644 modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/NewtonsoftStringValueJsonConverter_Tests.cs create mode 100644 modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/SystemTextJsonStringValueJsonConverter_Tests.cs diff --git a/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs b/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs index f7e39e18d2..854a9f7ff6 100644 --- a/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs +++ b/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs @@ -2,6 +2,7 @@ using Volo.Abp.Application; using Volo.Abp.Authorization; using Volo.Abp.FeatureManagement.JsonConverters; +using Volo.Abp.Json.Newtonsoft; using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Modularity; using Volo.Abp.VirtualFileSystem; @@ -22,6 +23,11 @@ namespace Volo.Abp.FeatureManagement options.FileSets.AddEmbedded(); }); + Configure(options => + { + options.Converters.Add(); + }); + Configure(options => { options.JsonSerializerOptions.Converters.AddIfNotContains(new StringValueTypeJsonConverter()); diff --git a/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/NewtonsoftStringValueTypeJsonConverter.cs b/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/NewtonsoftStringValueTypeJsonConverter.cs new file mode 100644 index 0000000000..7422e460dc --- /dev/null +++ b/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/NewtonsoftStringValueTypeJsonConverter.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Validation.StringValues; + +namespace Volo.Abp.FeatureManagement.JsonConverters +{ + public class NewtonsoftStringValueTypeJsonConverter : JsonConverter, ITransientDependency + { + public override bool CanWrite => false; + + public override bool CanConvert(Type objectType) + { + return objectType == typeof(IStringValueType); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new NotImplementedException("This method should not be called to write (since CanWrite is false)."); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + if (reader.TokenType != JsonToken.StartObject) + { + return null; + } + + var jsonObject = JObject.Load(reader); + + var stringValue = CreateStringValueTypeByName(jsonObject, jsonObject["name"].ToString()); + foreach (var o in serializer.Deserialize>( + new JsonTextReader(new StringReader(jsonObject["properties"].ToString())))) + { + stringValue[o.Key] = o.Value; + } + + stringValue.Validator = CreateValueValidatorByName(jsonObject["validator"], jsonObject["validator"]["name"].ToString()); + foreach (var o in serializer.Deserialize>( + new JsonTextReader(new StringReader(jsonObject["validator"]["properties"].ToString())))) + { + stringValue.Validator[o.Key] = o.Value; + } + + return stringValue; + } + + protected virtual IStringValueType CreateStringValueTypeByName(JObject jObject, string name) + { + if (name == "SelectionStringValueType") + { + var selectionStringValueType = new SelectionStringValueType(); + if (jObject["itemSource"].HasValues) + { + selectionStringValueType.ItemSource = new StaticSelectionStringValueItemSource(jObject["itemSource"]["items"] + .Select(item => new LocalizableSelectionStringValueItem() + { + Value = item["value"].ToString(), + DisplayText = new LocalizableStringInfo(item["displayText"]["resourceName"].ToString(), item["displayText"]["name"].ToString()) + }).ToArray()); + } + + return selectionStringValueType; + } + + return name switch + { + "FreeTextStringValueType" => new FreeTextStringValueType(), + "ToggleStringValueType" => new ToggleStringValueType(), + _ => throw new ArgumentException($"{nameof(IStringValueType)} named {name} was not found!") + }; + } + + protected virtual IValueValidator CreateValueValidatorByName(JToken jObject, string name) + { + return name switch + { + "NULL" => new AlwaysValidValueValidator(), + "BOOLEAN" => new BooleanValueValidator(), + "NUMERIC" => new NumericValueValidator(), + "STRING" => new StringValueValidator(), + _ => throw new ArgumentException($"{nameof(IValueValidator)} named {name} was not found!") + }; + } + } +} diff --git a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/NewtonsoftStringValueJsonConverter_Tests.cs b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/NewtonsoftStringValueJsonConverter_Tests.cs new file mode 100644 index 0000000000..8fa6ea68b1 --- /dev/null +++ b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/NewtonsoftStringValueJsonConverter_Tests.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Json; + +namespace Volo.Abp.FeatureManagement +{ + public class NewtonsoftStringValueJsonConverter_Tests : StringValueJsonConverter_Tests + { + protected override void AfterAddApplication(IServiceCollection services) + { + services.PreConfigure(options => + { + options.UseHybridSerializer = true; + }); + } + } +} diff --git a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs index 0a96027317..cd42c1cb5b 100644 --- a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs +++ b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Text.Json; using Shouldly; using Volo.Abp.Json; using Volo.Abp.Validation.StringValues; @@ -8,7 +7,7 @@ using Xunit; namespace Volo.Abp.FeatureManagement { - public class StringValueJsonConverter_Tests : FeatureManagementApplicationTestBase + public abstract class StringValueJsonConverter_Tests : FeatureManagementApplicationTestBase { private readonly IJsonSerializer _jsonSerializer; diff --git a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/SystemTextJsonStringValueJsonConverter_Tests.cs b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/SystemTextJsonStringValueJsonConverter_Tests.cs new file mode 100644 index 0000000000..2cc8df1785 --- /dev/null +++ b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/SystemTextJsonStringValueJsonConverter_Tests.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Json; + +namespace Volo.Abp.FeatureManagement +{ + public class SystemTextJsonStringValueJsonConverter_Tests : StringValueJsonConverter_Tests + { + protected override void AfterAddApplication(IServiceCollection services) + { + services.PreConfigure(options => + { + options.UseHybridSerializer = false; + }); + } + } +}