当前位置:网站首页>C# | 使用Json序列化对象时忽略只读的属性
C# | 使用Json序列化对象时忽略只读的属性
2022-08-01 03:55:00 【猿长大人】
C# 使用Json序列化对象时忽略只读的属性
前言
将对象序列化成为Json字符串是一个使用频率非常高的功能。Json格式具有很高的可读性,同时相较于XML更节省空间。
在开发过程中经常会遇到需要保存配置的场景,比如将配置信息保存在配置类型的实例中,再将这个对象序列化成为Json字符串并保存。当需要加载配置时,则是读取Json格式的字符串再将其还原成配置对象。在序列化的过程中,默认会将所有公开的属性和字段都序列化进入Json字符串中,这其中也会包含只读的属性或字段,而只读的属性和字段在反序列化的过程中其实是无意义的,也就是说这一部分存储是多余的。
本文将讲解如何在执行Json序列化时,忽略掉那些只读的属性和字段。
示例
修改前
创建一个student类型,包含四个属性,分别是ID,名称,生日和年龄。
其中年龄是根据生日计算得出的,是一个只读的属性。
使用序列化时的默认配置,编代码如下:嗯
public class Student
{
public int Id {
get; set; }
public string Name {
get; set; }
public DateTime Birthday {
get; set; }
public int Age => DateTime.Now.Year - Birthday.Year;
}
internal class Program
{
static void Main(string[] args)
{
Student student = new Student()
{
Id = 1,
Name = "张三",
Birthday = DateTime.Parse("2008-08-08"),
};
Console.WriteLine(JsonConvert.SerializeObject(student,Formatting.Indented));
Console.Read();
}
}
执行结果如下图所示,可以看到,只读的年龄属性也被序列化到了Json字符串中。
修改后
第一步:创建一个用于过滤只读属性的ContractResolver;
第二步:在序列化之前,创建一个Json序列化配置对象,并将刚才编写的ContractResolver放入配置中;
第三步:执行序列化是传入配置对象;
完整代码如下所示:
public class Student
{
public int Id {
get; set; }
public string Name {
get; set; }
public DateTime Birthday {
get; set; }
public int Age => DateTime.Now.Year - Birthday.Year;
}
internal class Program
{
static void Main(string[] args)
{
Student student = new Student()
{
Id = 1,
Name = "张三",
Birthday = DateTime.Parse("2008-08-08"),
};
var settings = new JsonSerializerSettings()
{
ContractResolver = new WritablePropertiesOnlyResolver()
};
Console.WriteLine(JsonConvert.SerializeObject(student, Formatting.Indented, settings));
Console.Read();
}
}
class WritablePropertiesOnlyResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
return props.Where(p => p.Writable).ToList();
}
}
运行结果如下:
至此,我们成功过滤了所有的只读属性。
边栏推荐
- leetcode6133. 分组的最大数量(中等)
- 最新 955 不加班的公司名单
- Advice given by experts with four years of development experience in Flutter tutorial
- Take you to experience a type programming practice
- Difference Between Compiled and Interpreted Languages
- Valentine's Day Romantic 3D Photo Wall [with source code]
- HCIP(15)
- 雪糕和轮胎
- 二舅
- 【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
猜你喜欢
基于ProXmoX VE的虚拟化家庭服务器(篇一)—ProXmoX VE 安装及基础配置
JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类
What is dynamic programming and what is the knapsack problem
Hackers can how bad to what degree?
黑客到底可以厉害到什么程度?
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
The maximum quantity leetcode6133. Grouping (medium)
Open source project site must-have & communication area function
This map drawing tool is amazing, I recommend it~~
软件测试周刊(第82期):其实所有纠结做选择的人心里早就有了答案,咨询只是想得到内心所倾向的选择。
随机推荐
Elastic Stack的介绍
Valentine's Day Romantic 3D Photo Wall [with source code]
【愚公系列】2022年07月 Go教学课程 024-函数
软件测试基础理论知识—用例篇
The fledgling Xiao Li's 114th blog project notes: Wisdom cloud intelligent flower watering device combat (3) - basic Demo implementation
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
How to promote new products online?
如何下载Keil包
纽约大学等 | TM-Vec:用于快速同源检测和比对的模版建模向量
微软 Win10 照片磁贴后的又一“刺客”,谷歌 Chrome 浏览器将在新标签页展示用户照片
[kali-information collection] enumeration - DNS enumeration: DNSenum, fierce
[FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
软件测试面试(三)
移动端页面秒开优化总结
测试
【Make YOLO Great Again】YOLOv1-v7全系列大解析(Neck篇)
HCIP(15)
PMP 相关方管理必背总结
JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类
【堆】小红的数组