69 lines
2.1 KiB
C#
69 lines
2.1 KiB
C#
using IRaCIS.Core.Infrastructure.NewtonsoftJson;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
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)
|
|
{
|
|
// 检查类是否有 LowerCamelCaseJsonAttribute 标记 有的话,属性名小写
|
|
if (type.GetCustomAttribute<LowerCamelCaseJsonAttribute>() != null)
|
|
{
|
|
base.NamingStrategy = new LowerCamelCaseNamingStrategy();
|
|
}
|
|
else
|
|
{
|
|
base.NamingStrategy = null;
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
}
|
|
}
|