Ejemplo n.º 1
0
        public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            var result = new Dictionary <string, object>();

            if (obj == null)
            {
                return(result);
            }

            var properties = obj.GetType().GetProperties();

            foreach (var propertyInfo in properties)
            {
                //continue;
                //排除的属性
                bool excludedProp = propertyInfo.IsDefined(typeof(JsonSetting.ExcludedAttribute), true);
                if (excludedProp)
                {
                    result.Add(propertyInfo.Name, propertyInfo.GetValue(obj, null));
                }
                else
                {
                    if (!this._jsonSetting.PropertiesToIgnore.Contains(propertyInfo.Name))
                    {
                        bool ignoreProp = propertyInfo.IsDefined(typeof(ScriptIgnoreAttribute), true);
                        if ((this._jsonSetting.IgnoreNulls || ignoreProp) && propertyInfo.GetValue(obj, null) == null)
                        {
                            continue;
                        }


                        //当值匹配时需要忽略的属性
                        JsonSetting.IgnoreValueAttribute attri = propertyInfo.GetCustomAttribute <JsonSetting.IgnoreValueAttribute>();
                        if (attri != null && attri.Value.Equals(propertyInfo.GetValue(obj)))
                        {
                            continue;
                        }

                        JsonSetting.EnumStringAttribute enumStringAttri = propertyInfo.GetCustomAttribute <JsonSetting.EnumStringAttribute>();
                        if (enumStringAttri != null)
                        {
                            //枚举类型显示字符串
                            result.Add(propertyInfo.Name, propertyInfo.GetValue(obj).ToString());
                        }
                        else
                        {
                            result.Add(propertyInfo.Name, propertyInfo.GetValue(obj, null));
                        }
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public void PropertiesToDictionary(ref Dictionary <string, object> result, object item)
        {
            var properties = item.GetType().GetProperties();

            foreach (var propertyInfo in properties)
            {
                //continue;
                //排除的属性
                bool excludedProp = propertyInfo.IsDefined(typeof(JsonSetting.ExcludedAttribute), true);
                if (excludedProp)
                {
                    result.Add(propertyInfo.Name, propertyInfo.GetValue(item, null));
                }
                else
                {
                    if (!this._jsonSetting.PropertiesToIgnore.Contains(propertyInfo.Name))
                    {
                        bool ignoreProp = propertyInfo.IsDefined(typeof(ScriptIgnoreAttribute), true);
                        if ((this._jsonSetting.IgnoreNulls || ignoreProp) && propertyInfo.GetValue(item, null) == null)
                        {
                            continue;
                        }
                        //当值匹配时需要忽略的属性
                        JsonSetting.IgnoreValueAttribute attri = propertyInfo.GetCustomAttribute <JsonSetting.IgnoreValueAttribute>();
                        if (attri != null && attri.Value.Equals(propertyInfo.GetValue(item)))
                        {
                            continue;
                        }

                        JsonSetting.EnumStringAttribute enumStringAttri = propertyInfo.GetCustomAttribute <JsonSetting.EnumStringAttribute>();
                        if (enumStringAttri != null)
                        {
                            //枚举类型显示字符串
                            result.Add(propertyInfo.Name, propertyInfo.GetValue(item).ToString());
                        }
                        else
                        {
                            result.Add(propertyInfo.Name, propertyInfo.GetValue(item, null));
                        }
                    }
                }
            }
        }