mirror of https://github.com/abpframework/abp.git
Browse Source
Rename AbpExtraPropertyDictionaryJsonConverter to AbpHasExtraPropertiesJsonConverter.pull/6176/head
committed by
GitHub
5 changed files with 53 additions and 37 deletions
@ -1,26 +0,0 @@ |
|||
using System; |
|||
using System.Reflection; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Serialization; |
|||
using Volo.Abp.ObjectExtending; |
|||
|
|||
namespace Volo.Abp.Json.SystemTextJson.JsonConverters |
|||
{ |
|||
public class AbpExtraPropertyDictionaryJsonConverterFactory : JsonConverterFactory |
|||
{ |
|||
public override bool CanConvert(Type typeToConvert) |
|||
{ |
|||
return typeToConvert == typeof(ExtensibleObject) || typeToConvert.IsSubclassOf(typeof(ExtensibleObject)); |
|||
} |
|||
|
|||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
|||
{ |
|||
return (JsonConverter) Activator.CreateInstance( |
|||
typeof(AbpExtraPropertyDictionaryJsonConverter<>).MakeGenericType(typeToConvert), |
|||
BindingFlags.Instance | BindingFlags.Public, |
|||
binder: null, |
|||
null, |
|||
culture: null)!; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
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>(); |
|||
|
|||
public override bool CanConvert(Type typeToConvert) |
|||
{ |
|||
//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