Browse Source

Refactor.

pull/5952/head
maliming 6 years ago
parent
commit
1e888c241f
  1. 1
      framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonSerializerOptionsSetup.cs
  2. 6
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs
  3. 17
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/JsonSerializerOptionsHelper.cs
  4. 24
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/LocalizableStringInfoJsonConverter.cs
  5. 36
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/SelectionStringValueItemJsonConverter.cs
  6. 12
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/SelectionStringValueItemSourceJsonConverter.cs
  7. 22
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/StringValueTypeJsonConverter.cs
  8. 19
      modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/ValueValidatorJsonConverter.cs
  9. 10
      modules/feature-management/src/Volo.Abp.FeatureManagement.HttpApi/Volo/Abp/FeatureManagement/AbpFeatureManagementHttpApiModule.cs
  10. 2
      modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs

1
framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonSerializerOptionsSetup.cs

@ -2,7 +2,6 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.Json.JsonConverters; using Volo.Abp.Json.JsonConverters;
using Volo.Abp.Json;
namespace Volo.Abp.Json namespace Volo.Abp.Json
{ {

6
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs

@ -1,7 +1,7 @@
using Volo.Abp.Application; using System.Collections.Generic;
using Volo.Abp.Application;
using Volo.Abp.FeatureManagement.JsonConverters; using Volo.Abp.FeatureManagement.JsonConverters;
using Volo.Abp.Json; using Volo.Abp.Json;
using Volo.Abp.Json;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem; using Volo.Abp.VirtualFileSystem;
@ -23,7 +23,7 @@ namespace Volo.Abp.FeatureManagement
Configure<AbpJsonSerializerOptions>(options => Configure<AbpJsonSerializerOptions>(options =>
{ {
options.JsonSerializerOptions.Converters.Add(new StringValueTypeJsonConverter()); options.JsonSerializerOptions.Converters.AddIfNotContains(new StringValueTypeJsonConverter());
}); });
} }
} }

17
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/JsonSerializerOptionsHelper.cs

@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Volo.Abp.FeatureManagement.JsonConverters
{
internal static class JsonSerializerOptionsHelper
{
public static JsonSerializerOptions Create(JsonSerializerOptions baseOptions, JsonConverter removeConverter, params JsonConverter[] addConverters)
{
var options = new JsonSerializerOptions(baseOptions);
options.Converters.RemoveAll(x => x == removeConverter);
options.Converters.AddIfNotContains(addConverters);
return options;
}
}
}

24
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/LocalizableStringInfoJsonConverter.cs

@ -1,24 +0,0 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.FeatureManagement.JsonConverters
{
public class LocalizableStringInfoJsonConverter : JsonConverter<LocalizableStringInfo>
{
public override LocalizableStringInfo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
return new LocalizableStringInfo(
rootElement.GetProperty("ResourceName").GetString(),
rootElement.GetProperty("Name").GetString()
);
}
public override void Write(Utf8JsonWriter writer, LocalizableStringInfo value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value);
}
}
}

36
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/SelectionStringValueItemJsonConverter.cs

@ -1,36 +0,0 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Volo.Abp.Validation.StringValues;
namespace Volo.Abp.FeatureManagement.JsonConverters
{
public class SelectionStringValueItemJsonConverter : JsonConverter<ISelectionStringValueItem>
{
public override ISelectionStringValueItem Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new LocalizableStringInfoJsonConverter());
return new LocalizableSelectionStringValueItem
{
Value = rootElement.GetProperty("Value").GetString(),
DisplayText = JsonSerializer.Deserialize<LocalizableStringInfo>(rootElement.GetProperty("DisplayText").GetRawText(), jsonSerializerOptions)
};
}
public override void Write(Utf8JsonWriter writer, ISelectionStringValueItem value, JsonSerializerOptions options)
{
if (value.GetType() == typeof(LocalizableStringInfo))
{
JsonSerializer.Serialize(writer, (LocalizableSelectionStringValueItem)value);
}
else
{
throw new JsonException("Unknown ISelectionStringValueItem type!");
}
}
}
}

