55 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
| using Newtonsoft.Json;
 | |
| using Newtonsoft.Json.Serialization;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| 
 | |
| namespace IRaCIS.Core.API
 | |
| {
 | |
|     public class NullToEmptyStringResolver : DefaultContractResolver
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// 创建属性
 | |
|         /// </summary>
 | |
|         /// <param name="type">类型</param>
 | |
|         /// <param name="memberSerialization">序列化成员</param>
 | |
|         /// <returns></returns>
 | |
|         //protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
 | |
|         //{
 | |
|         //    IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
 | |
| 
 | |
| 
 | |
|         //    foreach (var jsonProperty in properties)
 | |
|         //    {
 | |
|         //        jsonProperty.DefaultValue = new NullToEmptyStringValueProvider(jsonProperty);
 | |
|         //    }
 | |
| 
 | |
|         //    return properties;
 | |
| 
 | |
|         //}
 | |
| 
 | |
|         protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
 | |
|         {
 | |
|             IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
 | |
| 
 | |
|             var list= type.GetProperties()
 | |
|                 .Select(p =>
 | |
|                 {
 | |
|                     var jp = base.CreateProperty(p, memberSerialization);
 | |
|                     jp.ValueProvider = new NullToEmptyStringValueProvider(p);
 | |
|                     return jp;
 | |
|                 }).ToList();
 | |
| 
 | |
|             var uu = list.Select(t => t.PropertyName).ToList();
 | |
| 
 | |
|             //获取复杂对象属性
 | |
|           properties =   properties.TakeWhile(t => !uu.Contains(t.PropertyName)).ToList();
 | |
| 
 | |
|           list.AddRange(properties);
 | |
|           return list;
 | |
|         }
 | |
| 
 | |
| 
 | |
|     }
 | |
| }
 |