惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
Security Latest
Security Latest
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Scott Helme
Scott Helme
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
WordPress大学
WordPress大学
雷峰网
雷峰网
L
LangChain Blog
Y
Y Combinator Blog
I
Intezer
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
P
Privacy International News Feed
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
AI
AI
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
Martin Fowler
Martin Fowler
K
Kaspersky official blog
U
Unit 42
S
Schneier on Security
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
T
Tor Project blog
aimingoo的专栏
aimingoo的专栏
Engineering at Meta
Engineering at Meta
G
GRAHAM CLULEY
美团技术团队
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
P
Proofpoint News Feed
爱范儿
爱范儿
F
Fortinet All Blogs
有赞技术团队
有赞技术团队

博客园 - 威少小二orz

团结引擎抖音小游戏一定要手动填缓存资源域名 团结引擎+Addressable+Instant Game打包抖音小游戏 团结引擎发布抖音小游戏(十万个坑已踩完) 团结引擎发布小游戏区分不同平台 团结引擎发布小游戏与js版本SDK的互相调用 Unity发布京东小游戏 LayerMask的使用规范 Unity RectTransform中使用stretch模式时代码动态控制Left、Top、Right、Bottom Unity使用https请求握手失败的处理方案 【MAUI开发】一、初识MAUI Android中使用GSON解析JSON数据 游戏软著渠道的用户协议和隐私协议方案(启动显示方案) C#文件夹复制 安卓多个Activity大退时自动重启的问题处理 C#中接收到的Json数据中key为数字的问题处理 对于UniWebView这种组件违规获取信息的处理 TapTap实名认证-Android Unity使用Get和Post传递json数据并转换成class对象 Unity获取手机本地应用以及调起apk安装、启动app、安装完成回调 Unity接入穿山甲GroMore广告——重构偏2(Banner广告)
C#在Json序列化中动态忽略某些属性或字段
威少小二orz · 2023-03-17 · via 博客园 - 威少小二orz

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

    }