12
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/SelectionStringValueItemSourceJsonConverter.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Volo.Abp.Validation.StringValues; using Volo.Abp.Validation.StringValues;
@ -10,12 +11,13 @@ namespace Volo.Abp.FeatureManagement.JsonConverters
{ {
public override ISelectionStringValueItemSource Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override ISelectionStringValueItemSource Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ {
var newOptions = JsonSerializerOptionsHelper.Create(options, this);
var rootElement = JsonDocument.ParseValue(ref reader).RootElement; var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
var jsonSerializerOptions = new JsonSerializerOptions(); var items = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("Items", StringComparison.InvariantCultureIgnoreCase)).Value.GetRawText();
jsonSerializerOptions.Converters.Add(new SelectionStringValueItemJsonConverter());
var selectionStringValueItem = var selectionStringValueItem =
JsonSerializer.Deserialize<List<LocalizableSelectionStringValueItem>>(rootElement.GetProperty("Items").GetRawText(), jsonSerializerOptions) ?? JsonSerializer.Deserialize<List<LocalizableSelectionStringValueItem>>(items, newOptions) ??
new List<LocalizableSelectionStringValueItem>(); new List<LocalizableSelectionStringValueItem>();
return new StaticSelectionStringValueItemSource(selectionStringValueItem.ToArray()); return new StaticSelectionStringValueItemSource(selectionStringValueItem.ToArray());
@ -23,9 +25,11 @@ namespace Volo.Abp.FeatureManagement.JsonConverters
public override void Write(Utf8JsonWriter writer, ISelectionStringValueItemSource value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, ISelectionStringValueItemSource value, JsonSerializerOptions options)
{ {
var newOptions = JsonSerializerOptionsHelper.Create(options, this);
if (value.GetType() == typeof(StaticSelectionStringValueItemSource)) if (value.GetType() == typeof(StaticSelectionStringValueItemSource))
{ {
JsonSerializer.Serialize(writer, (StaticSelectionStringValueItemSource)value); JsonSerializer.Serialize(writer, (StaticSelectionStringValueItemSource)value, newOptions);
} }
else else
{ {

22
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/StringValueTypeJsonConverter.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Volo.Abp.Validation.StringValues; using Volo.Abp.Validation.StringValues;
@ -10,33 +11,32 @@ namespace Volo.Abp.FeatureManagement.JsonConverters
public override IStringValueType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override IStringValueType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ {
var rootElement = JsonDocument.ParseValue(ref reader).RootElement; var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
var jsonSerializerOptions = new JsonSerializerOptions(); var name = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("Name", StringComparison.InvariantCultureIgnoreCase)).Value.GetString();
jsonSerializerOptions.Converters.Add(new ValueValidatorJsonConverter()); var newOptions = JsonSerializerOptionsHelper.Create(options, this, new ValueValidatorJsonConverter(), new SelectionStringValueItemSourceJsonConverter());
jsonSerializerOptions.Converters.Add(new SelectionStringValueItemSourceJsonConverter());
var name = rootElement.GetProperty("Name").GetString();
return name switch return name switch
{ {
"SelectionStringValueType" => JsonSerializer.Deserialize<SelectionStringValueType>(rootElement.GetRawText(), jsonSerializerOptions), "SelectionStringValueType" => JsonSerializer.Deserialize<SelectionStringValueType>(rootElement.GetRawText(), newOptions),
"FreeTextStringValueType" => JsonSerializer.Deserialize<FreeTextStringValueType>(rootElement.GetRawText(), jsonSerializerOptions), "FreeTextStringValueType" => JsonSerializer.Deserialize<FreeTextStringValueType>(rootElement.GetRawText(), newOptions),
"ToggleStringValueType" => JsonSerializer.Deserialize<ToggleStringValueType>(rootElement.GetRawText(), jsonSerializerOptions), "ToggleStringValueType" => JsonSerializer.Deserialize<ToggleStringValueType>(rootElement.GetRawText(), newOptions),
_ => throw new ArgumentException($"{nameof(IStringValueType)} named {name} was not found!") _ => throw new ArgumentException($"{nameof(IStringValueType)} named {name} was not found!")
}; };
} }
public override void Write(Utf8JsonWriter writer, IStringValueType value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, IStringValueType value, JsonSerializerOptions options)
{ {
var newOptions = JsonSerializerOptionsHelper.Create(options, this);
if (value.GetType() == typeof(FreeTextStringValueType)) if (value.GetType() == typeof(FreeTextStringValueType))
{ {
JsonSerializer.Serialize(writer, (FreeTextStringValueType)value); JsonSerializer.Serialize(writer, (FreeTextStringValueType)value, newOptions);
} }
else if (value.GetType() == typeof(SelectionStringValueType)) else if (value.GetType() == typeof(SelectionStringValueType))
{ {
JsonSerializer.Serialize(writer, (SelectionStringValueType)value); JsonSerializer.Serialize(writer, (SelectionStringValueType)value, newOptions);
} }
else if (value.GetType() == typeof(ToggleStringValueType)) else if (value.GetType() == typeof(ToggleStringValueType))
{ {
JsonSerializer.Serialize(writer, (ToggleStringValueType)value); JsonSerializer.Serialize(writer, (ToggleStringValueType)value, newOptions);
} }
else else
{ {

19
modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/ValueValidatorJsonConverter.cs

@ -13,11 +13,12 @@ namespace Volo.Abp.FeatureManagement.JsonConverters
public override IValueValidator Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override IValueValidator Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ {
var rootElement = JsonDocument.ParseValue(ref reader).RootElement; var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
var valueValidator = CreateValueValidatorByName(rootElement.GetProperty("Name").GetString()); var name = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("Name", StringComparison.InvariantCultureIgnoreCase)).Value.GetString();
var valueValidator = CreateValueValidatorByName(name);
var deserializeOptions = new JsonSerializerOptions(); var propertiesRawText = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("Properties", StringComparison.InvariantCultureIgnoreCase)).Value.GetRawText();
deserializeOptions.Converters.Add(new ObjectToInferredTypesConverter()); var newOptions = JsonSerializerOptionsHelper.Create(options, this, new ObjectToInferredTypesConverter());
var properties = JsonSerializer.Deserialize<IDictionary<string, object>>(rootElement.GetProperty("Properties").GetRawText(), deserializeOptions); var properties = JsonSerializer.Deserialize<IDictionary<string, object>>(propertiesRawText, newOptions);
if (properties != null && properties.Any()) if (properties != null && properties.Any())
{ {
foreach (var property in properties) foreach (var property in properties)
@ -31,21 +32,23 @@ namespace Volo.Abp.FeatureManagement.JsonConverters
public override void Write(Utf8JsonWriter writer, IValueValidator value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, IValueValidator value, JsonSerializerOptions options)
{ {
var newOptions = JsonSerializerOptionsHelper.Create(options, this);
if (value.GetType() == typeof(AlwaysValidValueValidator)) if (value.GetType() == typeof(AlwaysValidValueValidator))
{ {
JsonSerializer.Serialize(writer, (AlwaysValidValueValidator)value); JsonSerializer.Serialize(writer, (AlwaysValidValueValidator)value, newOptions);
} }
else if (value.GetType() == typeof(BooleanValueValidator)) else if (value.GetType() == typeof(BooleanValueValidator))
{ {
JsonSerializer.Serialize(writer, (BooleanValueValidator)value); JsonSerializer.Serialize(writer, (BooleanValueValidator)value, newOptions);
} }
else if (value.GetType() == typeof(NumericValueValidator)) else if (value.GetType() == typeof(NumericValueValidator))
{ {
JsonSerializer.Serialize(writer, (NumericValueValidator)value); JsonSerializer.Serialize(writer, (NumericValueValidator)value, newOptions);
} }
else if (value.GetType() == typeof(StringValueValidator)) else if (value.GetType() == typeof(StringValueValidator))
{ {
JsonSerializer.Serialize(writer, (StringValueValidator)value); JsonSerializer.Serialize(writer, (StringValueValidator)value, newOptions);
} }
else else
{ {

10
modules/feature-management/src/Volo.Abp.FeatureManagement.HttpApi/Volo/Abp/FeatureManagement/AbpFeatureManagementHttpApiModule.cs

@ -1,9 +1,12 @@
using Localization.Resources.AbpUi; using System.Collections.Generic;
using Localization.Resources.AbpUi;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.FeatureManagement.Localization; using Volo.Abp.FeatureManagement.Localization;
using Volo.Abp.Localization; using Volo.Abp.Localization;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.FeatureManagement.JsonConverters;
namespace Volo.Abp.FeatureManagement namespace Volo.Abp.FeatureManagement
{ {
@ -28,6 +31,11 @@ namespace Volo.Abp.FeatureManagement
.Get<AbpFeatureManagementResource>() .Get<AbpFeatureManagementResource>()
.AddBaseTypes(typeof(AbpUiResource)); .AddBaseTypes(typeof(AbpUiResource));
}); });
Configure<JsonOptions>(options =>
{
options.JsonSerializerOptions.Converters.AddIfNotContains(new StringValueTypeJsonConverter());
});
} }
} }
} }

2
modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs

@ -66,7 +66,7 @@ namespace Volo.Abp.FeatureManagement
} }
}; };
var serialized = _jsonSerializer.Serialize(featureListDto); var serialized = _jsonSerializer.Serialize(featureListDto, indented: true);
var featureListDto2 = _jsonSerializer.Deserialize<GetFeatureListResultDto>(serialized); var featureListDto2 = _jsonSerializer.Deserialize<GetFeatureListResultDto>(serialized);

Loading…
Cancel
Save