当前位置:网站首页>有条件地 [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】,邀你加入技术交流群
边栏推荐
- 数据分析——seaborn可视化(笔记自用)
- 2.2 STM32 GPIO操作
- Pelosi: Congress will soon have legislation against members' stock speculation
- [optimization model] Monte Carlo method of optimization calculation
- Cross origin cross domain request
- UDP reliable transport protocol (quic)
- The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
- [slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
- 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
- BUAA计算器(表达式计算-表达式树实现)
猜你喜欢
Pytorch load data
2.2 fonctionnement stm32 GPIO
Safety science to | travel, you must read a guide
SAP ALV单元格级别设置颜色
Multi project programming minimalist use case
2.1 rtthread pin device details
SAP ALV color code corresponding color (finishing)
给新人工程师组员的建议
Getting started with applet cloud development - getting user search content
Edcircles: a real time circle detector with a false detection control translation
随机推荐
Pelosi: Congress will soon have legislation against members' stock speculation
数据分析——seaborn可视化(笔记自用)
Schnuka: visual positioning system working principle of visual positioning system
Advanced learning of MySQL -- Fundamentals -- isolation level of transactions
Map sorts according to the key value (ascending plus descending)
3分钟带你了解微信小程序开发
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
three. JS page background animation liquid JS special effect
BUAA calculator (expression calculation - expression tree implementation)
Data analysis Seaborn visualization (for personal use)
EDCircles: A real-time circle detector with a false detection control 翻译
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
pytorch加载数据
JS Vanke banner rotation chart JS special effect
Redo file corruption repair
ASU & OSU | model based regularized off-line meta reinforcement learning
Microkernel structure understanding
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)