当前位置:网站首页>Conditionally [jsonignore]
Conditionally [jsonignore]
2022-07-06 03:46:00 【Dotnet cross platform】
Preface
Usually , It's going on JSON When serializing or deserializing , If you want to ignore an attribute , We will use [JsonIgnore] characteristic :
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));
// Output
{"Id":1}
By chance , [JsonIgnore] Properties can also be specified Condition
attribute :
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Name { get; set; }
So what does it do ?
JsonIgnoreCondition enumeration
Can be set by [JsonIgnore] Characteristic Condition Property to specify the conditional exclusion .Condition Property corresponds to JsonIgnoreCondition Enumeration provides the following options :
Always - Always ignore attributes . If not specified Condition, This option defaults
Never - Ignore global settings , Always serialize and deserialize attributes
public class A
{
public string Name { get; } = "MyIO";
}
public class B
{
[JsonIgnore( Condition = JsonIgnoreCondition.Never)]
public string Name { get; } = "MyIO";
}
// Global setting to ignore read-only properties
var options = new JsonSerializerOptions
{
IgnoreReadOnlyProperties = true
};
Console.WriteLine(JsonSerializer.Serialize(new A(), options));
Console.WriteLine(JsonSerializer.Serialize(new B(), options));
// Output
//A Medium Name No serialization , and B Medium Name Still serialize
{}
{"Name":"B"}
WhenWritingDefault - If the attribute value is the default value of the attribute data type , Then ignore the attribute in the serialization
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));
// Output
{"Name":""}
WhenWritingNull - If the property value is null, Then ignore the attribute in the serialization
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));
// Output
{"Id":1}
Conclusion
We can also use JsonSerializerOptions Set the way to ignore properties globally :
DefaultIgnoreCondition - default JsonIgnoreCondition, The default value is Never
IgnoreReadOnlyProperties - Ignore all read-only properties
Add microsignals 【MyIO666】, Invite you to join the technical exchange group
边栏推荐
- Blue Bridge Cup - Castle formula
- 2.2 STM32 GPIO operation
- [prediction model] difference method model
- Yyds dry inventory what is test driven development
- Svg drag point crop image JS effect
- ESBuild & SWC浅谈: 新一代构建工具
- 施努卡:3d视觉检测应用行业 机器视觉3d检测
- 3.1 rtthread 串口设备(V1)详解
- 在 .NET 6 中使用 Startup.cs 更简洁的方法
- Crawler of explanation and application of agency theory
猜你喜欢
An article will give you a comprehensive understanding of the internal and external components of "computer"
[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
Blue style mall website footer code
Flask learning and project practice 8: introduction and use of cookies and sessions
Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did
C#(二十八)之C#鼠标事件、键盘事件
LTE CSFB test analysis
Alibaba testers use UI automated testing to achieve element positioning
[Massey] Massey font format and typesetting requirements
JS Vanke banner rotation chart JS special effect
随机推荐
多项目编程极简用例
3.2 detailed explanation of rtthread serial port device (V2)
Crawler of explanation and application of agency theory
RT-Thread--Lwip之FTP(2)
SAP ALV color code corresponding color (finishing)
Alibaba testers use UI automated testing to achieve element positioning
2、GPIO相关操作
Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did
Pointer for in-depth analysis (problem solution)
Overview of super-resolution reconstruction of remote sensing images
Basic concepts of LTE user experience
BUAA计算器(表达式计算-表达式树实现)
[risc-v] external interrupt
RT thread -- FTP of LwIP (2)
【SLAM】ORB-SLAM3解析——跟踪Track()(3)
Introduction to DeNO
Oracle ORA error message
Take you to wechat applet development in 3 minutes
Pytorch基础——(2)张量(tensor)的数学运算
Pytorch load data