コード例 #1
0
        private static bool ShouldDisplayMember(MemberInfo member, bool hasDataContractAttribute)
        {
            JsonIgnoreAttribute          jsonIgnore         = member.GetCustomAttribute <JsonIgnoreAttribute>();
            XmlIgnoreAttribute           xmlIgnore          = member.GetCustomAttribute <XmlIgnoreAttribute>();
            IgnoreDataMemberAttribute    ignoreDataMember   = member.GetCustomAttribute <IgnoreDataMemberAttribute>();
            NonSerializedAttribute       nonSerialized      = member.GetCustomAttribute <NonSerializedAttribute>();
            ApiExplorerSettingsAttribute apiExplorerSetting = member.GetCustomAttribute <ApiExplorerSettingsAttribute>();

            bool hasMemberAttribute = member.DeclaringType.IsEnum ?
                                      member.GetCustomAttribute <EnumMemberAttribute>() != null :
                                      member.GetCustomAttribute <DataMemberAttribute>() != null;

            // Display member only if all the followings are true:
            // no JsonIgnoreAttribute
            // no XmlIgnoreAttribute
            // no IgnoreDataMemberAttribute
            // no NonSerializedAttribute
            // no ApiExplorerSettingsAttribute with IgnoreApi set to true
            // no DataContractAttribute without DataMemberAttribute or EnumMemberAttribute
            return(jsonIgnore == null &&
                   xmlIgnore == null &&
                   ignoreDataMember == null &&
                   nonSerialized == null &&
                   (apiExplorerSetting == null || !apiExplorerSetting.IgnoreApi) &&
                   (!hasDataContractAttribute || hasMemberAttribute));
        }
コード例 #2
0
 /// <summary>Initializes a new instance of the <see cref="CustomAttributes"/> class.</summary>
 /// <param name="jsonIgnoreAttribute">The json ignore attribute.</param>
 /// <param name="jsonPropertyAttribute">The json property attribute.</param>
 /// <param name="dataContractAttribute">The data contract attribute.</param>
 /// <param name="dataMemberAttribute">The data member attribute.</param>
 public CustomAttributes(
     JsonIgnoreAttribute jsonIgnoreAttribute,
     JsonPropertyAttribute jsonPropertyAttribute,
     Attribute dataContractAttribute,
     Attribute dataMemberAttribute)
 {
     JsonIgnoreAttribute   = jsonIgnoreAttribute;
     JsonPropertyAttribute = jsonPropertyAttribute;
     DataContractAttribute = dataContractAttribute;
     DataMemberAttribute   = dataMemberAttribute;
 }
コード例 #3
0
        private static IList <string> GetFieldNames(Type modelType, HashSet <Type> typesVisited)
        {
            Type connectionType;

            if (TryUnwrapConnectionType(modelType, out connectionType))
            {
                modelType = connectionType;
            }

            PropertyInfo[] properties     = modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List <string>  facebookFields = new List <string>();

            foreach (PropertyInfo property in properties)
            {
                string propertyName            = property.Name;
                AttributeCollection attributes =
                    TypeDescriptor.GetProperties(modelType)[propertyName].Attributes;

                JsonIgnoreAttribute jsonIgnoreAttribute =
                    (JsonIgnoreAttribute)attributes[typeof(JsonIgnoreAttribute)];

                if (jsonIgnoreAttribute == null)
                {
                    JsonPropertyAttribute jsonPropertyAttribute =
                        (JsonPropertyAttribute)attributes[typeof(JsonPropertyAttribute)];
                    FacebookFieldModifierAttribute modifierAttribute =
                        (FacebookFieldModifierAttribute)attributes[typeof(FacebookFieldModifierAttribute)];

                    StringBuilder fieldName = new StringBuilder(
                        jsonPropertyAttribute != null ?
                        jsonPropertyAttribute.PropertyName :
                        propertyName);

                    if (modifierAttribute != null)
                    {
                        fieldName.AppendFormat(CultureInfo.InvariantCulture, ".{0}", modifierAttribute.FieldModifier);
                    }

                    Type propertyType = property.PropertyType;
                    if (TryUnwrapConnectionType(propertyType, out connectionType))
                    {
                        typesVisited.Add(property.DeclaringType);
                        fieldName.Append(GetConnectionFields(connectionType, typesVisited));
                    }

                    facebookFields.Add(fieldName.ToString().ToLowerInvariant());
                }
            }

            return(facebookFields);
        }
        private static bool ShouldDisplayMember(MemberInfo member, bool hasDataContractAttribute)
        {
            JsonIgnoreAttribute          jsonIgnore         = member.GetCustomAttribute <JsonIgnoreAttribute>();
            XmlIgnoreAttribute           xmlIgnore          = member.GetCustomAttribute <XmlIgnoreAttribute>();
            IgnoreDataMemberAttribute    ignoreDataMember   = member.GetCustomAttribute <IgnoreDataMemberAttribute>();
            NonSerializedAttribute       nonSerialized      = member.GetCustomAttribute <NonSerializedAttribute>();
            ApiExplorerSettingsAttribute apiExplorerSetting = member.GetCustomAttribute <ApiExplorerSettingsAttribute>();

            bool hasMemberAttribute = member.DeclaringType.IsEnum ?
                                      member.GetCustomAttribute <EnumMemberAttribute>() != null :
                                      member.GetCustomAttribute <DataMemberAttribute>() != null;

            return(jsonIgnore == null &&
                   xmlIgnore == null &&
                   ignoreDataMember == null &&
                   nonSerialized == null &&
                   (apiExplorerSetting == null || !apiExplorerSetting.IgnoreApi) &&
                   (!hasDataContractAttribute || hasMemberAttribute));
        }
コード例 #5
0
            /// <summary>
            /// Determines if the property or field should not be serialized.
            /// </summary>
            /// <param name="objType"></param>
            /// <param name="member"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            /// <remarks>
            /// Checks these in order, if any returns true then this is true:
            /// - is flagged with the JsonIgnoreAttribute property
            /// - has a JsonSpecifiedProperty which returns false
            /// </remarks>
            private bool IsIgnored(Type objType, MemberInfo member, object obj)
            {
                if (JsonIgnoreAttribute.IsJsonIgnore(member))
                {
                    return(true);
                }

                string specifiedProperty = JsonSpecifiedPropertyAttribute.GetJsonSpecifiedProperty(member);

                if (!String.IsNullOrEmpty(specifiedProperty))
                {
                    PropertyInfo specProp = objType.GetProperty(specifiedProperty);
                    if (specProp != null)
                    {
                        object isSpecified = specProp.GetValue(obj, null);
                        if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified))
                        {
                            return(true);
                        }
                    }
                }

                if (this.settings.UseXmlSerializationAttributes)
                {
                    if (JsonIgnoreAttribute.IsXmlIgnore(member))
                    {
                        return(true);
                    }

                    PropertyInfo specProp = objType.GetProperty(member.Name + "Specified");
                    if (specProp != null)
                    {
                        object isSpecified = specProp.GetValue(obj, null);
                        if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified))
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
コード例 #6
0
        private static CustomAttributes GetCustomAttributes(MemberInfo property)
        {
            JsonIgnoreAttribute   jsonIgnoreAttribute   = null;
            JsonPropertyAttribute jsonPropertyAttribute = null;
            Attribute             dataMemberAttribute   = null;

            foreach (var attribute in property.GetCustomAttributes(true).OfType <Attribute>())
            {
                if (attribute is JsonIgnoreAttribute)
                {
                    jsonIgnoreAttribute = attribute as JsonIgnoreAttribute;
                }
                else if (attribute is JsonPropertyAttribute)
                {
                    jsonPropertyAttribute = attribute as JsonPropertyAttribute;
                }
                else if (attribute.GetType().Name == "DataMemberAttribute")
                {
                    dataMemberAttribute = attribute;
                }
            }

            return(new CustomAttributes(jsonIgnoreAttribute, jsonPropertyAttribute, GetDataContractAttribute(property.DeclaringType), dataMemberAttribute));
        }
