mirror of https://github.com/abpframework/abp.git
7 changed files with 44 additions and 129 deletions
@ -1,56 +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 |
|||
{ |
|||
private JsonSerializerOptions _readJsonSerializerOptions; |
|||
|
|||
private JsonSerializerOptions _writeJsonSerializerOptions; |
|||
|
|||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
|||
{ |
|||
_readJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, x => x == this); |
|||
|
|||
var rootElement = JsonDocument.ParseValue(ref reader).RootElement; |
|||
if (rootElement.ValueKind == JsonValueKind.Object) |
|||
{ |
|||
var converterFactory = _readJsonSerializerOptions.Converters |
|||
.FirstOrDefault(x => x is AbpHasExtraPropertiesJsonConverterFactory) |
|||
.As<AbpHasExtraPropertiesJsonConverterFactory>(); |
|||
|
|||
T extensibleObject; |
|||
using (converterFactory != null ? converterFactory.Exclude(typeToConvert) : NullDisposable.Instance) |
|||
{ |
|||
extensibleObject = rootElement.Deserialize<T>(_readJsonSerializerOptions); |
|||
} |
|||
|
|||
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), _readJsonSerializerOptions); |
|||
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) |
|||
{ |
|||
_writeJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(options, x => |
|||
x == this || |
|||
x.GetType() == typeof(AbpHasExtraPropertiesJsonConverterFactory)); |
|||
|
|||
JsonSerializer.Serialize(writer, value, _writeJsonSerializerOptions); |
|||
} |
|||
} |
|||
} |
|||
@ -1,65 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Serialization; |
|||
using System.Threading; |
|||
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 AsyncLocal<List<Type>> _excludeTypes = new AsyncLocal<List<Type>>(); |
|||
|
|||
public IDisposable Exclude(params Type[] excludeTypes) |
|||
{ |
|||
var parent = _excludeTypes.Value; |
|||
_excludeTypes.Value = excludeTypes.ToList(); |
|||
return new DisposeAction(() => |
|||
{ |
|||
_excludeTypes.Value = parent; |
|||
}); |
|||
} |
|||
|
|||
public override bool CanConvert(Type typeToConvert) |
|||
{ |
|||
if (_excludeTypes.Value != null && _excludeTypes.Value.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)!; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue