-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10004 from abpframework/auto-merge/rel-4-4/544
Merge branch dev with rel-4.4
- Loading branch information
Showing
5 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...racts/Volo/Abp/FeatureManagement/JsonConverters/NewtonsoftStringValueTypeJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
}; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
....Application.Tests/Volo/Abp/FeatureManagement/NewtonsoftStringValueJsonConverter_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...Management.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...lication.Tests/Volo/Abp/FeatureManagement/SystemTextJsonStringValueJsonConverter_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} | ||
} |