JsonIgnoreAttribute,ErrorHandlingAttribute,DefaultValueAttribute

一、使用JsonIgnoreAttribute忽略其属性序列化

1.创建一个Movie对象,并在其属性上添加JsonIgnore.

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using GongHuiNewtonsoft.Json;  
  6.   
  7. namespace JSONDemo  
  8. {      
  9.     public class Movie  
  10.     {  
  11.         public string Name { getset; }  
  12.   
  13.         public string Director { getset; }  
  14.   
  15.         [JsonIgnore]  
  16.         public DateTime? LaunchDate { getset; }  
  17.     }  
  18. }  


2.实例化对象Movie,并给其忽略的属性赋值。

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json;  
  7. using GongHuiNewtonsoft.Json.Serialization;  
  8. using GongHuiNewtonsoft.Json.Converters;  
  9.   
  10. namespace JSONDemo  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Movie m = new Movie  
  17.             {  
  18.                 Name = "爱情呼叫转移",  
  19.                 Director = "张建亚",  
  20.                 LaunchDate=DateTime.Today  
  21.             };  
  22.   
  23.             string json = JsonConvert.SerializeObject(m, Formatting.Indented);  
  24.             Console.WriteLine(json);  
  25.         }  
  26.     }  
  27. }  


3.运行结果

 

二、使用ErrorHandlingAttribute忽略其属性异常

1.先创建一个Employee对象,必须得在属性Roles中get中添加抛出异常,然后用内部方法OnError,忽略其异常

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using GongHuiNewtonsoft.Json.Serialization;  
  6. using System.Runtime.Serialization;  
  7.   
  8. namespace JSONDemo  
  9. {  
  10.     public class Employee  
  11.     {  
  12.         private List<string> _roles;  
  13.   
  14.         public string Name { getset; }  
  15.         public int Age { getset; }  
  16.   
  17.         public List<string> Roles  
  18.         {  
  19.             get  
  20.             {  
  21.                 if (_roles == null)  
  22.                     throw new Exception("Roles not loaded!");  
  23.   
  24.                 return _roles;  
  25.             }  
  26.             set { _roles = value; }  
  27.         }  
  28.   
  29.         [OnError]  
  30.         internal void OnError(StreamingContext context, ErrorContext error)  
  31.         {  
  32.             error.Handled = true;  
  33.         }  
  34.     }  
  35. }  


2.调用代码,先实例化对象Employee,设置Roles属性为空,引发异常.这里,因为用了OnError方法,所以其异常忽略.

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json;  
  7. using GongHuiNewtonsoft.Json.Serialization;  
  8. using GongHuiNewtonsoft.Json.Converters;  
  9.   
  10. namespace JSONDemo  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Employee employee = new Employee  
  17.             {  
  18.                 Name = "GongHui",  
  19.                 Age = 28,  
  20.                 Roles = null  
  21.             };  
  22.   
  23.             string json = JsonConvert.SerializeObject(employee, Formatting.Indented);  
  24.             Console.WriteLine(json);  
  25.         }  
  26.     }  
  27. }  


3.运行的结果

 

 

三、使用DefaultValueAttribute设置属性默认值

1.先创建一个Address类,然后引用System.ComponentModel,设置属性的默认值.

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ComponentModel;  
  6.   
  7. namespace JSONDemo  
  8. {  
  9.     public class Address  
  10.     {  
  11.         public string Province { getset; }  
  12.         public string City { getset; }  
  13.         public string County { getset; }  
  14.   
  15.         [DefaultValue("GuangZhou")]  
  16.         public string DetailAddress  
  17.         {  
  18.            getreturn County + " " + City + " " + Province; }              
  19.         }  
  20.     }  
  21. }  


2.实例化Address对象,

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6. using GongHuiNewtonsoft.Json;  
  7. using GongHuiNewtonsoft.Json.Serialization;  
  8. using GongHuiNewtonsoft.Json.Converters;  
  9.   
  10. namespace JSONDemo  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             Address address = new Address();  
  17.   
  18.             Console.WriteLine("----------------显示所有序列化成员---------------");  
  19.             string json = JsonConvert.SerializeObject(address, Formatting.Indented);  
  20.             Console.WriteLine(json);  
  21.   
  22.             Console.WriteLine("----------------仅显示添加默认值的成员-------------");  
  23.             string json1 = JsonConvert.SerializeObject(address, Formatting.Indented, new JsonSerializerSettings  
  24.             {  
  25.                 DefaultValueHandling = DefaultValueHandling.Ignore  
  26.             });  
  27.             Console.WriteLine(json1);  
  28.         }  
  29.     }  
  30. }  


3.运行的结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值