37 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			C#
		
	
	
| using Newtonsoft.Json;
 | |
| using Newtonsoft.Json.Serialization;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| 
 | |
| namespace IRaCIS.Core.SCP
 | |
| {
 | |
|     public class NullToEmptyStringResolver : DefaultContractResolver
 | |
|     {
 | |
| 
 | |
| 
 | |
|         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;
 | |
|         }
 | |
| 
 | |
| 
 | |
|     }
 | |
| }
 |