-Refactored away dependency on ICustomAttributeProvider
This commit is contained in:
@@ -26,17 +26,14 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
#if NETFX_CORE || PORTABLE
|
||||
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
internal static class CachedAttributeGetter<T> where T : Attribute
|
||||
{
|
||||
private static readonly ThreadSafeStore<ICustomAttributeProvider, T> TypeAttributeCache = new ThreadSafeStore<ICustomAttributeProvider, T>(JsonTypeReflector.GetAttribute<T>);
|
||||
private static readonly ThreadSafeStore<object, T> TypeAttributeCache = new ThreadSafeStore<object, T>(JsonTypeReflector.GetAttribute<T>);
|
||||
|
||||
public static T GetAttribute(ICustomAttributeProvider type)
|
||||
public static T GetAttribute(object type)
|
||||
{
|
||||
return TypeAttributeCache.Get(type);
|
||||
}
|
||||
|
||||
@@ -44,9 +44,6 @@ using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
#if NETFX_CORE || PORTABLE
|
||||
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
|
||||
#endif
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
@@ -295,10 +292,10 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
// add members that are explicitly marked with JsonProperty/DataMember attribute
|
||||
// or are a field if serializing just fields
|
||||
if (JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(member.GetCustomAttributeProvider()) != null)
|
||||
if (JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(member) != null)
|
||||
serializableMembers.Add(member);
|
||||
#if !NET20
|
||||
else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute<DataMemberAttribute>(member.GetCustomAttributeProvider()) != null)
|
||||
else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute<DataMemberAttribute>(member) != null)
|
||||
serializableMembers.Add(member);
|
||||
#endif
|
||||
else if (memberSerialization == MemberSerialization.Fields && member.MemberType() == MemberTypes.Field)
|
||||
@@ -466,7 +463,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
property.PropertyType = parameterInfo.ParameterType;
|
||||
|
||||
bool allowNonPublicAccess;
|
||||
SetPropertySettingsFromAttributes(property, parameterInfo.GetCustomAttributeProvider(), parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess);
|
||||
SetPropertySettingsFromAttributes(property, parameterInfo, parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess);
|
||||
|
||||
property.Readable = false;
|
||||
property.Writable = true;
|
||||
@@ -500,7 +497,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
/// <returns>The contract's default <see cref="JsonConverter" />.</returns>
|
||||
protected virtual JsonConverter ResolveContractConverter(Type objectType)
|
||||
{
|
||||
return JsonTypeReflector.GetJsonConverter(objectType.GetCustomAttributeProvider(), objectType);
|
||||
return JsonTypeReflector.GetJsonConverter(objectType, objectType);
|
||||
}
|
||||
|
||||
private Func<object> GetDefaultCreator(Type createdType)
|
||||
@@ -950,7 +947,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
property.ValueProvider = CreateMemberValueProvider(member);
|
||||
|
||||
bool allowNonPublicAccess;
|
||||
SetPropertySettingsFromAttributes(property, member.GetCustomAttributeProvider(), member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
|
||||
SetPropertySettingsFromAttributes(property, member, member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
|
||||
|
||||
if (memberSerialization != MemberSerialization.Fields)
|
||||
{
|
||||
@@ -970,17 +967,12 @@ namespace Newtonsoft.Json.Serialization
|
||||
return property;
|
||||
}
|
||||
|
||||
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
|
||||
private void SetPropertySettingsFromAttributes(JsonProperty property, object attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
|
||||
{
|
||||
#if !NET20
|
||||
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);
|
||||
|
||||
MemberInfo memberInfo = null;
|
||||
#if !(NETFX_CORE || PORTABLE)
|
||||
memberInfo = attributeProvider as MemberInfo;
|
||||
#else
|
||||
memberInfo = attributeProvider.UnderlyingObject as MemberInfo;
|
||||
#endif
|
||||
MemberInfo memberInfo = attributeProvider as MemberInfo;
|
||||
|
||||
DataMemberAttribute dataMemberAttribute;
|
||||
if (dataContractAttribute != null && memberInfo != null)
|
||||
|
||||
@@ -32,9 +32,6 @@ using System.Security;
|
||||
using System.Security.Permissions;
|
||||
#endif
|
||||
using Newtonsoft.Json.Utilities;
|
||||
#if NETFX_CORE || PORTABLE
|
||||
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
|
||||
#endif
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
@@ -53,6 +50,9 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
internal static class JsonTypeReflector
|
||||
{
|
||||
private static bool? _dynamicCodeGeneration;
|
||||
private static bool? _fullyTrusted;
|
||||
|
||||
public const string IdPropertyName = "$id";
|
||||
public const string RefPropertyName = "$ref";
|
||||
public const string TypePropertyName = "$type";
|
||||
@@ -62,7 +62,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
public const string ShouldSerializePrefix = "ShouldSerialize";
|
||||
public const string SpecifiedPostfix = "Specified";
|
||||
|
||||
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> JsonConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetJsonConverterTypeFromAttribute);
|
||||
private static readonly ThreadSafeStore<object, Type> JsonConverterTypeCache = new ThreadSafeStore<object, Type>(GetJsonConverterTypeFromAttribute);
|
||||
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
|
||||
private static readonly ThreadSafeStore<Type, Type> AssociatedMetadataTypesCache = new ThreadSafeStore<Type, Type>(GetAssociateMetadataTypeFromAttribute);
|
||||
|
||||
@@ -71,9 +71,9 @@ namespace Newtonsoft.Json.Serialization
|
||||
private static Type _cachedMetadataTypeAttributeType;
|
||||
#endif
|
||||
#if SILVERLIGHT
|
||||
private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> TypeConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetTypeConverterTypeFromAttribute);
|
||||
private static readonly ThreadSafeStore<object, Type> TypeConverterTypeCache = new ThreadSafeStore<object, Type>(GetTypeConverterTypeFromAttribute);
|
||||
|
||||
private static Type GetTypeConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
|
||||
private static Type GetTypeConverterTypeFromAttribute(object attributeProvider)
|
||||
{
|
||||
TypeConverterAttribute converterAttribute = GetAttribute<TypeConverterAttribute>(attributeProvider);
|
||||
if (converterAttribute == null)
|
||||
@@ -82,7 +82,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
return Type.GetType(converterAttribute.ConverterTypeName);
|
||||
}
|
||||
|
||||
private static Type GetTypeConverterType(ICustomAttributeProvider attributeProvider)
|
||||
private static Type GetTypeConverterType(object attributeProvider)
|
||||
{
|
||||
return TypeConverterTypeCache.Get(attributeProvider);
|
||||
}
|
||||
@@ -90,7 +90,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
public static JsonContainerAttribute GetJsonContainerAttribute(Type type)
|
||||
{
|
||||
return CachedAttributeGetter<JsonContainerAttribute>.GetAttribute(type.GetCustomAttributeProvider());
|
||||
return CachedAttributeGetter<JsonContainerAttribute>.GetAttribute(type);
|
||||
}
|
||||
|
||||
public static JsonObjectAttribute GetJsonObjectAttribute(Type type)
|
||||
@@ -111,7 +111,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
public static SerializableAttribute GetSerializableAttribute(Type type)
|
||||
{
|
||||
return CachedAttributeGetter<SerializableAttribute>.GetAttribute(type.GetCustomAttributeProvider());
|
||||
return CachedAttributeGetter<SerializableAttribute>.GetAttribute(type);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -119,7 +119,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
public static DataContractAttribute GetDataContractAttribute(Type type)
|
||||
{
|
||||
// DataContractAttribute does not have inheritance
|
||||
return CachedAttributeGetter<DataContractAttribute>.GetAttribute(type.GetCustomAttributeProvider());
|
||||
return CachedAttributeGetter<DataContractAttribute>.GetAttribute(type);
|
||||
}
|
||||
|
||||
public static DataMemberAttribute GetDataMemberAttribute(MemberInfo memberInfo)
|
||||
@@ -128,11 +128,11 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
// can't override a field
|
||||
if (memberInfo.MemberType() == MemberTypes.Field)
|
||||
return CachedAttributeGetter<DataMemberAttribute>.GetAttribute(memberInfo.GetCustomAttributeProvider());
|
||||
return CachedAttributeGetter<DataMemberAttribute>.GetAttribute(memberInfo);
|
||||
|
||||
// search property and then search base properties if nothing is returned and the property is virtual
|
||||
PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
|
||||
DataMemberAttribute result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(propertyInfo.GetCustomAttributeProvider());
|
||||
DataMemberAttribute result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(propertyInfo);
|
||||
if (result == null)
|
||||
{
|
||||
if (propertyInfo.IsVirtual())
|
||||
@@ -143,7 +143,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
PropertyInfo baseProperty = (PropertyInfo)ReflectionUtils.GetMemberInfoFromType(currentType, propertyInfo);
|
||||
if (baseProperty != null && baseProperty.IsVirtual())
|
||||
result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(baseProperty.GetCustomAttributeProvider());
|
||||
result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(baseProperty);
|
||||
|
||||
currentType = currentType.BaseType();
|
||||
}
|
||||
@@ -179,12 +179,12 @@ namespace Newtonsoft.Json.Serialization
|
||||
return MemberSerialization.OptOut;
|
||||
}
|
||||
|
||||
private static Type GetJsonConverterType(ICustomAttributeProvider attributeProvider)
|
||||
private static Type GetJsonConverterType(object attributeProvider)
|
||||
{
|
||||
return JsonConverterTypeCache.Get(attributeProvider);
|
||||
}
|
||||
|
||||
private static Type GetJsonConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
|
||||
private static Type GetJsonConverterTypeFromAttribute(object attributeProvider)
|
||||
{
|
||||
JsonConverterAttribute converterAttribute = GetAttribute<JsonConverterAttribute>(attributeProvider);
|
||||
return (converterAttribute != null)
|
||||
@@ -192,15 +192,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
: null;
|
||||
}
|
||||
|
||||
public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
|
||||
public static JsonConverter GetJsonConverter(object attributeProvider, Type targetConvertedType)
|
||||
{
|
||||
object provider = null;
|
||||
#if !(NETFX_CORE || PORTABLE)
|
||||
provider = attributeProvider as MemberInfo;
|
||||
#else
|
||||
provider = attributeProvider.UnderlyingObject;
|
||||
#endif
|
||||
|
||||
Type converterType = GetJsonConverterType(attributeProvider);
|
||||
|
||||
if (converterType != null)
|
||||
@@ -284,13 +277,13 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
#endif
|
||||
|
||||
attribute = ReflectionUtils.GetAttribute<T>(type.GetCustomAttributeProvider(), true);
|
||||
attribute = ReflectionUtils.GetAttribute<T>(type, true);
|
||||
if (attribute != null)
|
||||
return attribute;
|
||||
|
||||
foreach (Type typeInterface in type.GetInterfaces())
|
||||
{
|
||||
attribute = ReflectionUtils.GetAttribute<T>(typeInterface.GetCustomAttributeProvider(), true);
|
||||
attribute = ReflectionUtils.GetAttribute<T>(typeInterface, true);
|
||||
if (attribute != null)
|
||||
return attribute;
|
||||
}
|
||||
@@ -317,7 +310,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
#endif
|
||||
|
||||
attribute = ReflectionUtils.GetAttribute<T>(memberInfo.GetCustomAttributeProvider(), true);
|
||||
attribute = ReflectionUtils.GetAttribute<T>(memberInfo, true);
|
||||
if (attribute != null)
|
||||
return attribute;
|
||||
|
||||
@@ -329,7 +322,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
if (interfaceTypeMemberInfo != null)
|
||||
{
|
||||
attribute = ReflectionUtils.GetAttribute<T>(interfaceTypeMemberInfo.GetCustomAttributeProvider(), true);
|
||||
attribute = ReflectionUtils.GetAttribute<T>(interfaceTypeMemberInfo, true);
|
||||
if (attribute != null)
|
||||
return attribute;
|
||||
}
|
||||
@@ -339,15 +332,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
return null;
|
||||
}
|
||||
|
||||
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
|
||||
public static T GetAttribute<T>(object provider) where T : Attribute
|
||||
{
|
||||
object provider = null;
|
||||
#if !(NETFX_CORE || PORTABLE)
|
||||
provider = attributeProvider;
|
||||
#else
|
||||
provider = attributeProvider.UnderlyingObject;
|
||||
#endif
|
||||
|
||||
Type type = provider as Type;
|
||||
if (type != null)
|
||||
return GetAttribute<T>(type);
|
||||
@@ -356,12 +342,9 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (memberInfo != null)
|
||||
return GetAttribute<T>(memberInfo);
|
||||
|
||||
return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
|
||||
return ReflectionUtils.GetAttribute<T>(provider, true);
|
||||
}
|
||||
|
||||
private static bool? _dynamicCodeGeneration;
|
||||
private static bool? _fullyTrusted;
|
||||
|
||||
#if DEBUG
|
||||
internal static void SetFullyTrusted(bool fullyTrusted)
|
||||
{
|
||||
|
||||
@@ -34,9 +34,6 @@ using System.Globalization;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using System.Text;
|
||||
#if NETFX_CORE || PORTABLE
|
||||
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
|
||||
#endif
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
@@ -56,21 +53,6 @@ namespace Newtonsoft.Json.Utilities
|
||||
Other
|
||||
}
|
||||
|
||||
internal class CustomAttributeProvider
|
||||
{
|
||||
private readonly object _underlyingObject;
|
||||
|
||||
public CustomAttributeProvider(object o)
|
||||
{
|
||||
_underlyingObject = o;
|
||||
}
|
||||
|
||||
public object UnderlyingObject
|
||||
{
|
||||
get { return _underlyingObject; }
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
internal enum BindingFlags
|
||||
{
|
||||
@@ -110,15 +92,6 @@ namespace Newtonsoft.Json.Utilities
|
||||
#endif
|
||||
}
|
||||
|
||||
public static ICustomAttributeProvider GetCustomAttributeProvider(this object o)
|
||||
{
|
||||
#if !(NETFX_CORE || PORTABLE)
|
||||
return (ICustomAttributeProvider)o;
|
||||
#else
|
||||
return new ICustomAttributeProvider(o);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool IsVirtual(this PropertyInfo propertyInfo)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(propertyInfo, "propertyInfo");
|
||||
@@ -582,7 +555,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
return true;
|
||||
return false;
|
||||
case MemberTypes.Property:
|
||||
PropertyInfo propertyInfo = (PropertyInfo) member;
|
||||
PropertyInfo propertyInfo = (PropertyInfo)member;
|
||||
|
||||
if (!propertyInfo.CanRead)
|
||||
return false;
|
||||
@@ -686,12 +659,12 @@ namespace Newtonsoft.Json.Utilities
|
||||
return true;
|
||||
}
|
||||
|
||||
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
|
||||
public static T GetAttribute<T>(object attributeProvider) where T : Attribute
|
||||
{
|
||||
return GetAttribute<T>(attributeProvider, true);
|
||||
}
|
||||
|
||||
public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
|
||||
public static T GetAttribute<T>(object attributeProvider, bool inherit) where T : Attribute
|
||||
{
|
||||
T[] attributes = GetAttributes<T>(attributeProvider, inherit);
|
||||
|
||||
@@ -699,7 +672,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
|
||||
#if !(NETFX_CORE || PORTABLE)
|
||||
public static T[] GetAttributes<T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
|
||||
public static T[] GetAttributes<T>(object attributeProvider, bool inherit) where T : Attribute
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(attributeProvider, "attributeProvider");
|
||||
|
||||
@@ -723,13 +696,11 @@ namespace Newtonsoft.Json.Utilities
|
||||
if (provider is ParameterInfo)
|
||||
return (T[])Attribute.GetCustomAttributes((ParameterInfo)provider, typeof(T), inherit);
|
||||
|
||||
return (T[])attributeProvider.GetCustomAttributes(typeof(T), inherit);
|
||||
return (T[])((ICustomAttributeProvider)attributeProvider).GetCustomAttributes(typeof(T), inherit);
|
||||
}
|
||||
#else
|
||||
public static T[] GetAttributes<T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
|
||||
public static T[] GetAttributes<T>(object provider, bool inherit) where T : Attribute
|
||||
{
|
||||
object provider = attributeProvider.UnderlyingObject;
|
||||
|
||||
if (provider is Type)
|
||||
return ((Type)provider).GetTypeInfo().GetCustomAttributes<T>(inherit).ToArray();
|
||||
|
||||
@@ -760,7 +731,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
|
||||
public static object CreateGeneric(Type genericTypeDefinition, Type innerType, params object[] args)
|
||||
{
|
||||
return CreateGeneric(genericTypeDefinition, new [] { innerType }, args);
|
||||
return CreateGeneric(genericTypeDefinition, new[] { innerType }, args);
|
||||
}
|
||||
|
||||
public static object CreateGeneric(Type genericTypeDefinition, IList<Type> innerTypes, params object[] args)
|
||||
@@ -779,12 +750,12 @@ namespace Newtonsoft.Json.Utilities
|
||||
return instanceCreator(specificType, args);
|
||||
}
|
||||
|
||||
public static object CreateInstance(Type type, params object[] args)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(type, "type");
|
||||
public static object CreateInstance(Type type, params object[] args)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(type, "type");
|
||||
|
||||
return Activator.CreateInstance(type, args);
|
||||
}
|
||||
return Activator.CreateInstance(type, args);
|
||||
}
|
||||
|
||||
public static void SplitFullyQualifiedTypeName(string fullyQualifiedTypeName, out string typeName, out string assemblyName)
|
||||
{
|
||||
@@ -836,7 +807,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
switch (memberInfo.MemberType())
|
||||
{
|
||||
case MemberTypes.Property:
|
||||
PropertyInfo propertyInfo = (PropertyInfo) memberInfo;
|
||||
PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
|
||||
|
||||
Type[] types = propertyInfo.GetIndexParameters().Select(p => p.ParameterType).ToArray();
|
||||
|
||||
@@ -947,7 +918,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
bool isMethodOverriden = currentType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
|
||||
.Any(info =>
|
||||
info.Name == method &&
|
||||
// check that the method overrides the original on DynamicObjectProxy
|
||||
// check that the method overrides the original on DynamicObjectProxy
|
||||
info.DeclaringType != methodDeclaringType
|
||||
&& info.GetBaseDefinition().DeclaringType == methodDeclaringType
|
||||
);
|
||||
@@ -993,6 +964,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
case PrimitiveTypeCode.DateTimeOffset:
|
||||
return new DateTimeOffset();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
if (IsNullable(type))
|
||||
|
||||
Reference in New Issue
Block a user