首页 > 其他分享 >Json过滤

Json过滤

时间:2022-12-27 20:01:03浏览次数:61  
标签:shouldSerialize ShouldSerializeContractResolver member Json 过滤 var new property

  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;
            }
        }

标签:shouldSerialize,ShouldSerializeContractResolver,member,Json,过滤,var,new,property
From: https://www.cnblogs.com/tianbang/p/17008867.html

相关文章

  • IOS中Json解析的四种方法
    作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式。有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验(​​......
  • SQL Server JSON 转行 后转列
    1.使用ParseJsonFuncJSON转行USE[DataIntegration]GOCREATEFUNCTION[dbo].[ParseJsonFunc](@jsonnvarchar(max))RETURNS@hierarchytable(object_idintNOT......
  • 使用awk过滤出来eth0的IP
    原始输出[root]#ifconfigeth0Linkencap:EthernetHWaddr6C:92:BF:2B:DC:2Finetaddr:100.8.73.88Bcast:100.8.73.255Mask:255.255.255.0......
  • GO json.Unmarshal() 解析不区分json字段的大小写
    GOjson.Unmarshal()解析不区分json字段的大小写demopackagemainimport( "encoding/json" "fmt")typeDemostruct{ ABDstring`json:"ABD"`}typeDem......
  • rapidjson使用总结
    目录​​(?)​​​​[-]​​​​rapidjson简介​​​​Dom解析示例​​rapidjson简介rapidjson是腾讯的开源json解析框架,用c++实现。由于全部代码仅用headerfile实现,所以很......
  • 列表搜索(过滤)
    在有些项目中,一些没分页的搜索功能需要做,搜索一般分为精确搜索和模糊搜索。搜索也要叫过滤,一般用filter来实现。consta=[1,2,3,4,5,7,23,12]constresult=a.filter(i......
  • 用IDEA插件实现json字符串格式化
    用IDEA插件实现json字符串格式化Echoo华地于2022-10-1411:50:47发布分类专栏:使用工具文章标签:intellij-ideaidejson格式化json字符串格式化订阅专栏安装插件......
  • 16、SQL操作JSON字段
    Mysql5.7版本以后提供一个原生的Json类型,Json值将不再以字符串的形式存储,而是采用一种允许快速读取文本元素(documentelements)的内部二进制(internalbinary)格式。一、Mysq......
  • fastjson提取json返回值(java)
    返回json数据:{"data":{"offset":2,"total":2952,"restTotal":2950,"dataList":[{"ISBN":"9787539197456",......
  • MapReduce实战之过滤日志及自定义日志输出路径案例(自定义OutputFormat)
    1)需求      过滤输入的log日志中是否包含atguigu      (1)包含atguigu的网站输出到e:/atguigu.log      (2)不包含atguigu的网站输出到e:/other.log2)输入......