C# Newtonsoft.Json JsonSerializerSettings配置
2021-03-05 00:28
标签:技术 view 分享 ffd col mode indent 延迟加载 ror JsonSerializerSettings常用配置整理 1.忽略某些属性 MemberSerialization.OptIn 默认情况下,所有的成员不会被序列化,类中的成员只有标有特性JsonProperty的才会被序列化,当类的成员很多,但客户端仅仅需要一部分数据时,很有用 输出结果: {"Age":18} MemberSerialization.OptOut 默认类中所有公有成员会被序列化,如果不想被序列化,可以用特性JsonIgnore 将上面的代码OptIn改为OptOut输出结果: {"Age":18,"BirthDay":"2011-01-01T00:00:00"} 2.默认值的处理 DefaultValueHandling.Ignore 序列化和反序列化时,忽略默认值 输出结果: {"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:添加特性,指定转换格式 输出结果:{"BirthDay":"2011-01-01"} 解决方案2:指定JsonSerializerSettings.DateFormatString的值 输出结果:{"BirthDay":"2011-01-01 00:00:00"} 同时指定时以特性(即方案1)为准 解决方案3: object model = new object(); var jsonSettings = new JsonSerializerSettings(); jsonSettings.DateFormatHandling = DatFormatHandling.IsoDateFormat; jsonSettings.Converters.Add(new Newtonsoft.Json.Converters.IsoDateTimeConverter () {DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"}); string content = JsonConvert.SerializeObject (model,Formatting.Indented,jsonSettings); 6.自定义序列化的字段名称 实体中定义的属性名可能不是自己想要的名称,但是又不能更改实体定义,这个时候可以自定义序列化字段名称 [JsonProperty(PropertyName = "age")] 输出结果:{"age":18} 7.动态决定属性是否序列化 指定JsonSerializerSettings.ContractResolver的实例,继承DefaultContractResolver类,重写方法 过滤属性名为Sign的字段,输出结果:{"age":18,"BirthDay":"2011-01-01T00:00:00"} 8.枚举值的自定义格式化问题 默认枚举输出的是枚举的 值 {"E":1} 在属性上加上JsonConverter(typeof(StringEnumConverter))表示将枚举值转换成对应的字符串,StringEnumConverter是Newtonsoft.Json内置的转换类型 输出结果:{"E":"TestEnum1"} 9.自定义类型转换 需要扩展类JsonConverter类 10.全局序列化设置 指定JsonConvert.DefaultSettings 11.指定序列化时Key的处理方式:驼峰样式,默认样式(ContractResolver) JsonSerializerSettings settings = new JsonSerializerSettings(); var str = JsonConvert.SerializeObject(new Person { Name = "test", Age = 18, BirthDay = new DateTime(2011, 1, 1) },Formatting.Indented); Formatting.Indented 格式化json字符串数据,锯齿状的 输出结果: { 12.序列化循环 引用及处理层数 对于关联表的 对象或列表都不会序列化出来 //设置循环引用,及引用类型序列化的层数。 本文参考文档: https://blog.csdn.net/u011127019/article/details/72801033 Newtonsoft.Json 官方文档:https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm C# Newtonsoft.Json JsonSerializerSettings配置 标签:技术 view 分享 ffd col mode indent 延迟加载 ror 原文地址:https://www.cnblogs.com/net-sky/p/12910009.html 1 [JsonObject(MemberSerialization.OptIn)]
2 public class TestClass
3 {
4 public int A { get; set; }
5 public long B { get; set; }
6 public string C { get; set; }
7 [JsonProperty]
8 public DateTime D { get; set; }
9 public TestEnum E { get; set; }
10 [JsonIgnore]
11 public TestClass F { get; set; }
12 public List
1 Console.WriteLine(JsonConvert.SerializeObject(new Person { Name = "test", Age = 18, BirthDay = new DateTime(2011, 1, 1) }));
2 TestClass testClass = new TestClass
3 {
4 A = 1,
5 B = 2,
6 C = "C",
7 D = DateTime.Now,
8 E = TestEnum.TestEnum1,
9 F = new TestClass
10 {
11 A = 11,
12 B = 22,
13 C = "Cc",
14 D = DateTime.Now,
15 E = TestEnum.TestEnum2,
16 F = new TestClass()
17 },
18 G = new List
{"D":"2019-11-26T10:37:33.7655514+08: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} 1 TestClass testClass = new TestClass
2 {
3 A = 1,
4 B = 2,
5 C = "C",
6 D = DateTime.Now,
7 E = TestEnum.TestEnum1,
8 F = new TestClass
9 {
10 A = 11,
11 B = 22,
12 C = "Cc",
13 D = DateTime.Now,
14 E = TestEnum.TestEnum2,
15 F = new TestClass()
16 },
17 G = new List
1 public class LongDateTimeConvert: Newtonsoft.Json.Converters.IsoDateTimeConverter
2 {
3 public LongDateTimeConvert() : base()
4 {
5 base.DateTimeFormat = "yyyy-MM-dd";
6 }
7 }
8 //[JsonConverter(typeof(LongDateTimeConvert))]
9 public DateTime BirthDay { get; set; }
1 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);
public int Age { get; set; } 1 public class JsonContractResolver : DefaultContractResolver
2 {
3 protected IEnumerable
1 JsonSerializerSettings settings = new JsonSerializerSettings();
2 settings.ContractResolver =new JsonContractResolver(new[] { "Sign" });
3 settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
4 settings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
5 JsonConvert.DefaultSettings = () => settings;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
"sign": false,
"age": 18,
"birthDay": "2011-01-01T00:00:00"
}
//注:目前在 EF core中目前不支持延迟加载,无所谓循环引用了
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
settings.MaxDepth = 10; //设置序列化的最大层数
settings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;//指定如何处理循环引用,None--不序列化,Error-抛出异常,Serialize--仍要序列化
文章标题:C# Newtonsoft.Json JsonSerializerSettings配置
文章链接:http://soscw.com/index.php/essay/60228.html