当前位置:网站首页>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
边栏推荐
- Serial port-rs232-rs485-ttl
- Svg drag point crop image JS effect
- 2. GPIO related operations
- Pytorch基础——(2)张量(tensor)的数学运算
- 11. Container with the most water
- Containerization Foundation
- [meisai] meisai thesis reference template
- Differential GPS RTK thousand search
- 3分钟带你了解微信小程序开发
- No qualifying bean of type ‘......‘ available
猜你喜欢

Facebook等大厂超十亿用户数据遭泄露,早该关注DID了

Cubemx 移植正点原子LCD显示例程

Recommended papers on remote sensing image super-resolution

2.2 STM32 GPIO操作

SWC介绍

Map sorts according to the key value (ascending plus descending)

Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art

Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art

Suggestions for new engineer team members

WPF效果第一百九十一篇之框选ListBox
随机推荐
three. JS page background animation liquid JS special effect
C language -- structs, unions, enumerations, and custom types
Four logs of MySQL server layer
[matlab] - draw a five-star red flag
[analysis of variance] single factor analysis and multi factor analysis
登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
Quick sort function in C language -- qsort
MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置
2.1 rtthread pin设备详解
暑期刷题-Day3
施努卡:3d视觉检测应用行业 机器视觉3d检测
自动化测试怎么规范部署?
有条件地 [JsonIgnore]
3.2 detailed explanation of rtthread serial port device (V2)
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
LTE CSFB test analysis
施努卡:什么是视觉定位系统 视觉系统如何定位
P7735-[noi2021] heavy and heavy edges [tree chain dissection, line segment tree]
给新人工程师组员的建议
Shell pass parameters