コード例 #7
0
        private void DiscoverRecordFields(Type recordType, string declaringMember, bool optIn = false)
        {
            if (!recordType.IsDynamicType())
            {
                Type pt = null;
                if (ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType<ChoJSONRecordFieldAttribute>().Any()).Any())
                {
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        pt = pd.PropertyType.GetUnderlyingType();
                        bool optIn1 = ChoTypeDescriptor.GetProperties(pt).Where(pd1 => pd1.Attributes.OfType<ChoJSONRecordFieldAttribute>().Any()).Any();
                        if (optIn1 && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport)
                        {
                            DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn1);
                        }
                        else if (pd.Attributes.OfType<ChoJSONRecordFieldAttribute>().Any())
                        {
                            var obj = new ChoJSONRecordFieldConfiguration(pd.Name, pd.Attributes.OfType<ChoJSONRecordFieldAttribute>().First(), pd.Attributes.OfType<Attribute>().ToArray());
                            obj.FieldType = pt;
                            obj.PropertyDescriptor = pd;
                            obj.DeclaringMember = declaringMember == null ? null : "{0}.{1}".FormatString(declaringMember, pd.Name);
                            if (!JSONRecordFieldConfigurations.Any(c => c.Name == pd.Name))
                                JSONRecordFieldConfigurations.Add(obj);
                        }
                    }
                }
                else
                {
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        JsonIgnoreAttribute jiAttr = pd.Attributes.OfType<JsonIgnoreAttribute>().FirstOrDefault();
                        if (jiAttr != null)
                            continue;

                        pt = pd.PropertyType.GetUnderlyingType();
                        if (pt != typeof(object) && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport)
                        {
                            DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn);
                        }
                        else
                        {
                            var obj = new ChoJSONRecordFieldConfiguration(pd.Name, (string)null);
                            obj.FieldType = pt;
                            obj.PropertyDescriptor = pd;
                            obj.DeclaringMember = declaringMember == null ? null : "{0}.{1}".FormatString(declaringMember, pd.Name);
                            StringLengthAttribute slAttr = pd.Attributes.OfType<StringLengthAttribute>().FirstOrDefault();
                            if (slAttr != null && slAttr.MaximumLength > 0)
                                obj.Size = slAttr.MaximumLength;
                            ChoUseJSONSerializationAttribute sAttr = pd.Attributes.OfType<ChoUseJSONSerializationAttribute>().FirstOrDefault();
                            if (sAttr != null)
                                obj.UseJSONSerialization = true;

                            JsonPropertyAttribute jAttr = pd.Attributes.OfType<JsonPropertyAttribute>().FirstOrDefault();
                            if (jAttr != null && !jAttr.PropertyName.IsNullOrWhiteSpace())
                            {
                                obj.FieldName = jAttr.PropertyName;
                            }
                            else
                            {
                                DisplayAttribute dpAttr = pd.Attributes.OfType<DisplayAttribute>().FirstOrDefault();
                                if (dpAttr != null)
                                {
                                    if (!dpAttr.ShortName.IsNullOrWhiteSpace())
                                        obj.FieldName = dpAttr.ShortName;
                                    else if (!dpAttr.Name.IsNullOrWhiteSpace())
                                        obj.FieldName = dpAttr.Name;
                                }
                            }
                            DisplayFormatAttribute dfAttr = pd.Attributes.OfType<DisplayFormatAttribute>().FirstOrDefault();
                            if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace())
                            {
                                obj.FormatText = dfAttr.DataFormatString;
                            }
                            if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace())
                            {
                                obj.NullValue = dfAttr.NullDisplayText;
                            }
                            if (!JSONRecordFieldConfigurations.Any(c => c.Name == pd.Name))
                                JSONRecordFieldConfigurations.Add(obj);
                        }
                    }
                }
            }
        }
コード例 #8
0
        private Type DiscoverRecordFields(Type recordType, string declaringMember, bool optIn = false,
                                          List <ChoJSONRecordFieldConfiguration> recordFieldConfigurations = null, bool isTop = false)
        {
            if (recordType == null)
            {
                return(recordType);
            }
            if (!recordType.IsDynamicType())
            {
                Type pt = null;
                if (ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()).Any())
                {
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        pt = pd.PropertyType.GetUnderlyingType();
                        bool optIn1 = ChoTypeDescriptor.GetProperties(pt).Where(pd1 => pd1.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()).Any();
                        if (optIn1 && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport)
                        {
                            DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn1, recordFieldConfigurations, false);
                        }
                        else if (pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any())
                        {
                            var obj = new ChoJSONRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().First(), pd.Attributes.OfType <Attribute>().ToArray());
                            obj.FieldType          = pt;
                            obj.PropertyDescriptor = pd;
                            obj.DeclaringMember    = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name);
                            if (recordFieldConfigurations != null)
                            {
                                if (!recordFieldConfigurations.Any(c => c.Name == pd.Name))
                                {
                                    recordFieldConfigurations.Add(obj);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (isTop)
                    {
                        if (typeof(IList).IsAssignableFrom(recordType) || (recordType.IsGenericType && recordType.GetGenericTypeDefinition() == typeof(IList <>)))
                        {
                            throw new ChoParserException("Record type not supported.");
                        }
                        else if (typeof(IDictionary <string, object>).IsAssignableFrom(recordType))
                        {
                            recordType = typeof(ExpandoObject);
                            return(recordType);
                        }
                        else if (typeof(IDictionary).IsAssignableFrom(recordType))
                        {
                            recordType = typeof(ExpandoObject);
                            return(recordType);
                        }
                    }

                    if (recordType.IsSimple())
                    {
                        var obj = new ChoJSONRecordFieldConfiguration("Value", "$.Value");
                        obj.FieldType = recordType;

                        recordFieldConfigurations.Add(obj);
                        return(recordType);
                    }

                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        JsonIgnoreAttribute jiAttr = pd.Attributes.OfType <JsonIgnoreAttribute>().FirstOrDefault();
                        if (jiAttr != null)
                        {
                            continue;
                        }

                        pt = pd.PropertyType.GetUnderlyingType();
                        if (pt != typeof(object) && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport)
                        {
                            DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn, recordFieldConfigurations, false);
                        }
                        else
                        {
                            var obj = new ChoJSONRecordFieldConfiguration(pd.Name, ChoTypeDescriptor.GetPropetyAttribute <ChoJSONRecordFieldAttribute>(pd),
                                                                          pd.Attributes.OfType <Attribute>().ToArray());

                            obj.FieldType          = pt;
                            obj.PropertyDescriptor = pd;
                            obj.DeclaringMember    = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name);
                            StringLengthAttribute slAttr = pd.Attributes.OfType <StringLengthAttribute>().FirstOrDefault();
                            if (slAttr != null && slAttr.MaximumLength > 0)
                            {
                                obj.Size = slAttr.MaximumLength;
                            }
                            ChoUseJSONSerializationAttribute sAttr = pd.Attributes.OfType <ChoUseJSONSerializationAttribute>().FirstOrDefault();
                            if (sAttr != null)
                            {
                                obj.UseJSONSerialization = sAttr.Flag;
                            }
                            ChoJSONPathAttribute jpAttr = pd.Attributes.OfType <ChoJSONPathAttribute>().FirstOrDefault();
                            if (jpAttr != null)
                            {
                                obj.JSONPath = jpAttr.JSONPath;
                            }

                            JsonPropertyAttribute jAttr = pd.Attributes.OfType <JsonPropertyAttribute>().FirstOrDefault();
                            if (jAttr != null && !jAttr.PropertyName.IsNullOrWhiteSpace())
                            {
                                obj.FieldName = jAttr.PropertyName;
                                obj.JSONPath  = jAttr.PropertyName;
                                obj.Order     = jAttr.Order;
                            }
                            else
                            {
                                DisplayNameAttribute dnAttr = pd.Attributes.OfType <DisplayNameAttribute>().FirstOrDefault();
                                if (dnAttr != null && !dnAttr.DisplayName.IsNullOrWhiteSpace())
                                {
                                    obj.FieldName = dnAttr.DisplayName.Trim();
                                }
                                else
                                {
                                    DisplayAttribute dpAttr = pd.Attributes.OfType <DisplayAttribute>().FirstOrDefault();
                                    if (dpAttr != null)
                                    {
                                        if (!dpAttr.ShortName.IsNullOrWhiteSpace())
                                        {
                                            obj.FieldName = dpAttr.ShortName;
                                        }
                                        else if (!dpAttr.Name.IsNullOrWhiteSpace())
                                        {
                                            obj.FieldName = dpAttr.Name;
                                        }

                                        obj.Order = dpAttr.Order;
                                    }
                                    else
                                    {
                                        ColumnAttribute clAttr = pd.Attributes.OfType <ColumnAttribute>().FirstOrDefault();
                                        if (clAttr != null)
                                        {
                                            obj.Order = clAttr.Order;
                                            if (!clAttr.Name.IsNullOrWhiteSpace())
                                            {
                                                obj.FieldName = clAttr.Name;
                                            }
                                        }
                                    }
                                }
                            }
                            DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault();
                            if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace())
                            {
                                obj.FormatText = dfAttr.DataFormatString;
                            }
                            if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace())
                            {
                                obj.NullValue = dfAttr.NullDisplayText;
                            }

                            if (recordFieldConfigurations != null)
                            {
                                if (!recordFieldConfigurations.Any(c => c.Name == pd.Name))
                                {
                                    recordFieldConfigurations.Add(obj);
                                }
                            }
                        }
                    }
                }
            }
            return(recordType);
        }
