mirror of https://github.com/abpframework/abp.git
committed by
GitHub
20 changed files with 249 additions and 185 deletions
@ -1,55 +0,0 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Serialization; |
|||
using Volo.Abp.Data; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.JsonConverters |
|||
{ |
|||
public class AbpHasExtraPropertiesJsonConverter<T> : JsonConverter<T> |
|||
where T : IHasExtraProperties |
|||
{ |
|||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
|||
{ |
|||
var newOptions = JsonSerializerOptionsHelper.Create(options, x => x == this); |
|||
|
|||
var converterFactory = newOptions.Converters.FirstOrDefault(x => x is AbpHasExtraPropertiesJsonConverterFactory).As<AbpHasExtraPropertiesJsonConverterFactory>(); |
|||
var newConverterFactory = new AbpHasExtraPropertiesJsonConverterFactory(); |
|||
if (converterFactory != null) |
|||
{ |
|||
newOptions.Converters.Remove(converterFactory); |
|||
newConverterFactory.AddExcludeTypes(converterFactory.GetExcludeTypes().ToArray()); |
|||
} |
|||
|
|||
newConverterFactory.AddExcludeTypes(typeToConvert); |
|||
newOptions.Converters.Add(newConverterFactory); |
|||
|
|||
var rootElement = JsonDocument.ParseValue(ref reader).RootElement; |
|||
if (rootElement.ValueKind == JsonValueKind.Object) |
|||
{ |
|||
var extensibleObject = rootElement.Deserialize<T>(newOptions); |
|||
|
|||
var extraPropertiesJsonProperty = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals(nameof(IHasExtraProperties.ExtraProperties), StringComparison.OrdinalIgnoreCase)); |
|||
|
|||
if (extraPropertiesJsonProperty.Value.ValueKind == JsonValueKind.Object) |
|||
{ |
|||
var extraPropertyDictionary = extraPropertiesJsonProperty.Value.Deserialize(typeof(ExtraPropertyDictionary), newOptions); |
|||
ObjectHelper.TrySetProperty(extensibleObject, x => x.ExtraProperties, () => extraPropertyDictionary); |
|||
} |
|||
|
|||
return extensibleObject; |
|||
} |
|||
|
|||
throw new JsonException("RootElement's ValueKind is not Object!"); |
|||
} |
|||
|
|||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) |
|||
{ |
|||
var newOptions = JsonSerializerOptionsHelper.Create(options, x => |
|||
x == this || |
|||
x.GetType() == typeof(AbpHasExtraPropertiesJsonConverterFactory)); |
|||
|
|||
JsonSerializer.Serialize(writer, value, newOptions); |
|||
} |
|||
} |
|||
} |
|||
@ -1,65 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Collections.Generic; |
|||
using System.Collections.Immutable; |
|||
using System.Reflection; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Serialization; |
|||
using Volo.Abp.Data; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.JsonConverters |
|||
{ |
|||
public class AbpHasExtraPropertiesJsonConverterFactory : JsonConverterFactory |
|||
{ |
|||
private static readonly ConcurrentDictionary<Type, bool> CachedTypes = new ConcurrentDictionary<Type, bool>(); |
|||
|
|||
private readonly List<Type> _excludeTypes = new List<Type>(); |
|||
|
|||
public virtual AbpHasExtraPropertiesJsonConverterFactory AddExcludeTypes(params Type[] excludeTypes) |
|||
{ |
|||
_excludeTypes.AddIfNotContains(excludeTypes); |
|||
return this; |
|||
} |
|||
|
|||
public virtual IReadOnlyList<Type> GetExcludeTypes() |
|||
{ |
|||
return _excludeTypes.ToImmutableList(); |
|||
} |
|||
|
|||
public override bool CanConvert(Type typeToConvert) |
|||
{ |
|||
if (_excludeTypes.Contains(typeToConvert)) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
//Only for private or protected ExtraProperties.
|
|||
if (typeof(IHasExtraProperties).IsAssignableFrom(typeToConvert)) |
|||
{ |
|||
return CachedTypes.GetOrAdd(typeToConvert, type => |
|||
{ |
|||
var property = type.GetProperty(nameof(IHasExtraProperties.ExtraProperties)); |
|||
if (property != null) |
|||
{ |
|||
var setMethod = property.GetSetMethod(true); |
|||
return setMethod != null && (setMethod.IsPrivate || setMethod.IsFamily); |
|||
} |
|||
|
|||
return false; |
|||
}); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
|||
{ |
|||
return (JsonConverter) Activator.CreateInstance( |
|||
typeof(AbpHasExtraPropertiesJsonConverter<>).MakeGenericType(typeToConvert), |
|||
BindingFlags.Instance | BindingFlags.Public, |
|||
binder: null, |
|||
null, |
|||
culture: null)!; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
using System.Text.Json; |
|||
using Shouldly; |
|||
using Volo.Abp.Json.SystemTextJson.JsonConverters; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Json |
|||
{ |
|||
public class AbpStringToBoolean_Tests |
|||
{ |
|||
[Fact] |
|||
public void Test_Read() |
|||
{ |
|||
var options = new JsonSerializerOptions() |
|||
{ |
|||
Converters = |
|||
{ |
|||
new AbpStringToBooleanConverter() |
|||
} |
|||
}; |
|||
|
|||
var testClass = JsonSerializer.Deserialize<TestClass>("{\"Enabled\": \"TrUe\"}", options); |
|||
testClass.ShouldNotBeNull(); |
|||
testClass.Enabled.ShouldBe(true); |
|||
|
|||
testClass = JsonSerializer.Deserialize<TestClass>("{\"Enabled\": true}", options); |
|||
testClass.ShouldNotBeNull(); |
|||
testClass.Enabled.ShouldBe(true); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Test_Write() |
|||
{ |
|||
var options = new JsonSerializerOptions() |
|||
{ |
|||
Converters = |
|||
{ |
|||
new AbpStringToBooleanConverter() |
|||
} |
|||
}; |
|||
|
|||
var testClassJson = JsonSerializer.Serialize(new TestClass() |
|||
{ |
|||
Enabled = true |
|||
}); |
|||
|
|||
testClassJson.ShouldBe("{\"Enabled\":true}"); |
|||
} |
|||
|
|||
class TestClass |
|||
{ |
|||
public bool Enabled { get; set; } |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,55 @@ |
|||
using System; |
|||
using System.Text.Json; |
|||
using Shouldly; |
|||
using Volo.Abp.Json.SystemTextJson.JsonConverters; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Json |
|||
{ |
|||
public class AbpStringToEnum_Tests |
|||
{ |
|||
[Fact] |
|||
public void Test_Read() |
|||
{ |
|||
var options = new JsonSerializerOptions() |
|||
{ |
|||
Converters = |
|||
{ |
|||
new AbpStringToEnumFactory() |
|||
} |
|||
}; |
|||
|
|||
var testClass = JsonSerializer.Deserialize<TestClass>("{\"Day\": \"Monday\"}", options); |
|||
testClass.ShouldNotBeNull(); |
|||
testClass.Day.ShouldBe(DayOfWeek.Monday); |
|||
|
|||
testClass = JsonSerializer.Deserialize<TestClass>("{\"Day\": 1}", options); |
|||
testClass.ShouldNotBeNull(); |
|||
testClass.Day.ShouldBe(DayOfWeek.Monday); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Test_Write() |
|||
{ |
|||
var options = new JsonSerializerOptions() |
|||
{ |
|||
Converters = |
|||
{ |
|||
new AbpStringToEnumFactory() |
|||
} |
|||
}; |
|||
|
|||
var testClassJson = JsonSerializer.Serialize(new TestClass() |
|||
{ |
|||
Day = DayOfWeek.Monday |
|||
}); |
|||
|
|||
testClassJson.ShouldBe("{\"Day\":1}"); |
|||
} |
|||
|
|||
class TestClass |
|||
{ |
|||
public DayOfWeek Day { get; set; } |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue