JsonSerializerSettings常用配置整理
1.忽略某些属性
MemberSerialization.OptIn
默认情况下,所有的成员不会被序列化,类中的成员只有标有特性JsonProperty的才会被序列化,当类的成员很多,但客户端仅仅需要一部分数据时,很有用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[JsonObject(MemberSerialization.OptIn)] public class TestClass { public int A { get; set; } public long B { get; set; } public string C { get; set; } [JsonProperty] public DateTime D { get; set; } public TestEnum E { get; set; } [JsonIgnore] public TestClass F { get; set; } public List<TestClass> G { get; set; } public bool H { get; set; } } public enum TestEnum { TestEnum0 = 0, TestEnum1 = 1, TestEnum2 = 2, TestEnum3 = 3, } [JsonObject(MemberSerialization.OptIn)] public class Person { [JsonIgnore] public string Name { get; set; } [JsonProperty] public int Age { get; set; } public DateTime BirthDay { get; set; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Console.WriteLine(JsonConvert.SerializeObject(new Person { Name = "test", Age = 18, BirthDay = new DateTime(2011, 1, 1) })); TestClass testClass = new TestClass { A = 1, B = 2, C = "C", D = DateTime.Now, E = TestEnum.TestEnum1, F = new TestClass { A = 11, B = 22, C = "Cc", D = DateTime.Now, E = TestEnum.TestEnum2, F = new TestClass() }, G = new List<TestClass> { new TestClass { A=111, B=222, C="CCC", E=TestEnum.TestEnum1, F=new TestClass() } } }; Console.WriteLine(JsonConvert.SerializeObject(testClass));
输出结果:
{"Age":18}
{"D":"2019-11-26T10:37:33.7655514+08:00"}
MemberSerialization.OptOut
默认类中所有公有成员会被序列化,如果不想被序列化,可以用特性JsonIgnore
将上面的代码OptIn改为OptOut输出结果:
{"Age":18,"BirthDay":"2011-01-01T00:00:00"}
{"A":1,"B":2,"C":"C","D":"2019-11-26T15:49:37.9507028+08:00","E":1,"G":[{"A":111,"B":222,"C":"CCC","D":"0001-01-01T00:00:00","E":1,"G":null,"H":false}],"H":false}
2.默认值的处理
DefaultValueHandling.Ignore 序列化和反序列化时,忽略默认值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
TestClass testClass = new TestClass { A = 1, B = 2, C = "C", D = DateTime.Now, E = TestEnum.TestEnum1, F = new TestClass { A = 11, B = 22, C = "Cc", D = DateTime.Now, E = TestEnum.TestEnum2, F = new TestClass() }, G = new List<TestClass> { new TestClass { A=111, B=222, C="CCC", E=TestEnum.TestEnum1, F=new TestClass() } } }; JsonSerializerSettings settings = new JsonSerializerSettings(); settings.DefaultValueHandling = DefaultValueHandling.Ignore; Console.WriteLine(JsonConvert.SerializeObject(testClass,settings));
输出结果:
{"A":1,"B":2,"C":"C","D":"2019-11-26T15:56:21.7204132+08:00","E":1,"G":[{"A":111,"B":222,"C":"CCC","E":1}]}
DefaultValueHandling.Include 序列化和反序列化时,包含默认值
将JsonSerializerSettings.DefaultValueHandling属性设为DefaultValueHandling.Include输出结果:
{"A":1,"B":2,"C":"C","D":"2019-11-26T16:21:48.5429746+08:00","E":1,"G":[{"A":111,"B":222,"C":"CCC","D":"0001-01-01T00:00:00","E":1,"G":null,"H":false}],"H":false}
DefaultValueHandling.Populate 在反序列化时,具有默认值但没有JSON的成员将被设置为其默认值。
DefaultValueHandling.IgnoreAndPopulate 在序列化对象时忽略成员值与成员默认值相同的成员,在反序列化时将成员设置为其默认值
不指定的情况下,序列化时 默认 包含-DefaultValueHandling.Include
3.空值的处理
设置JsonSerializerSettings.NullValueHandling属性
对序列化过程中所有属性生效的,想单独对某一个属性生效可以使用JsonProperty
值为NullValueHandling.Ignore时,输出结果为:
{"A":1,"B":2,"C":"C","D":"2019-11-26T16:34:21.3205145+08:00","E":1,"G":[{"A":111,"B":222,"C":"CCC","D":"0001-01-01T00:00:00","E":1,"H":false}],"H":false}
值为NullValueHandling.Include时,输出结果为:
{"A":1,"B":2,"C":"C","D":"2019-11-26T16:34:21.3205145+08:00","E":1,"G":[{"A":111,"B":222,"C":"CCC","D":"0001-01-01T00:00:00","E":1,"G":null,"H":false}],"H":false}
4.支持非公共成员
序列化时默认都是处理公共成员,如果需要处理非公共成员,就要在该成员上加特性JsonProperty
5.日期处理(DateFormatHandling)
对于Dateime类型日期的格式化,系统自带的会格式化成iso日期标准{"BirthDay":"2011-01-01T00:00:00"}
解决方案1:添加特性,指定转换格式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class LongDateTimeConvert: Newtonsoft.Json.Converters.IsoDateTimeConverter { public LongDateTimeConvert() : base() { base.DateTimeFormat = "yyyy-MM-dd"; } } //[JsonConverter(typeof(LongDateTimeConvert))] public DateTime BirthDay { get; set; }
输出结果:{"BirthDay":"2011-01-01"}
解决方案2:指定JsonSerializerSettings.DateFormatString的值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
JsonSerializerSettings settings = new JsonSerializerSettings(); 2 settings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; 3 var str = JsonConvert.SerializeObject(new Person { Name = "test", Age = 18, BirthDay = new DateTime(2011, 1, 1) }, settings); 4 Console.WriteLine(str);
输出结果:{"BirthDay":"2011-01-01 00:00:00"}
同时指定时以特性(即方案1)为准
6.自定义序列化的字段名称
实体中定义的属性名可能不是自己想要的名称,但是又不能更改实体定义,这个时候可以自定义序列化字段名称
[JsonProperty(PropertyName = "age")]
public int Age { get; set; }
输出结果:{"age":18}
7.动态决定属性是否序列化
指定JsonSerializerSettings.ContractResolver的实例,继承DefaultContractResolver类,重写方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class JsonContractResolver : DefaultContractResolver { protected IEnumerable<string> _excludedProperties; public JsonContractResolver(IEnumerable<string> excludedProperties) { this._excludedProperties = excludedProperties; } protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { return base.CreateProperties(type, memberSerialization).Where(a=>!this._excludedProperties.Contains(a.PropertyName)).ToList(); } } JsonSerializerSettings settings = new JsonSerializerSettings(); settings.ContractResolver =new JsonContractResolver(new[] { "Sign" }); settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat; settings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; var str = JsonConvert.SerializeObject(new Person { Name = "test", Age = 18, BirthDay = new DateTime(2011, 1, 1) }, settings); Console.WriteLine(str);
过滤属性名为Sign的字段,输出结果:{"age":18,"BirthDay":"2011-01-01T00:00:00"}
8.枚举值的自定义格式化问题
默认枚举输出的是枚举的 值 {"E":1}
在属性上加上JsonConverter(typeof(StringEnumConverter))表示将枚举值转换成对应的字符串,StringEnumConverter是Newtonsoft.Json内置的转换类型
输出结果:{"E":"TestEnum1"}
9.自定义类型转换
需要扩展类JsonConverter类
10.全局序列化设置
JsonSerializerSettings settings = new JsonSerializerSettings(); settings.ContractResolver =new JsonContractResolver(new[] { "Sign" }); settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat; settings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; JsonConvert.DefaultSettings = () => settings;
指定JsonConvert.DefaultSettings
11.指定序列化时Key的处理方式:驼峰样式,默认样式(ContractResolver)
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var str = JsonConvert.SerializeObject(new Person { Name = "test", Age = 18, BirthDay = new DateTime(2011, 1, 1) },Formatting.Indented);
Formatting.Indented 格式化json字符串数据,锯齿状的
输出结果:
{
"sign": false,
"age": 18,
"birthDay": "2011-01-01T00:00:00"
}
12.序列化循环 引用及处理层数
对于关联表的 对象或列表都不会序列化出来
//设置循环引用,及引用类型序列化的层数。
//注:目前在 EF core中目前不支持延迟加载,无所谓循环引用了
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
settings.MaxDepth = 10; //设置序列化的最大层数
settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;//指定如何处理循环引用,None--不序列化,Error-抛出异常,Serialize--仍要序列化
本文参考文档:
https://blog.csdn.net/u011127019/article/details/72801033
Newtonsoft.Json 官方文档:https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm