当前位置:网站首页>有条件地 [JsonIgnore]
有条件地 [JsonIgnore]
2022-07-06 03:39:00 【dotNET跨平台】
前言
通常,在进行 JSON 序列化或反序列化时,如果要忽略某个属性,我们会使用 [JsonIgnore] 特性:
public class User
{
public int Id { get; set; }
[JsonIgnore]
public string Name { get; set; }
}
var user = new User { Id = 1, Name = "MyIO" };
Console.WriteLine(JsonSerializer.Serialize(user));
//输出
{"Id":1}偶然发现, [JsonIgnore] 特性还可以指定 Condition 属性:
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Name { get; set; }那么它有什么作用呢?
JsonIgnoreCondition 枚举
可以通过设置 [JsonIgnore] 特性的 Condition 属性来指定条件排除。Condition 属性对应的 JsonIgnoreCondition 枚举提供下列选项:
Always - 始终忽略属性。如果未指定 Condition,则默认此选项
Never - 忽略全局设置,始终序列化和反序列化属性
public class A
{
public string Name { get; } = "MyIO";
}
public class B
{
[JsonIgnore( Condition = JsonIgnoreCondition.Never)]
public string Name { get; } = "MyIO";
}
//全局设置为忽略只读属性
var options = new JsonSerializerOptions
{
IgnoreReadOnlyProperties = true
};
Console.WriteLine(JsonSerializer.Serialize(new A(), options));
Console.WriteLine(JsonSerializer.Serialize(new B(), options));
//输出
//A 中的 Name 不会序列化,而 B 中的 Name 还是会序列化
{}
{"Name":"B"}WhenWritingDefault - 如果属性值是该属性数据类型的默认值,则在序列化中忽略属性
public class User
{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int Id { get; set; }
public string Name { get; set; }
}
var user = new User { Id = 0, Name = "" };
Console.WriteLine(JsonSerializer.Serialize(user));
//输出
{"Name":""}WhenWritingNull - 如果属性值是 null,则在序列化中忽略属性
public class User
{
public int Id { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Name { get; set; }
}
var user = new User { Id = 1, Name = null };
Console.WriteLine(JsonSerializer.Serialize(user));
//输出
{"Id":1}结论
我们还可以使用 JsonSerializerOptions 全局设置忽略属性的方式:
DefaultIgnoreCondition - 默认的JsonIgnoreCondition,默认值为 Never
IgnoreReadOnlyProperties - 忽略所有只读属性
添加微信号【MyIO666】,邀你加入技术交流群
边栏推荐
- 3857 Mercator coordinate system converted to 4326 (WGS84) longitude and latitude coordinates
- Indicator system of KQI and KPI
- Deno介绍
- Suggestions for new engineer team members
- 2.2 STM32 GPIO operation
- 11. Container with the most water
- ESBuild & SWC浅谈: 新一代构建工具
- Item 10: Prefer scoped enums to unscoped enums.
- Pelosi: Congress will soon have legislation against members' stock speculation
- Python implementation of maddpg - (1) openai maddpg environment configuration
猜你喜欢

Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation

自动化测试怎么规范部署?

Explore pointers and pointer types in depth

Pytorch load data
![[optimization model] Monte Carlo method of optimization calculation](/img/e6/2865806ffbbfaa8cc07ebf625fcde6.jpg)
[optimization model] Monte Carlo method of optimization calculation

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Recommended papers on remote sensing image super-resolution
![[slam] orb-slam3 parsing - track () (3)](/img/87/b580837778c2c9f6bac5ba49403d6b.png)
[slam] orb-slam3 parsing - track () (3)

11. Container with the most water

Tomb. Weekly update of Finance (February 7 - February 13)
随机推荐
BUAA喜鹊筑巢
three.js网页背景动画液态js特效
Arabellacpc 2019 (supplementary question)
IPv6 comprehensive experiment
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
[matlab] - draw a five-star red flag
A brief introduction to symbols and link libraries in C language
Pytoch foundation - (2) mathematical operation of tensor
MPLS experiment
JS音乐在线播放插件vsPlayAudio.js
Multi project programming minimalist use case
Schnuka: 3D vision detection application industry machine vision 3D detection
canvas切积木小游戏代码
UDP reliable transport protocol (quic)
[American competition] mathematical terms
Redo file corruption repair
Edcircles: a real time circle detector with a false detection control translation
Failure causes and optimization methods of LTE CSFB
Four logs of MySQL server layer
2.2 STM32 GPIO操作