public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType) { object provider = null; #if !NETFX_CORE provider = attributeProvider as MemberInfo; #else provider = attributeProvider.UnderlyingObject; #endif Type converterType = GetJsonConverterType(attributeProvider); if (converterType != null) { JsonConverter memberConverter = JsonConverterAttribute.CreateJsonConverterInstance(converterType); if (!memberConverter.CanConvert(targetConvertedType)) { throw new JsonSerializationException("JsonConverter {0} on {1} is not compatible with member type {2}.".FormatWith(CultureInfo.InvariantCulture, memberConverter.GetType().Name, provider, targetConvertedType.Name)); } return(memberConverter); } return(null); }
private static Type GetJsonConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider) { JsonConverterAttribute converterAttribute = GetAttribute <JsonConverterAttribute>(attributeProvider); return((converterAttribute != null) ? converterAttribute.ConverterType : null); }
private static Type GetTypeConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider) { TypeConverterAttribute converterAttribute = GetAttribute <TypeConverterAttribute>(attributeProvider); if (converterAttribute == null) { return(null); } return(Type.GetType(converterAttribute.ConverterTypeName)); }
public static JsonConverter GetJsonConverter(ICustomAttributeProvider 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) { JsonConverter memberConverter = JsonConverterAttribute.CreateJsonConverterInstance(converterType); return(memberConverter); } return(null); }
public static T GetAttribute <T>(ICustomAttributeProvider attributeProvider) 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)); } MemberInfo memberInfo = provider as MemberInfo; if (memberInfo != null) { return(GetAttribute <T>(memberInfo)); } return(ReflectionUtils.GetAttribute <T>(attributeProvider, true)); }
private static Type GetTypeConverterType(ICustomAttributeProvider attributeProvider) { return(TypeConverterTypeCache.Get(attributeProvider)); }
public static T GetAttribute(ICustomAttributeProvider type) { return(TypeAttributeCache.Get(type)); }
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute) { hasExplicitAttribute = false; #if !PocketPC && !NET20 DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType); MemberInfo memberInfo = null; #if !(NETFX_CORE || PORTABLE) memberInfo = attributeProvider as MemberInfo; #else memberInfo = attributeProvider.UnderlyingObject as MemberInfo; #endif DataMemberAttribute dataMemberAttribute; if (dataContractAttribute != null && memberInfo != null) { dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)memberInfo); } else { dataMemberAttribute = null; } #endif JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider); if (propertyAttribute != null) { hasExplicitAttribute = true; } string mappedName; if (propertyAttribute != null && propertyAttribute.PropertyName != null) { mappedName = propertyAttribute.PropertyName; } #if !PocketPC && !NET20 else if (dataMemberAttribute != null && dataMemberAttribute.Name != null) { mappedName = dataMemberAttribute.Name; } #endif else { mappedName = name; } property.PropertyName = ResolvePropertyName(mappedName); property.UnderlyingName = name; if (propertyAttribute != null) { property._required = propertyAttribute._required; property.Order = propertyAttribute._order; } #if !PocketPC && !NET20 else if (dataMemberAttribute != null) { property._required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default; property.Order = (dataMemberAttribute.Order != -1) ? (int?)dataMemberAttribute.Order : null; } #endif bool hasJsonIgnoreAttribute = JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null #if !(SILVERLIGHT || NETFX_CORE || PORTABLE) || JsonTypeReflector.GetAttribute <NonSerializedAttribute>(attributeProvider) != null #endif ; if (memberSerialization != MemberSerialization.OptIn) { // ignored if it has JsonIgnore or NonSerialized attributes property.Ignored = hasJsonIgnoreAttribute; } else { // ignored if it has JsonIgnore/NonSerialized or does not have DataMember or JsonProperty attributes property.Ignored = hasJsonIgnoreAttribute || (propertyAttribute == null #if !PocketPC && !NET20 && dataMemberAttribute == null #endif ); } // resolve converter for property // the class type might have a converter but the property converter takes presidence property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider); property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null; property.NullValueHandling = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null; property.DefaultValueHandling = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null; property.ReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null; property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null; property.TypeNameHandling = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null; property.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null; property.ItemIsReference = (propertyAttribute != null) ? propertyAttribute._itemIsReference : null; property.ItemConverter = (propertyAttribute != null && propertyAttribute.ItemConverterType != null) ? JsonConverterAttribute.CreateJsonConverterInstance(propertyAttribute.ItemConverterType) : null; property.ItemReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._itemReferenceLoopHandling : null; property.ItemTypeNameHandling = (propertyAttribute != null) ? propertyAttribute._itemTypeNameHandling : null; allowNonPublicAccess = false; if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic) { allowNonPublicAccess = true; } if (propertyAttribute != null) { allowNonPublicAccess = true; } if (memberSerialization == MemberSerialization.Fields) { allowNonPublicAccess = true; } #if !PocketPC && !NET20 if (dataMemberAttribute != null) { allowNonPublicAccess = true; hasExplicitAttribute = true; } #endif }