private void InitializeContract(JsonContract contract) { JsonContainerAttribute jsonContainerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.UnderlyingType); if (jsonContainerAttribute != null) { contract.IsReference = jsonContainerAttribute._isReference; } else { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.UnderlyingType); if (dataContractAttribute != null && dataContractAttribute.IsReference) { contract.IsReference = new bool?(true); } } contract.Converter = this.ResolveContractConverter(contract.UnderlyingType); contract.InternalConverter = JsonSerializer.GetMatchingConverter(DefaultContractResolver.BuiltInConverters, contract.UnderlyingType); if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true) || contract.CreatedType.IsValueType) { contract.DefaultCreator = this.GetDefaultCreator(contract.CreatedType); contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType && ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null); } this.ResolveCallbackMethods(contract, contract.UnderlyingType); }
/// <summary> /// Gets the serializable members for the type. /// </summary> /// <param name="objectType">The type to get serializable members for.</param> /// <returns>The serializable members for the type.</returns> protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { #if !PocketPC && !SILVERLIGHT && !MONO DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); #endif List <MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags); List <MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); List <MemberInfo> serializableMembers = new List <MemberInfo>(); foreach (MemberInfo member in allMembers) { if (defaultMembers.Contains(member)) { serializableMembers.Add(member); } else { if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member) != null) { serializableMembers.Add(member); } #if !PocketPC && !SILVERLIGHT && !MONO else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(member) != null) { serializableMembers.Add(member); } #endif } } return(serializableMembers); }
private void InitializeContract(JsonContract contract) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.UnderlyingType); if (containerAttribute != null) { contract.IsReference = containerAttribute._isReference; } else { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.UnderlyingType); // doesn't have a null value if (dataContractAttribute != null && dataContractAttribute.IsReference) { contract.IsReference = true; } } contract.Converter = ResolveContractConverter(contract.UnderlyingType); // then see whether object is compadible with any of the built in converters contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.UnderlyingType); if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true) || contract.CreatedType.IsValueType) { contract.DefaultCreator = GetDefaultCreator(contract.CreatedType); contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType && ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null); } ResolveCallbackMethods(contract, contract.UnderlyingType); }
protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); List <MemberInfo> list = (from m in ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags) where !ReflectionUtils.IsIndexedProperty(m) select m).ToList(); List <MemberInfo> list2 = (from m in ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) where !ReflectionUtils.IsIndexedProperty(m) select m).ToList(); List <MemberInfo> list3 = new List <MemberInfo>(); foreach (MemberInfo item in list2) { if (SerializeCompilerGeneratedMembers || !item.IsDefined(typeof(CompilerGeneratedAttribute), inherit: true)) { if (list.Contains(item)) { list3.Add(item); } else if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(item) != null) { list3.Add(item); } else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(item) != null) { list3.Add(item); } } } if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out Type _)) { list3 = list3.Where(ShouldSerializeEntityMember).ToList(); } return(list3); }
protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { bool ignoreSerializableAttribute = this.IgnoreSerializableAttribute; MemberSerialization objectMemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute); List <MemberInfo> list = (from m in ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) where !ReflectionUtils.IsIndexedProperty(m) select m).ToList <MemberInfo>(); List <MemberInfo> list2 = new List <MemberInfo>(); if (objectMemberSerialization != MemberSerialization.Fields) { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); List <MemberInfo> list3 = (from m in ReflectionUtils.GetFieldsAndProperties(objectType, this.DefaultMembersSearchFlags) where !ReflectionUtils.IsIndexedProperty(m) select m).ToList <MemberInfo>(); foreach (MemberInfo current in list) { if (this.SerializeCompilerGeneratedMembers || !current.IsDefined(typeof(CompilerGeneratedAttribute), true)) { if (list3.Contains(current)) { list2.Add(current); } else if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(current) != null) { list2.Add(current); } else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(current) != null) { list2.Add(current); } else if (objectMemberSerialization == MemberSerialization.Fields && current.MemberType() == MemberTypes.Field) { list2.Add(current); } } } Type type; if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out type)) { list2 = list2.Where(new Func <MemberInfo, bool>(this.ShouldSerializeEntityMember)).ToList <MemberInfo>(); } } else { foreach (MemberInfo current2 in list) { FieldInfo fieldInfo = current2 as FieldInfo; if (fieldInfo != null && !fieldInfo.IsStatic) { list2.Add(current2); } } } return(list2); }
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute) { hasExplicitAttribute = false; DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType); DataMemberAttribute dataMemberAttribute = (dataContractAttribute == null || !(attributeProvider is MemberInfo)) ? null : JsonTypeReflector.GetDataMemberAttribute((MemberInfo)attributeProvider); JsonPropertyAttribute attribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider); if (attribute != null) { hasExplicitAttribute = true; } bool flag = JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null; string propertyName = (attribute != null && attribute.PropertyName != null) ? attribute.PropertyName : ((dataMemberAttribute == null || dataMemberAttribute.Name == null) ? name : dataMemberAttribute.Name); property.PropertyName = ResolvePropertyName(propertyName); property.UnderlyingName = name; if (attribute != null) { property.Required = attribute.Required; property.Order = attribute._order; } else if (dataMemberAttribute != null) { property.Required = (dataMemberAttribute.IsRequired ? Required.AllowNull : Required.Default); property.Order = ((dataMemberAttribute.Order == -1) ? null : new int?(dataMemberAttribute.Order)); } else { property.Required = Required.Default; } property.Ignored = (flag || (memberSerialization == MemberSerialization.OptIn && attribute == null && dataMemberAttribute == null)); property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); property.DefaultValue = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider)?.Value; property.NullValueHandling = attribute?._nullValueHandling; property.DefaultValueHandling = attribute?._defaultValueHandling; property.ReferenceLoopHandling = attribute?._referenceLoopHandling; property.ObjectCreationHandling = attribute?._objectCreationHandling; property.TypeNameHandling = attribute?._typeNameHandling; property.IsReference = attribute?._isReference; allowNonPublicAccess = false; if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic) { allowNonPublicAccess = true; } if (attribute != null) { allowNonPublicAccess = true; } if (dataMemberAttribute != null) { allowNonPublicAccess = true; hasExplicitAttribute = true; } }
private void InitializeContract(JsonContract contract) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.UnderlyingType); if (containerAttribute != null) { contract.IsReference = containerAttribute._isReference; } #if !PocketPC && !NET20 else { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.UnderlyingType); // doesn't have a null value if (dataContractAttribute != null && dataContractAttribute.IsReference) { contract.IsReference = true; } } #endif contract.DefaultContstructor = ReflectionUtils.GetDefaultConstructor(contract.CreatedType, false) ?? ReflectionUtils.GetDefaultConstructor(contract.CreatedType, true); foreach (MethodInfo method in contract.UnderlyingType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { Type prevAttributeType = null; ParameterInfo[] parameters = method.GetParameters(); #if !PocketPC && !SILVERLIGHT && !NET20 if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), contract.OnSerializing, ref prevAttributeType)) { contract.OnSerializing = method; } if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), contract.OnSerialized, ref prevAttributeType)) { contract.OnSerialized = method; } if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), contract.OnDeserializing, ref prevAttributeType)) { contract.OnDeserializing = method; } if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), contract.OnDeserialized, ref prevAttributeType)) { contract.OnDeserialized = method; } #endif if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), contract.OnError, ref prevAttributeType)) { contract.OnError = method; } } }
public static MemberSerialization GetObjectMemberSerialization(Type objectType) { JsonObjectAttribute jsonObjectAttribute = JsonTypeReflector.GetJsonObjectAttribute(objectType); if (jsonObjectAttribute != null) { return(jsonObjectAttribute.MemberSerialization); } if (JsonTypeReflector.GetDataContractAttribute(objectType) != null) { return(MemberSerialization.OptIn); } return(MemberSerialization.OptOut); }
/// <summary> /// Gets the serializable members for the type. /// </summary> /// <param name="objectType">The type to get serializable members for.</param> /// <returns>The serializable members for the type.</returns> protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { #if !PocketPC && !NET20 DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); #endif List <MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags) .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList(); List <MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList(); List <MemberInfo> serializableMembers = new List <MemberInfo>(); foreach (MemberInfo member in allMembers) { // exclude members that are compiler generated if set if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof(CompilerGeneratedAttribute), true)) { if (defaultMembers.Contains(member)) { // add all members that are found by default member search serializableMembers.Add(member); } else { // add members that are explicitly marked with JsonProperty/DataMember attribute if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member) != null) { serializableMembers.Add(member); } #if !PocketPC && !NET20 else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(member) != null) { serializableMembers.Add(member); } #endif } } } #if !PocketPC && !SILVERLIGHT && !NET20 Type match; // don't include EntityKey on entities objects... this is a bit hacky if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out match)) { serializableMembers = serializableMembers.Where(ShouldSerializeEntityMember).ToList(); } #endif return(serializableMembers); }
public static MemberSerialization GetObjectMemberSerialization( Type objectType, bool ignoreSerializableAttribute) { JsonObjectAttribute cachedAttribute = JsonTypeReflector.GetCachedAttribute <JsonObjectAttribute>((object)objectType); if (cachedAttribute != null) { return(cachedAttribute.MemberSerialization); } if (JsonTypeReflector.GetDataContractAttribute(objectType) != null) { return(MemberSerialization.OptIn); } return(!ignoreSerializableAttribute && JsonTypeReflector.GetCachedAttribute <SerializableAttribute>((object)objectType) != null ? MemberSerialization.Fields : MemberSerialization.OptOut); }
protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { Type type; DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); List <MemberInfo> list = ( from m in ReflectionUtils.GetFieldsAndProperties(objectType, this.DefaultMembersSearchFlags) where !ReflectionUtils.IsIndexedProperty(m) select m).ToList <MemberInfo>(); List <MemberInfo> memberInfos = ( from m in ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) where !ReflectionUtils.IsIndexedProperty(m) select m).ToList <MemberInfo>(); List <MemberInfo> memberInfos1 = new List <MemberInfo>(); foreach (MemberInfo memberInfo in memberInfos) { if (!this.SerializeCompilerGeneratedMembers && memberInfo.IsDefined(typeof(CompilerGeneratedAttribute), true)) { continue; } if (list.Contains(memberInfo)) { memberInfos1.Add(memberInfo); } else if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>((ICustomAttributeProvider)memberInfo) == null) { if (dataContractAttribute == null || JsonTypeReflector.GetAttribute <DataMemberAttribute>((ICustomAttributeProvider)memberInfo) == null) { continue; } memberInfos1.Add(memberInfo); } else { memberInfos1.Add(memberInfo); } } if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out type)) { memberInfos1 = memberInfos1.Where <MemberInfo>(new Func <MemberInfo, bool>(this.ShouldSerializeEntityMember)).ToList <MemberInfo>(); } return(memberInfos1); }
/// <summary> /// Gets the serializable members for the type. /// </summary> /// <param name="objectType">The type to get serializable members for.</param> /// <returns>The serializable members for the type.</returns> protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { #if !PocketPC && !NET20 DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); #endif List <MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags); List <MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); List <MemberInfo> serializableMembers = new List <MemberInfo>(); foreach (MemberInfo member in allMembers) { if (defaultMembers.Contains(member)) { serializableMembers.Add(member); } else { if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member) != null) { serializableMembers.Add(member); } #if !PocketPC && !NET20 else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(member) != null) { serializableMembers.Add(member); } #endif } } #if !PocketPC && !SILVERLIGHT && !NET20 Type match; // don't include EntityKey on entities objects... this is a bit hacky if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out match)) { serializableMembers = serializableMembers.Where(ShouldSerializeEntityMember).ToList(); } #endif return(serializableMembers); }
protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); List <MemberInfo> list = Enumerable.ToList <MemberInfo>(Enumerable.Where <MemberInfo>(ReflectionUtils.GetFieldsAndProperties(objectType, this.DefaultMembersSearchFlags), (MemberInfo m) => !ReflectionUtils.IsIndexedProperty(m))); List <MemberInfo> list2 = Enumerable.ToList <MemberInfo>(Enumerable.Where <MemberInfo>(ReflectionUtils.GetFieldsAndProperties(objectType, 60), (MemberInfo m) => !ReflectionUtils.IsIndexedProperty(m))); List <MemberInfo> list3 = new List <MemberInfo>(); using (List <MemberInfo> .Enumerator enumerator = list2.GetEnumerator()) { while (enumerator.MoveNext()) { MemberInfo current = enumerator.get_Current(); if (this.SerializeCompilerGeneratedMembers || !current.IsDefined(typeof(CompilerGeneratedAttribute), true)) { if (list.Contains(current)) { list3.Add(current); } else if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(current) != null) { list3.Add(current); } else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(current) != null) { list3.Add(current); } } } } Type type; if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out type)) { list3 = Enumerable.ToList <MemberInfo>(Enumerable.Where <MemberInfo>(list3, new Func <MemberInfo, bool>(this.ShouldSerializeEntityMember))); } return(list3); }
public static MemberSerialization GetObjectMemberSerialization(Type objectType, bool ignoreSerializableAttribute) { JsonObjectAttribute jsonObjectAttribute = JsonTypeReflector.GetJsonObjectAttribute(objectType); if (jsonObjectAttribute != null) { return(jsonObjectAttribute.MemberSerialization); } DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); if (dataContractAttribute != null) { return(MemberSerialization.OptIn); } if (!ignoreSerializableAttribute) { SerializableAttribute serializableAttribute = JsonTypeReflector.GetSerializableAttribute(objectType); if (serializableAttribute != null) { return(MemberSerialization.Fields); } } return(MemberSerialization.OptOut); }
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute) { hasExplicitAttribute = false; DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType); DataMemberAttribute dataMemberAttribute; if (dataContractAttribute != null && attributeProvider is MemberInfo) { dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)attributeProvider); } else { dataMemberAttribute = null; } JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider); if (propertyAttribute != null) { hasExplicitAttribute = true; } bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null); string mappedName; if (propertyAttribute != null && propertyAttribute.PropertyName != null) { mappedName = propertyAttribute.PropertyName; } else if (dataMemberAttribute != null && dataMemberAttribute.Name != null) { mappedName = dataMemberAttribute.Name; } else { mappedName = name; } property.PropertyName = ResolvePropertyName(mappedName); property.UnderlyingName = name; if (propertyAttribute != null) { property.Required = propertyAttribute.Required; property.Order = propertyAttribute._order; } else if (dataMemberAttribute != null) { property.Required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default; property.Order = (dataMemberAttribute.Order != -1) ? (int?)dataMemberAttribute.Order : null; } else { property.Required = Required.Default; } property.Ignored = (hasIgnoreAttribute || (memberSerialization == MemberSerialization.OptIn && propertyAttribute == null && dataMemberAttribute == null )); // 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; allowNonPublicAccess = false; if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic) { allowNonPublicAccess = true; } if (propertyAttribute != null) { allowNonPublicAccess = true; } if (dataMemberAttribute != null) { allowNonPublicAccess = true; hasExplicitAttribute = true; } }
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 }
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute) { DataMemberAttribute dataMemberAttribute; string propertyName; bool flag; NullValueHandling? nullable; DefaultValueHandling? nullable1; ReferenceLoopHandling? nullable2; ObjectCreationHandling?nullable3; TypeNameHandling? nullable4; bool? nullable5; int?nullable6; hasExplicitAttribute = false; if (JsonTypeReflector.GetDataContractAttribute(declaringType) == null || !(attributeProvider is MemberInfo)) { dataMemberAttribute = null; } else { dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)attributeProvider); } JsonPropertyAttribute attribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider); if (attribute != null) { hasExplicitAttribute = true; } bool attribute1 = JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null; if (attribute == null || attribute.PropertyName == null) { propertyName = (dataMemberAttribute == null || dataMemberAttribute.Name == null ? name : dataMemberAttribute.Name); } else { propertyName = attribute.PropertyName; } property.PropertyName = this.ResolvePropertyName(propertyName); property.UnderlyingName = name; if (attribute != null) { property.Required = attribute.Required; property.Order = attribute._order; } else if (dataMemberAttribute == null) { property.Required = Required.Default; } else { property.Required = (!dataMemberAttribute.IsRequired ? Required.Default : Required.AllowNull); JsonProperty jsonProperty = property; if (dataMemberAttribute.Order == -1) { nullable6 = null; } else { nullable6 = new int?(dataMemberAttribute.Order); } jsonProperty.Order = nullable6; } JsonProperty jsonProperty1 = property; if (attribute1) { flag = true; } else { flag = (memberSerialization != MemberSerialization.OptIn || attribute != null ? false : dataMemberAttribute == null); } jsonProperty1.Ignored = flag; property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider); property.DefaultValue = (defaultValueAttribute == null ? null : defaultValueAttribute.Value); JsonProperty jsonProperty2 = property; if (attribute == null) { nullable = null; } else { nullable = attribute._nullValueHandling; } jsonProperty2.NullValueHandling = nullable; JsonProperty jsonProperty3 = property; if (attribute == null) { nullable1 = null; } else { nullable1 = attribute._defaultValueHandling; } jsonProperty3.DefaultValueHandling = nullable1; JsonProperty jsonProperty4 = property; if (attribute == null) { nullable2 = null; } else { nullable2 = attribute._referenceLoopHandling; } jsonProperty4.ReferenceLoopHandling = nullable2; JsonProperty jsonProperty5 = property; if (attribute == null) { nullable3 = null; } else { nullable3 = attribute._objectCreationHandling; } jsonProperty5.ObjectCreationHandling = nullable3; JsonProperty jsonProperty6 = property; if (attribute == null) { nullable4 = null; } else { nullable4 = attribute._typeNameHandling; } jsonProperty6.TypeNameHandling = nullable4; JsonProperty jsonProperty7 = property; if (attribute == null) { nullable5 = null; } else { nullable5 = attribute._isReference; } jsonProperty7.IsReference = nullable5; allowNonPublicAccess = false; if ((this.DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic) { allowNonPublicAccess = true; } if (attribute != null) { allowNonPublicAccess = true; } if (dataMemberAttribute != null) { allowNonPublicAccess = true; hasExplicitAttribute = true; } }
/// <summary> /// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>. /// </summary> /// <param name="contract">The member's declaring types <see cref="JsonObjectContract"/>.</param> /// <param name="member">The member to create a <see cref="JsonProperty"/> for.</param> /// <returns>A created <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.</returns> protected virtual JsonProperty CreateProperty(JsonObjectContract contract, MemberInfo member) { JsonProperty property = new JsonProperty(); property.Member = member; #if !PocketPC && !SILVERLIGHT && !MONO DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(member.DeclaringType); DataMemberAttribute dataMemberAttribute; if (dataContractAttribute != null) { dataMemberAttribute = JsonTypeReflector.GetAttribute <DataMemberAttribute>(member); } else { dataMemberAttribute = null; } #endif JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member); bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(member) != null); string mappedName; if (propertyAttribute != null && propertyAttribute.PropertyName != null) { mappedName = propertyAttribute.PropertyName; } #if !PocketPC && !SILVERLIGHT && !MONO else if (dataMemberAttribute != null && dataMemberAttribute.Name != null) { mappedName = dataMemberAttribute.Name; } #endif else { mappedName = member.Name; } property.PropertyName = ResolvePropertyName(mappedName); if (propertyAttribute != null) { property.Required = propertyAttribute.IsRequired; } #if !PocketPC && !SILVERLIGHT && !MONO else if (dataMemberAttribute != null) { property.Required = dataMemberAttribute.IsRequired; } #endif else { property.Required = false; } property.Ignored = (hasIgnoreAttribute || (contract.MemberSerialization == MemberSerialization.OptIn && propertyAttribute == null #if !PocketPC && !SILVERLIGHT && !MONO && dataMemberAttribute == null #endif )); property.Readable = ReflectionUtils.CanReadMemberValue(member); property.Writable = ReflectionUtils.CanSetMemberValue(member); property.MemberConverter = JsonTypeReflector.GetConverter(member, ReflectionUtils.GetMemberUnderlyingType(member)); DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(member); 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.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null; return(property); }
/// <summary> /// Gets the serializable members for the type. /// </summary> /// <param name="objectType">The type to get serializable members for.</param> /// <returns>The serializable members for the type.</returns> protected virtual List <MemberInfo> GetSerializableMembers(Type objectType) { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType); List <MemberInfo> defaultMembers = new List <MemberInfo> (); List <MemberInfo> .Enumerator defMembersIterator = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags).GetEnumerator(); while (defMembersIterator.MoveNext()) { if (!ReflectionUtils.IsIndexedProperty(defMembersIterator.Current)) { defaultMembers.Add(defMembersIterator.Current); } } //List<MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags).Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList(); List <MemberInfo> allMembers = new List <MemberInfo> (); List <MemberInfo> .Enumerator allMembersIterator = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).GetEnumerator(); while (allMembersIterator.MoveNext()) { if (!ReflectionUtils.IsIndexedProperty(allMembersIterator.Current)) { allMembers.Add(allMembersIterator.Current); } } //List<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList(); List <MemberInfo> serializableMembers = new List <MemberInfo>(); foreach (MemberInfo member in allMembers) { // exclude members that are compiler generated if set if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof(CompilerGeneratedAttribute), true)) { if (defaultMembers.Contains(member)) { // add all members that are found by default member search serializableMembers.Add(member); } else { // add members that are explicitly marked with JsonProperty/DataMember attribute if (JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member) != null) { serializableMembers.Add(member); } else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute <DataMemberAttribute>(member) != null) { serializableMembers.Add(member); } } } } Type match; // don't include EntityKey on entities objects... this is a bit hacky if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out match)) { List <MemberInfo> tempMemberInfoList = new List <MemberInfo>(); List <MemberInfo> .Enumerator iterator = serializableMembers.GetEnumerator(); while (iterator.MoveNext()) { if (ShouldSerializeEntityMember(iterator.Current)) { tempMemberInfoList.Add(iterator.Current); } } serializableMembers.Clear(); serializableMembers = null; serializableMembers = tempMemberInfoList; //serializableMembers = serializableMembers.Where (ShouldSerializeEntityMember).ToList (); } return(serializableMembers); }
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess, out bool hasExplicitAttribute) { hasExplicitAttribute = false; DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType); DataMemberAttribute dataMemberAttribute; if (dataContractAttribute != null && attributeProvider is MemberInfo) { dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo)attributeProvider); } else { dataMemberAttribute = null; } JsonPropertyAttribute attribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider); if (attribute != null) { hasExplicitAttribute = true; } bool flag = JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null; string propertyName; if (attribute != null && attribute.PropertyName != null) { propertyName = attribute.PropertyName; } else if (dataMemberAttribute != null && dataMemberAttribute.Name != null) { propertyName = dataMemberAttribute.Name; } else { propertyName = name; } property.PropertyName = this.ResolvePropertyName(propertyName); property.UnderlyingName = name; if (attribute != null) { property.Required = attribute.Required; property.Order = attribute._order; } else if (dataMemberAttribute != null) { property.Required = ((!dataMemberAttribute.IsRequired) ? Required.Default : Required.AllowNull); property.Order = ((dataMemberAttribute.Order == -1) ? default(int?) : new int?(dataMemberAttribute.Order)); } else { property.Required = Required.Default; } property.Ignored = (flag || (memberSerialization == MemberSerialization.OptIn && attribute == null && dataMemberAttribute == null)); property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); DefaultValueAttribute attribute2 = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider); property.DefaultValue = ((attribute2 == null) ? null : attribute2.get_Value()); property.NullValueHandling = ((attribute == null) ? default(NullValueHandling?) : attribute._nullValueHandling); property.DefaultValueHandling = ((attribute == null) ? default(DefaultValueHandling?) : attribute._defaultValueHandling); property.ReferenceLoopHandling = ((attribute == null) ? default(ReferenceLoopHandling?) : attribute._referenceLoopHandling); property.ObjectCreationHandling = ((attribute == null) ? default(ObjectCreationHandling?) : attribute._objectCreationHandling); property.TypeNameHandling = ((attribute == null) ? default(TypeNameHandling?) : attribute._typeNameHandling); property.IsReference = ((attribute == null) ? default(bool?) : attribute._isReference); allowNonPublicAccess = false; if ((this.DefaultMembersSearchFlags & 32) == 32) { allowNonPublicAccess = true; } if (attribute != null) { allowNonPublicAccess = true; } if (dataMemberAttribute != null) { allowNonPublicAccess = true; hasExplicitAttribute = true; } }
private void InitializeContract(JsonContract contract) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.UnderlyingType); if (containerAttribute != null) { contract.IsReference = containerAttribute._isReference; } #if !PocketPC && !NET20 else { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.UnderlyingType); // doesn't have a null value if (dataContractAttribute != null && dataContractAttribute.IsReference) { contract.IsReference = true; } } #endif contract.Converter = ResolveContractConverter(contract.UnderlyingType); // then see whether object is compadible with any of the built in converters contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.UnderlyingType); if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true) || contract.CreatedType.IsValueType) { contract.DefaultCreator = GetDefaultCreator(contract.CreatedType); contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType && ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null); } foreach (MethodInfo method in contract.UnderlyingType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { // compact framework errors when getting parameters for a generic method // lame, but generic methods should not be callbacks anyway if (method.ContainsGenericParameters) { continue; } Type prevAttributeType = null; ParameterInfo[] parameters = method.GetParameters(); #if !PocketPC if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), contract.OnSerializing, ref prevAttributeType)) { contract.OnSerializing = method; } if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), contract.OnSerialized, ref prevAttributeType)) { contract.OnSerialized = method; } if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), contract.OnDeserializing, ref prevAttributeType)) { contract.OnDeserializing = method; } if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), contract.OnDeserialized, ref prevAttributeType)) { contract.OnDeserialized = method; } #endif if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), contract.OnError, ref prevAttributeType)) { contract.OnError = method; } } }
/// <summary> /// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>. /// </summary> /// <param name="contract">The member's declaring types <see cref="JsonObjectContract"/>.</param> /// <param name="member">The member to create a <see cref="JsonProperty"/> for.</param> /// <returns>A created <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.</returns> protected virtual JsonProperty CreateProperty(JsonObjectContract contract, MemberInfo member) { JsonProperty property = new JsonProperty(); property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member); #if !PocketPC property.ValueProvider = new DynamicValueProvider(member); #else property.ValueProvider = new ReflectionValueProvider(member); #endif // resolve converter for property // the class type might have a converter but the property converter takes presidence property.Converter = JsonTypeReflector.GetJsonConverter(member, property.PropertyType); #if !PocketPC && !NET20 DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(member.DeclaringType); DataMemberAttribute dataMemberAttribute; if (dataContractAttribute != null) { dataMemberAttribute = JsonTypeReflector.GetAttribute <DataMemberAttribute>(member); } else { dataMemberAttribute = null; } #endif JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member); bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(member) != null); 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 = member.Name; } property.PropertyName = ResolvePropertyName(mappedName); if (propertyAttribute != null) { property.Required = propertyAttribute.Required; } #if !PocketPC && !NET20 else if (dataMemberAttribute != null) { property.Required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default; } #endif else { property.Required = Required.Default; } property.Ignored = (hasIgnoreAttribute || (contract.MemberSerialization == MemberSerialization.OptIn && propertyAttribute == null #if !PocketPC && !NET20 && dataMemberAttribute == null #endif )); property.Readable = ReflectionUtils.CanReadMemberValue(member); property.Writable = ReflectionUtils.CanSetMemberValue(member); property.MemberConverter = JsonTypeReflector.GetJsonConverter(member, ReflectionUtils.GetMemberUnderlyingType(member)); DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(member); 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.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null; return(property); }
/// <summary> /// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>. /// </summary> /// <param name="memberSerialization">The member's parent <see cref="MemberSerialization"/>.</param> /// <param name="member">The member to create a <see cref="JsonProperty"/> for.</param> /// <returns>A created <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.</returns> protected virtual JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { JsonProperty property = new JsonProperty(); property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member); property.ValueProvider = CreateMemberValueProvider(member); // resolve converter for property // the class type might have a converter but the property converter takes presidence property.Converter = JsonTypeReflector.GetJsonConverter(member, property.PropertyType); #if !PocketPC && !NET20 DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(member.DeclaringType); DataMemberAttribute dataMemberAttribute; if (dataContractAttribute != null) { dataMemberAttribute = JsonTypeReflector.GetAttribute <DataMemberAttribute>(member); } else { dataMemberAttribute = null; } #endif JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(member); bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(member) != null); 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 = member.Name; } property.PropertyName = ResolvePropertyName(mappedName); if (propertyAttribute != null) { property.Required = propertyAttribute.Required; } #if !PocketPC && !NET20 else if (dataMemberAttribute != null) { property.Required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default; } #endif else { property.Required = Required.Default; } property.Ignored = (hasIgnoreAttribute || (memberSerialization == MemberSerialization.OptIn && propertyAttribute == null #if !PocketPC && !NET20 && dataMemberAttribute == null #endif )); bool allowNonPublicAccess = false; if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic) { allowNonPublicAccess = true; } if (propertyAttribute != null) { allowNonPublicAccess = true; } #if !PocketPC && !NET20 if (dataMemberAttribute != null) { allowNonPublicAccess = true; } #endif property.Readable = ReflectionUtils.CanReadMemberValue(member, allowNonPublicAccess); property.Writable = ReflectionUtils.CanSetMemberValue(member, allowNonPublicAccess); property.MemberConverter = JsonTypeReflector.GetJsonConverter(member, ReflectionUtils.GetMemberUnderlyingType(member)); DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(member); 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.ShouldSerialize = CreateShouldSerializeTest(member); return(property); }
private void SetPropertySettingsFromAttributes(JsonProperty property, object attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess) { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType); MemberInfo memberInfo = attributeProvider as MemberInfo; DataMemberAttribute dataMemberAttribute; if (dataContractAttribute != null && memberInfo != null) { dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute(memberInfo); } else { dataMemberAttribute = null; } JsonPropertyAttribute attribute = JsonTypeReflector.GetAttribute <JsonPropertyAttribute>(attributeProvider); if (attribute != null) { property.HasMemberAttribute = true; } string propertyName; if (attribute != null && attribute.PropertyName != null) { propertyName = attribute.PropertyName; } else if (dataMemberAttribute != null && dataMemberAttribute.Name != null) { propertyName = dataMemberAttribute.Name; } else { propertyName = name; } property.PropertyName = this.ResolvePropertyName(propertyName); property.UnderlyingName = name; bool flag = false; if (attribute != null) { property._required = attribute._required; property.Order = attribute._order; property.DefaultValueHandling = attribute._defaultValueHandling; flag = true; } else if (dataMemberAttribute != null) { property._required = new Required?(dataMemberAttribute.IsRequired ? Required.AllowNull : Required.Default); property.Order = ((dataMemberAttribute.Order != -1) ? new int?(dataMemberAttribute.Order) : null); property.DefaultValueHandling = ((!dataMemberAttribute.EmitDefaultValue) ? new DefaultValueHandling?(DefaultValueHandling.Ignore) : null); flag = true; } bool flag2 = JsonTypeReflector.GetAttribute <JsonIgnoreAttribute>(attributeProvider) != null || JsonTypeReflector.GetAttribute <JsonExtensionDataAttribute>(attributeProvider) != null || JsonTypeReflector.GetAttribute <NonSerializedAttribute>(attributeProvider) != null; if (memberSerialization != MemberSerialization.OptIn) { bool flag3 = JsonTypeReflector.GetAttribute <IgnoreDataMemberAttribute>(attributeProvider) != null; property.Ignored = (flag2 || flag3); } else { property.Ignored = (flag2 || !flag); } property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType); DefaultValueAttribute attribute2 = JsonTypeReflector.GetAttribute <DefaultValueAttribute>(attributeProvider); if (attribute2 != null) { property.DefaultValue = attribute2.Value; } property.NullValueHandling = ((attribute != null) ? attribute._nullValueHandling : null); property.ReferenceLoopHandling = ((attribute != null) ? attribute._referenceLoopHandling : null); property.ObjectCreationHandling = ((attribute != null) ? attribute._objectCreationHandling : null); property.TypeNameHandling = ((attribute != null) ? attribute._typeNameHandling : null); property.IsReference = ((attribute != null) ? attribute._isReference : null); property.ItemIsReference = ((attribute != null) ? attribute._itemIsReference : null); property.ItemConverter = ((attribute != null && attribute.ItemConverterType != null) ? JsonConverterAttribute.CreateJsonConverterInstance(attribute.ItemConverterType) : null); property.ItemReferenceLoopHandling = ((attribute != null) ? attribute._itemReferenceLoopHandling : null); property.ItemTypeNameHandling = ((attribute != null) ? attribute._itemTypeNameHandling : null); allowNonPublicAccess = false; if ((this.DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic) { allowNonPublicAccess = true; } if (attribute != null) { allowNonPublicAccess = true; } if (memberSerialization == MemberSerialization.Fields) { allowNonPublicAccess = true; } if (dataMemberAttribute != null) { allowNonPublicAccess = true; property.HasMemberAttribute = true; } }