예제 #1
0
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

#if NET45
            //IgnoreNull标注的字段根据IgnoreNulls设定是否序列化
            var ignoreNull = member.GetCustomAttribute <JsonSetting.IgnoreNullAttribute>();
            if (ignoreNull != null || IgnoreNulls)
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }
            else
            {
                property.NullValueHandling = NullValueHandling.Include;
            }

            //propertiesToIgnoreNull指定字段为Null时不序列化
            if (PropertiesToIgnoreNull.Contains(member.Name))
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }

            ////符合IgnoreValue标注值的字段不序列化
            //var ignoreValue = member.GetCustomAttribute<JsonSetting.IgnoreValueAttribute>();
            //if (ignoreValue != null)
            //{
            //    property.DefaultValueHandling = DefaultValueHandling.Ignore;
            //    var t = member.DeclaringType;
            //    property.ShouldSerialize = instance =>
            //    {
            //        var obj = Convert.ChangeType(instance, t);
            //        var value = (member as PropertyInfo).GetValue(obj, null);
            //        return value != ignoreValue.Value;
            //    };
            //}

            //枚举序列化
            var enumString = member.GetCustomAttribute <JsonSetting.EnumStringAttribute>();
            if (enumString != null)
            {
                property.Converter = new StringEnumConverter();
                //property = base.CreateProperty(member, memberSerialization);
            }
#else
            var customAttributes    = member.GetCustomAttributes(false);
            var ignoreNullAttribute = typeof(JsonSetting.IgnoreNullAttribute);
            //IgnoreNull标注的字段根据IgnoreNulls设定是否序列化
            if (IgnoreNulls || customAttributes.Count(o => o.GetType() == ignoreNullAttribute) == 1)
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }
            else
            {
                property.NullValueHandling = NullValueHandling.Include;
            }

            //TODO:一旦执行了 IgnoreNulls,有一些特殊的判断就可以不需要了

            //PropertiesToIgnoreNull指定字段为Null时不序列化
            if (PropertiesToIgnoreNull.Contains(member.Name))
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }

            //TypesToIgnoreNull特定类型字段为Null时不序列化
            if (TypesToIgnoreNull.Contains(property.PropertyType))
            {
                //Console.WriteLine("忽略null值:" + property.PropertyType);
                property.NullValueHandling = NullValueHandling.Ignore;//这样设置无效

                var t = member.DeclaringType;

                property.ShouldSerialize = instance =>
                {
                    try
                    {
                        //var obj = Convert.ChangeType(instance, t);
                        var value = (member as PropertyInfo).GetValue(instance, null);

                        //跟踪测试
                        //Console.WriteLine("Object Value:" + value);
                        //Console.WriteLine("Setting Value:" + (ignoreValue as JsonSetting.IgnoreValueAttribute).Value);
                        //Console.WriteLine("ShouldSerialize Result:" + (!value.Equals((ignoreValue as JsonSetting.IgnoreValueAttribute).Value)));

                        //return value != (ignoreValue as JsonSetting.IgnoreValueAttribute).Value;

                        //Console.WriteLine("TypesToIgnoreNull Value:" + value);
                        //Console.WriteLine("TypesToIgnoreNull Value is null:" + (value == null));

                        return(value != null);
                    }
                    catch (Exception ex)
                    {
                        Trace.SenparcTrace.BaseExceptionLog(new Exceptions.BaseException(ex.Message, ex));
                        return(true);
                    }
                };
            }


            //符合IgnoreValue标注值的字段不序列化
            var ignoreValueAttribute = typeof(JsonSetting.IgnoreValueAttribute);
            var ignoreValue          = customAttributes.FirstOrDefault(o => o.GetType() == ignoreValueAttribute);
            if (ignoreValue != null)
            {
                //property.DefaultValueHandling = DefaultValueHandling.Ignore;
                var t = member.DeclaringType;

                property.ShouldSerialize = instance =>
                {
                    //var obj = Convert.ChangeType(instance, t);
                    var value = (member as PropertyInfo).GetValue(instance, null);

                    //跟踪测试
                    //Console.WriteLine("Object Value:" + value);
                    //Console.WriteLine("Setting Value:" + (ignoreValue as JsonSetting.IgnoreValueAttribute).Value);
                    //Console.WriteLine("ShouldSerialize Result:" + (!value.Equals((ignoreValue as JsonSetting.IgnoreValueAttribute).Value)));

                    //return value != (ignoreValue as JsonSetting.IgnoreValueAttribute).Value;
                    return(!value.Equals((ignoreValue as JsonSetting.IgnoreValueAttribute).Value));
                };
            }

            //枚举序列化
            var enumStringAttribute = typeof(JsonSetting.EnumStringAttribute);
            if (customAttributes.Count(o => o.GetType() == enumStringAttribute) == 1)
            {
                property.Converter = new StringEnumConverter();
            }
