From 4a32a80583e9485f74e76290fff1a9873273f287 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 13 Apr 2019 19:12:51 +0800 Subject: [PATCH] Support Column No Serialize --- .../Entities/Mapping/SugarMappingAttribute.cs | 6 ++++ .../IntegrationServices/SerializeService.cs | 35 +++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs b/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs index be5fbbc28..e73f635d0 100644 --- a/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs +++ b/Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs @@ -128,6 +128,12 @@ namespace SqlSugar set { _IsTranscoding = value; } } + private bool _NoSerialize; + public bool NoSerialize + { + get { return _NoSerialize; } + set { _NoSerialize = value; } + } } } diff --git a/Src/Asp.Net/SqlSugar/IntegrationServices/SerializeService.cs b/Src/Asp.Net/SqlSugar/IntegrationServices/SerializeService.cs index cf6ec0a46..04cf77aa4 100644 --- a/Src/Asp.Net/SqlSugar/IntegrationServices/SerializeService.cs +++ b/Src/Asp.Net/SqlSugar/IntegrationServices/SerializeService.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; using System; using System.Collections.Generic; using System.Linq; @@ -6,17 +7,45 @@ using System.Text; namespace SqlSugar { - public class SerializeService:ISerializeService + public class SerializeService : ISerializeService { public string SerializeObject(object value) { - return JsonConvert.SerializeObject(value); + return JsonConvert.SerializeObject(value, new JsonSerializerSettings() + { + ContractResolver = new MyContractResolver() + }); } public T DeserializeObject(string value) { - var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; + var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new MyContractResolver() }; return JsonConvert.DeserializeObject(value, jSetting); } } + public class MyContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver + { + + + public MyContractResolver() + { + + } + + protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) + { + var list = type.GetProperties() + .Where(x => !x.GetCustomAttributes(true).Any(a => (a is SugarColumn) && ((SugarColumn)a).NoSerialize == true)) + .Select(p => new JsonProperty() + { + PropertyName = p.Name, + PropertyType = p.PropertyType, + Readable = true, + Writable = true, + ValueProvider = base.CreateMemberValueProvider(p) + }).ToList(); + + return list; + } + } }