C#在Json序列化中动态忽略某些属性或字段

C#在Json序列化中动态忽略某些属性或字段

先准备好Newtonsoft.Json的程序包。

 

固定忽略:

在代码上面加上[JsonIgnore]特性。

 

动态忽略:

使用鲜为人知的ShouldSerialize方法。

ShouldSerialize的用法:

在需要序列化的类当中增加一个bool类型的方法,

方法的名字为ShouldSerialize和你需要序列化的字段的名字相加,

且没有参数。

如果方法返回true,则该字段会被序列化。

如果方法返回false,则该字段不会被序列化。

 

如果字段拥有对应的ShouldSerialize方法,则会被序列化,

如果字段打上了[JsonIgnore]特性,则以特性优先,不会再触发ShouldSerialize方法。

 

以下示例代码一共有9个字段或属性a,b,c,d,e,f,g,h,s:

然后给出调用方式:

执行结果:

{"a":1,"c":"3","e":true,"h":null}

解释:

我们一共写了a-g的7个ShouldSerialize方法,名字的前缀都为ShouldSerialize,后缀分别为a-g的字段名字。

字段b和s上打了固定的忽略特性,不会被序列化。

字段a,b,c,e上的ShouldSerialize的返回结果为true,会被序列化,但是b字段打了固定忽略,所以b不会被序列化。

字段h,没有打上固定忽略特性,也没有对应的ShouldSerialize方法,默认会被序列化。

            TestClase test = new TestClase();
            string s = JsonConvert.SerializeObject(test);
 public class TestClase 
    {
        public int a = 1;
        [JsonIgnore]
        public int b = 2;
        public string c = "3";
        public string d = "4";
        public bool e = true;
        public bool f = false;
        public string g { get; set; }
        public string h { get; set; }

        [JsonIgnore]//固定忽略字段s,这个字段不会被序列化
        public string s = "a,b,c,e";

        //方法的名字为ShouldSerialize + a
        public bool ShouldSerializea()
        {
        
            List<string> strs = s.Split(',').ToList<string>();
            if (strs.FindAll((x)=>x == "a").Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //方法的名字为ShouldSerialize + b
        public bool ShouldSerializeb()
        {
         
            List<string> strs = s.Split(',').ToList<string>();
            if (strs.FindAll((x) => x == "b").Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //方法的名字为ShouldSerialize + c
        public bool ShouldSerializec()
        {
     
            List<string> strs = s.Split(',').ToList<string>();
            if (strs.FindAll((x) => x == "c").Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool ShouldSerialized()
        {
           
            List<string> strs = s.Split(',').ToList<string>();
            if (strs.FindAll((x) => x == "d").Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool ShouldSerializee()
        {
      
            List<string> strs = s.Split(',').ToList<string>();
            if (strs.FindAll((x) => x == "e").Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool ShouldSerializef()
        {
            
            List<string> strs = s.Split(',').ToList<string>();
            if (strs.FindAll((x) => x == "f").Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ShouldSerializeg()
        {

            List<string> strs = s.Split(',').ToList<string>();
            if (strs.FindAll((x) => x == "g").Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }

 

posted @ 2023-03-17 16:33  威少小二orz  阅读(4069)  评论(3编辑  收藏  举报