コード例 #9
0
            private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType)
            {
                if (this.MemberMapCache.ContainsKey(objectType))
                {
                    // map was stored in cache
                    return(this.MemberMapCache[objectType]);
                }

                // create a new map
                Dictionary <string, MemberInfo> memberMap = new Dictionary <string, MemberInfo>();

                // load properties into property map
                PropertyInfo[] properties = objectType.GetProperties();
                foreach (PropertyInfo info in properties)
                {
                    if (!info.CanRead || !info.CanWrite)
                    {
                        continue;
                    }

                    if (JsonIgnoreAttribute.IsJsonIgnore(info))
                    {
                        continue;
                    }

                    string jsonName = JsonNameAttribute.GetJsonName(info);
                    if (String.IsNullOrEmpty(jsonName))
                    {
                        memberMap[info.Name] = info;
                    }
                    else
                    {
                        memberMap[jsonName] = info;
                    }
                }

                // load public fields into property map
                FieldInfo[] fields = objectType.GetFields();
                foreach (FieldInfo info in fields)
                {
                    if (!info.IsPublic)
                    {
                        continue;
                    }

                    if (JsonIgnoreAttribute.IsJsonIgnore(info))
                    {
                        continue;
                    }

                    string jsonName = JsonNameAttribute.GetJsonName(info);
                    if (String.IsNullOrEmpty(jsonName))
                    {
                        memberMap[info.Name] = info;
                    }
                    else
                    {
                        memberMap[jsonName] = info;
                    }
                }

                // store in cache for repeated usage
                this.MemberMapCache[objectType] = memberMap;

                return(memberMap);
            }
コード例 #10
0
 public CustomAttributes(
     JsonIgnoreAttribute jsonIgnoreAttribute,
     JsonPropertyAttribute jsonPropertyAttribute,
     Attribute dataContractAttribute,
     Attribute dataMemberAttribute)
 {
     JsonIgnoreAttribute = jsonIgnoreAttribute;
     JsonPropertyAttribute = jsonPropertyAttribute;
     DataContractAttribute = dataContractAttribute;
     DataMemberAttribute = dataMemberAttribute;
 }