mirror of https://github.com/abpframework/abp.git
5 changed files with 129 additions and 2 deletions
@ -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!") |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
@ -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; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -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; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue