当前位置:网站首页>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
边栏推荐
猜你喜欢
WPF效果第一百九十一篇之框选ListBox
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
JS Vanke banner rotation chart JS special effect
Pointer written test questions ~ approaching Dachang
自动化测试怎么规范部署?
[risc-v] external interrupt
[meisai] meisai thesis reference template
【SLAM】lidar-camera外参标定(港大MarsLab)无需二维码标定板
Flask learning and project practice 9: WTF form verification
3.1 detailed explanation of rtthread serial port device (V1)
随机推荐
C#(二十八)之C#鼠标事件、键盘事件
Pointer for in-depth analysis (problem solution)
JS music online playback plug-in vsplayaudio js
Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation
【Rust 笔记】18-宏
Indicator system of KQI and KPI
C language judgment, ternary operation and switch statement usage
Quick sort function in C language -- qsort
Python implementation of maddpg - (1) openai maddpg environment configuration
EDCircles: A real-time circle detector with a false detection control 翻译
SWC介绍
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搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
Codeforces Global Round 19
关于非虚函数的假派生
cookie,session,Token 这些你都知道吗?
施努卡:视觉定位系统 视觉定位系统的工作原理
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
Flask learning and project practice 8: introduction and use of cookies and sessions
Pytorch load data