当前位置:网站首页>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
边栏推荐
- 简述C语言中的符号和链接库
- pytorch加载数据
- C#(二十八)之C#鼠标事件、键盘事件
- Basic concepts of LTE user experience
- Facebook等大廠超十億用戶數據遭泄露,早該關注DID了
- Yyds dry inventory what is test driven development
- Prime Protocol宣布在Moonbeam上的跨链互连应用程序
- Suggestions for new engineer team members
- KS003基于JSP和Servlet实现的商城系统
- [meisai] meisai thesis reference template
猜你喜欢
2.13 weekly report
JVM的手术刀式剖析——一文带你窥探JVM的秘密
阿里测试师用UI自动化测试实现元素定位
Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
C language -- structs, unions, enumerations, and custom types
C#(三十)之C#comboBox ListView treeView
Pointer for in-depth analysis (problem solution)
Cubemx transplantation punctual atom LCD display routine
Quartz misfire missed and compensated execution
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
随机推荐
BUAA喜鹊筑巢
2.1 rtthread pin设备详解
MySQL reads missing data from a table in a continuous period of time
在 .NET 6 中使用 Startup.cs 更简洁的方法
mysql从一个连续时间段的表中读取缺少数据
mysql关于自增长增长问题
Pelosi: Congress will soon have legislation against members' stock speculation
Quick sort function in C language -- qsort
[risc-v] external interrupt
BUAA calculator (expression calculation - expression tree implementation)
cookie,session,Token 这些你都知道吗?
Multi project programming minimalist use case
SAP ALV cell level set color
After five years of testing in byte, I was ruthlessly dismissed in July, hoping to wake up my brother who was paddling
如何修改表中的字段约束条件(类型,default, null等)
Schnuka: visual positioning system working principle of visual positioning system
Recommended papers on remote sensing image super-resolution
Brush questions in summer -day3
Overview of super-resolution reconstruction of remote sensing images
RT-Thread--Lwip之FTP(2)