From bc39339fc63a6dca3817d96838b5ebe49d494375 Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Wed, 29 Jul 2020 11:44:55 +0800 Subject: [PATCH] Implement StringValueTypeJsonConverter. Resolve #4337 --- .../StringValueTypeJsonConverter.cs | 65 ++++++++++++++++++- .../StringValueJsonConverter_Tests.cs | 46 +++++++++++-- 2 files changed, 103 insertions(+), 8 deletions(-) diff --git a/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/StringValueTypeJsonConverter.cs b/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/StringValueTypeJsonConverter.cs index 5c14675b5c..0ee276cce1 100644 --- a/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/StringValueTypeJsonConverter.cs +++ b/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/StringValueTypeJsonConverter.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Volo.Abp.DependencyInjection; @@ -22,8 +25,66 @@ namespace Volo.Abp.FeatureManagement public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - //TODO: Deserialize! - return null; + 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/StringValueJsonConverter_Tests.cs b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs index e7017db80e..528e0de5e8 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,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Newtonsoft.Json; using Shouldly; using Volo.Abp.Json; @@ -16,7 +17,7 @@ namespace Volo.Abp.FeatureManagement _jsonSerializer = GetRequiredService(); } - [Fact(Skip = "StringValueTypeJsonConverter is not implemented yet!")] + [Fact] public void Should_Serialize_And_Deserialize() { var featureListDto = new FeatureListDto @@ -29,18 +30,51 @@ namespace Volo.Abp.FeatureManagement { Validator = new BooleanValueValidator() } + }, + new FeatureDto + { + ValueType = new SelectionStringValueType + { + ItemSource = new StaticSelectionStringValueItemSource( + new LocalizableSelectionStringValueItem + { + Value = "TestValue", + DisplayText = new LocalizableStringInfo("TestResourceName", "TestName") + }), + Validator = new AlwaysValidValueValidator() + } + }, + new FeatureDto + { + ValueType = new ToggleStringValueType + { + Validator = new NumericValueValidator + { + MaxValue = 1000, + MinValue = 10 + } + } } - - //TODO: Add more to test } }; - var serialized = _jsonSerializer.Serialize(featureListDto); - + var serialized = _jsonSerializer.Serialize(featureListDto, indented: true); var featureListDto2 = _jsonSerializer.Deserialize(serialized); featureListDto2.Features[0].ValueType.ShouldBeOfType(); featureListDto2.Features[0].ValueType.Validator.ShouldBeOfType(); + + featureListDto2.Features[1].ValueType.ShouldBeOfType(); + featureListDto2.Features[1].ValueType.Validator.ShouldBeOfType(); + featureListDto2.Features[1].ValueType.As().ItemSource.Items.ShouldBeOfType(); + featureListDto2.Features[1].ValueType.As().ItemSource.Items.ShouldContain(x => + x.Value == "TestValue" && x.DisplayText.ResourceName == "TestResourceName" && + x.DisplayText.Name == "TestName"); + + featureListDto2.Features[2].ValueType.ShouldBeOfType(); + featureListDto2.Features[2].ValueType.Validator.ShouldBeOfType(); + featureListDto2.Features[2].ValueType.Validator.As().MaxValue.ShouldBe(1000); + featureListDto2.Features[2].ValueType.Validator.As().MinValue.ShouldBe(10); } } }