Skip to content

Commit

Permalink
Merge pull request #10004 from abpframework/auto-merge/rel-4-4/544
Browse files Browse the repository at this point in the history
Merge branch dev with rel-4.4
  • Loading branch information
maliming committed Sep 10, 2021
2 parents bad98a4 + 95bc213 commit ebeedd3
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,11 @@ public override void ConfigureServices(ServiceConfigurationContext context)
options.FileSets.AddEmbedded<AbpFeatureManagementApplicationContractsModule>();
});

Configure<AbpNewtonsoftJsonSerializerOptions>(options =>
{
options.Converters.Add<NewtonsoftStringValueTypeJsonConverter>();
});

Configure<AbpSystemTextJsonSerializerOptions>(options =>
{
options.JsonSerializerOptions.Converters.AddIfNotContains(new StringValueTypeJsonConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Dictionary<string, object>>(
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<Dictionary<string, object>>(
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!")
};
}
}
}
Original file line number Diff line number Diff line change
@@ -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<AbpJsonOptions>(options =>
{
options.UseHybridSerializer = true;
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using Shouldly;
using Volo.Abp.Json;
using Volo.Abp.Validation.StringValues;
using Xunit;

namespace Volo.Abp.FeatureManagement
{
public class StringValueJsonConverter_Tests : FeatureManagementApplicationTestBase
public abstract class StringValueJsonConverter_Tests : FeatureManagementApplicationTestBase
{
private readonly IJsonSerializer _jsonSerializer;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<AbpJsonOptions>(options =>
{
options.UseHybridSerializer = false;
});
}
}
}

0 comments on commit ebeedd3

Please sign in to comment.