Beispiel #1
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>
        internal bool IsIgnored(Type objType, MemberInfo member, object obj)
        {
            if (BTIgnoreAttribute.IsJsonIgnore(member))
            {
                return(true);
            }

            // Cutting support for this
            // -- Aron

            /*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 the class is specified as opt-in serialization only, members must have the JsonMember attribute
            if (objType.GetCustomAttributes(typeof(BTOptInAttribute), true).Length != 0)
            {
                if (member.GetCustomAttributes(typeof(BTPropertyAttribute), true).Length == 0)
                {
                    return(true);
                }
            }

            if (UseXmlSerializationAttributes)
            {
                if (BTIgnoreAttribute.IsXmlIgnore(member))
                {
                    return(true);
                }

                /*
                 * throw new System.Exception ("No longer supporting XML Serialization Attributes");
                 *
                 * 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);
        }
Beispiel #2
0
        /** Creates a member map for the type */
        private Dictionary <string, MemberInfo> CreateMemberMap(Type objectType)
        {
            Dictionary <string, MemberInfo> memberMap;

            if (this.MemberMapCache.TryGetValue(objectType, out memberMap))
            {
                // map was stored in cache
                return(memberMap);
            }

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

            // load properties into property map
            Type tp = objectType;

            while (tp != null)
            {
                PropertyInfo[] properties = TCU.GetTypeInfo(tp).GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                for (int i = 0; i < properties.Length; i++)
                {
                    PropertyInfo info = properties[i];
                    if (!info.CanRead || !info.CanWrite)
                    {
                        continue;
                    }

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

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

                // load public fields into property map
                FieldInfo[] fields = TCU.GetTypeInfo(tp).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                foreach (FieldInfo info in fields)
                {
                    if (!info.IsPublic &&
#if WINDOWS_STORE
                        info.GetCustomAttribute <JsonMemberAttribute>(false) == null
#else
                        info.GetCustomAttributes(typeof(BTPropertyAttribute), false).Length == 0
#endif
                        )
                    {
                        continue;
                    }

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

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

                tp = tp.BaseType;
            }

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

            return(memberMap);
        }