Json过滤

  var settings = new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore,
                    ContractResolver = ShouldSerializeContractResolver.Instance
                };
                return JsonConvert.SerializeObject(
                     new ResponseInfo<T>()
                     {
                         code = RESULT_CODE_SUCCESS,
                         message = RESULT_MSG_SUCCESS,
                         data = data,
                         traceId = LogicCallContext.GetIISRequestId()
                     }, settings);

过滤器

   internal class ShouldSerializeContractResolver : DefaultContractResolver
        {
            internal static ShouldSerializeContractResolver Instance = new ShouldSerializeContractResolver();

            private static List<string> _ignoreProperties = new List<string> {"inusersysno", "inusername", "editusersysno", "editusername", "editdatestr", "ismydata" };

            private ShouldSerializeContractResolver()
            {
            }

            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                var property = base.CreateProperty(member, memberSerialization);

                if (member.MemberType == MemberTypes.Property)
                {
                    // 需忽略的属性列表
                    var isIngoreProperty = _ignoreProperties.Contains(property.PropertyName.ToLower());
                    property.ShouldSerialize = instance =>
                    {
                        var shouldSerialize = false;
                        if (!isIngoreProperty)
                        {
                            var prop = instance.GetType().GetProperty(member.Name);
                            // 忽略只读属性
                            if (prop.CanWrite)
                            {
                                var value = prop.GetValue(instance, null);
                                // 忽略空字符串
                                if (property.PropertyType == typeof(string))
                                {
                                    shouldSerialize = !string.IsNullOrWhiteSpace(value as string);
                                }
                                // 忽略空列表
                                else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
                                {
                                    var enumerable = value as IEnumerable;
                                    shouldSerialize = enumerable != null ? enumerable.GetEnumerator().MoveNext() : false;
                                }
                                else
                                {
                                    shouldSerialize = value != null;
                                }
                            }
                        }
                        return shouldSerialize;
                    };
                }
                else
                {
                    property.ShouldSerialize = i => false;
                }

                return property;
            }
        }
posted @ 2022-12-27 19:53  [在河之洲]  阅读(46)  评论(0编辑  收藏  举报