#endif

            //var defaultIgnore = member.GetCustomAttribute<DefaultIgnoreAttribute>();
            //if (defaultIgnore != null)
            //{
            //    //defaultIgnore.Value == member.
            //}
            return(property);
        }
예제 #2
0
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);

#if NET45
            //IgnoreNull标注的字段根据IgnoreNulls设定是否序列化
            var ignoreNull = member.GetCustomAttribute <JsonSetting.IgnoreNullAttribute>();
            if (ignoreNull != null || IgnoreNulls)
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }
            else
            {
                property.NullValueHandling = NullValueHandling.Include;
            }

            //propertiesToIgnoreNull指定字段为Null时不序列化
            if (PropertiesToIgnoreNull.Contains(member.Name))
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }

            ////符合IgnoreValue标注值的字段不序列化
            //var ignoreValue = member.GetCustomAttribute<JsonSetting.IgnoreValueAttribute>();
            //if (ignoreValue != null)
            //{
            //    property.DefaultValueHandling = DefaultValueHandling.Ignore;
            //    var t = member.DeclaringType;
            //    property.ShouldSerialize = instance =>
            //    {
            //        var obj = Convert.ChangeType(instance, t);
            //        var value = (member as PropertyInfo).GetValue(obj, null);
            //        return value != ignoreValue.Value;
            //    };
            //}

            //枚举序列化
            var enumString = member.GetCustomAttribute <JsonSetting.EnumStringAttribute>();
            if (enumString != null)
            {
                property.Converter = new StringEnumConverter();
                //property = base.CreateProperty(member, memberSerialization);
            }
#else
            var customAttributes    = member.GetCustomAttributes(false);
            var ignoreNullAttribute = typeof(JsonSetting.IgnoreNullAttribute);
            //IgnoreNull标注的字段根据IgnoreNulls设定是否序列化
            if (IgnoreNulls || customAttributes.Count(o => o.GetType() == ignoreNullAttribute) == 1)
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }
            else
            {
                property.NullValueHandling = NullValueHandling.Include;
            }

            //propertiesToIgnoreNull指定字段为Null时不序列化
            if (PropertiesToIgnoreNull.Contains(member.Name))
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }

            ////符合IgnoreValue标注值的字段不序列化
            //var ignoreValueAttribute = typeof(JsonSetting.IgnoreValueAttribute);
            //var ignoreValue = customAttributes.FirstOrDefault(o => o.GetType() == ignoreValueAttribute);
            //if (ignoreValue != null)
            //{
            //    property.DefaultValueHandling = DefaultValueHandling.Ignore;
            //    var t = member.DeclaringType;
            //    property.ShouldSerialize = instance =>
            //    {
            //        var obj = Convert.ChangeType(instance, t);
            //        var value = (member as PropertyInfo).GetValue(obj, null);
            //        return value != (ignoreValue as JsonSetting.IgnoreValueAttribute).Value;
            //    };
            //}

            //枚举序列化
            var enumStringAttribute = typeof(JsonSetting.EnumStringAttribute);
            if (customAttributes.Count(o => o.GetType() == enumStringAttribute) == 1)
            {
                property.Converter = new StringEnumConverter();
            }
#endif

            //var defaultIgnore = member.GetCustomAttribute<DefaultIgnoreAttribute>();
            //if (defaultIgnore != null)
            //{
            //    //defaultIgnore.Value == member.
            //}
            return(property);
        }