diff --git a/Src/Newtonsoft.Json/Serialization/CachedAttributeGetter.cs b/Src/Newtonsoft.Json/Serialization/CachedAttributeGetter.cs index 97ba9043..c216ce53 100644 --- a/Src/Newtonsoft.Json/Serialization/CachedAttributeGetter.cs +++ b/Src/Newtonsoft.Json/Serialization/CachedAttributeGetter.cs @@ -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 where T : Attribute { - private static readonly ThreadSafeStore TypeAttributeCache = new ThreadSafeStore(JsonTypeReflector.GetAttribute); + private static readonly ThreadSafeStore TypeAttributeCache = new ThreadSafeStore(JsonTypeReflector.GetAttribute); - public static T GetAttribute(ICustomAttributeProvider type) + public static T GetAttribute(object type) { return TypeAttributeCache.Get(type); } diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 78dfeae6..b581f5ba 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -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(member.GetCustomAttributeProvider()) != null) + if (JsonTypeReflector.GetAttribute(member) != null) serializableMembers.Add(member); #if !NET20 - else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute(member.GetCustomAttributeProvider()) != null) + else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute(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 /// The contract's default . protected virtual JsonConverter ResolveContractConverter(Type objectType) { - return JsonTypeReflector.GetJsonConverter(objectType.GetCustomAttributeProvider(), objectType); + return JsonTypeReflector.GetJsonConverter(objectType, objectType); } private Func 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) diff --git a/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs b/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs index 9cb87c79..fdbe6876 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs @@ -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 JsonConverterTypeCache = new ThreadSafeStore(GetJsonConverterTypeFromAttribute); + private static readonly ThreadSafeStore JsonConverterTypeCache = new ThreadSafeStore(GetJsonConverterTypeFromAttribute); #if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE) private static readonly ThreadSafeStore AssociatedMetadataTypesCache = new ThreadSafeStore(GetAssociateMetadataTypeFromAttribute); @@ -71,9 +71,9 @@ namespace Newtonsoft.Json.Serialization private static Type _cachedMetadataTypeAttributeType; #endif #if SILVERLIGHT - private static readonly ThreadSafeStore TypeConverterTypeCache = new ThreadSafeStore(GetTypeConverterTypeFromAttribute); + private static readonly ThreadSafeStore TypeConverterTypeCache = new ThreadSafeStore(GetTypeConverterTypeFromAttribute); - private static Type GetTypeConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider) + private static Type GetTypeConverterTypeFromAttribute(object attributeProvider) { TypeConverterAttribute converterAttribute = GetAttribute(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.GetAttribute(type.GetCustomAttributeProvider()); + return CachedAttributeGetter.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.GetAttribute(type.GetCustomAttributeProvider()); + return CachedAttributeGetter.GetAttribute(type); } #endif @@ -119,7 +119,7 @@ namespace Newtonsoft.Json.Serialization public static DataContractAttribute GetDataContractAttribute(Type type) { // DataContractAttribute does not have inheritance - return CachedAttributeGetter.GetAttribute(type.GetCustomAttributeProvider()); + return CachedAttributeGetter.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.GetAttribute(memberInfo.GetCustomAttributeProvider()); + return CachedAttributeGetter.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.GetAttribute(propertyInfo.GetCustomAttributeProvider()); + DataMemberAttribute result = CachedAttributeGetter.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.GetAttribute(baseProperty.GetCustomAttributeProvider()); + result = CachedAttributeGetter.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(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(type.GetCustomAttributeProvider(), true); + attribute = ReflectionUtils.GetAttribute(type, true); if (attribute != null) return attribute; foreach (Type typeInterface in type.GetInterfaces()) { - attribute = ReflectionUtils.GetAttribute(typeInterface.GetCustomAttributeProvider(), true); + attribute = ReflectionUtils.GetAttribute(typeInterface, true); if (attribute != null) return attribute; } @@ -317,7 +310,7 @@ namespace Newtonsoft.Json.Serialization } #endif - attribute = ReflectionUtils.GetAttribute(memberInfo.GetCustomAttributeProvider(), true); + attribute = ReflectionUtils.GetAttribute(memberInfo, true); if (attribute != null) return attribute; @@ -329,7 +322,7 @@ namespace Newtonsoft.Json.Serialization if (interfaceTypeMemberInfo != null) { - attribute = ReflectionUtils.GetAttribute(interfaceTypeMemberInfo.GetCustomAttributeProvider(), true); + attribute = ReflectionUtils.GetAttribute(interfaceTypeMemberInfo, true); if (attribute != null) return attribute; } @@ -339,15 +332,8 @@ namespace Newtonsoft.Json.Serialization return null; } - public static T GetAttribute(ICustomAttributeProvider attributeProvider) where T : Attribute + public static T GetAttribute(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(type); @@ -356,12 +342,9 @@ namespace Newtonsoft.Json.Serialization if (memberInfo != null) return GetAttribute(memberInfo); - return ReflectionUtils.GetAttribute(attributeProvider, true); + return ReflectionUtils.GetAttribute(provider, true); } - private static bool? _dynamicCodeGeneration; - private static bool? _fullyTrusted; - #if DEBUG internal static void SetFullyTrusted(bool fullyTrusted) { diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 89bc330c..e1f75746 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -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(ICustomAttributeProvider attributeProvider) where T : Attribute + public static T GetAttribute(object attributeProvider) where T : Attribute { return GetAttribute(attributeProvider, true); } - public static T GetAttribute(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute + public static T GetAttribute(object attributeProvider, bool inherit) where T : Attribute { T[] attributes = GetAttributes(attributeProvider, inherit); @@ -699,7 +672,7 @@ namespace Newtonsoft.Json.Utilities } #if !(NETFX_CORE || PORTABLE) - public static T[] GetAttributes(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute + public static T[] GetAttributes(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(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute + public static T[] GetAttributes(object provider, bool inherit) where T : Attribute { - object provider = attributeProvider.UnderlyingObject; - if (provider is Type) return ((Type)provider).GetTypeInfo().GetCustomAttributes(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 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))