83 lines
2.2 KiB
C#
83 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
namespace ServerBase;
|
|
|
|
public class JsonText : IFormatter
|
|
{
|
|
private static readonly JsonSerializerSettings m_serialize_settings = new JsonSerializerSettings();
|
|
private static readonly ContractResolver m_resolver = new ContractResolver();
|
|
|
|
public JsonText()
|
|
{
|
|
m_serialize_settings.ContractResolver = m_resolver;
|
|
m_serialize_settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
|
|
}
|
|
|
|
public override string toLogString(BusinessLog log)
|
|
{
|
|
return JsonConvert.SerializeObject(log, m_serialize_settings);
|
|
}
|
|
|
|
|
|
//=============================================================================================
|
|
// Json을 만들때 무시할 property를 등록한다.
|
|
//=============================================================================================
|
|
private class ContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
|
|
{
|
|
private readonly Dictionary<Type, HashSet<string>> m_ignores;
|
|
|
|
public ContractResolver()
|
|
{
|
|
m_ignores = new Dictionary<Type, HashSet<string>>();
|
|
}
|
|
|
|
public void ignoreProperty(Type type, params string[] jsonPropertyNames)
|
|
{
|
|
if (false == m_ignores.ContainsKey(type))
|
|
{
|
|
m_ignores[type] = new HashSet<string>();
|
|
}
|
|
|
|
foreach (var prop in jsonPropertyNames)
|
|
{
|
|
m_ignores[type].Add(prop);
|
|
}
|
|
}
|
|
|
|
public bool isIgnored(Type type, string jsonPropertyName)
|
|
{
|
|
if (false == m_ignores.ContainsKey(type))
|
|
{
|
|
return false;
|
|
}
|
|
return m_ignores[type].Contains(jsonPropertyName);
|
|
}
|
|
|
|
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
|
{
|
|
var property = base.CreateProperty(member, memberSerialization);
|
|
|
|
if (property.DeclaringType != null &&
|
|
property.PropertyName != null &&
|
|
isIgnored(property.DeclaringType, property.PropertyName))
|
|
{
|
|
property.ShouldSerialize = i => false;
|
|
property.Ignored = true;
|
|
}
|
|
return property;
|
|
}
|
|
|
|
}//ContractResolver
|
|
|
|
}//JsonText
|