• 自定义Json序列化


       public class ObjectConverter<T> : JsonConverter
        {
            public override bool CanConvert(Type objectType)
            {
                return true;
            }
    
            public override bool CanWrite
            {
                get
                {
                    var type = typeof(T);
                    return type.IsClass;
                }
            }
    
            public override bool CanRead
            {
                get
                {
                    return true;
                }
            }
    
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                throw new NotImplementedException("Unnecessary because CanRead is false.The type will skip the converter.");
            }
    
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                JObject containerObj = new JObject();
                var properties = value.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                string enumPropStrName = string.Empty;
                List<string> enumPropNames = new List<string>();
                foreach (var prop in properties)
                {
                    var val = prop.GetValue(value);
                    if (prop.PropertyType.IsEnum || (prop.PropertyType.IsGenericType && prop.PropertyType.GenericTypeArguments[0].IsEnum))
                    {
                        if (properties.Count(x => string.Compare(x.Name, (prop.Name + "Str"), true) == 0) > 0)
                        {
                            enumPropStrName = prop.Name + "Str";
                            var first = properties.First(x => string.Compare(x.Name, enumPropStrName, true) == 0);
                            if (containerObj.Properties().Count(x => x.Name == enumPropStrName) <= 0)
                            {
                                containerObj.Add(enumPropStrName, val.GetEnumDescription());
                            }
                            else
                            {
                                containerObj[enumPropStrName] = val.GetEnumDescription();
                            }
                            enumPropNames.Add(enumPropStrName);
                        }
                        else if (properties.Count(x => string.Compare(x.Name, (prop.Name + "Desc"), true) == 0) > 0)
                        {
                            enumPropStrName = prop.Name + "Desc";
                            var first = properties.First(x => string.Compare(x.Name, enumPropStrName, true) == 0);
                            if (containerObj.Properties().Count(x => x.Name == enumPropStrName) <= 0)
                            {
                                containerObj.Add(enumPropStrName, val.GetEnumDescription());
                            }
                            else
                            {
                                containerObj[enumPropStrName] = val.GetEnumDescription();
                            }
                            enumPropNames.Add(enumPropStrName);
                        }
                    }
                    else if (prop.PropertyType.IsClass && prop.PropertyType != typeof(string))
                    {
                        JToken itemObj = null;
                        if (val.GetType().GetInterfaces()?.Count(x => x == typeof(IEnumerable)) > 0)
                        {
                            itemObj = JArray.FromObject(val);
                        }
                        else
                        {
                            itemObj = JObject.FromObject(val);
                        }
    
                        if (containerObj.Properties().Count(x => x.Name == prop.Name) <= 0)
                        {
                            containerObj.Add(prop.Name, itemObj);
                        }
                        else
                        {
                            containerObj[prop.Name] = itemObj;
                        }
                    }
                    else
                    {
                        var attr = prop.GetCustomAttribute<PropertySerilizerAttribute>();
                        if (attr != null)
                        {
                            val = attr.Convert(val, value);
                        }
    
                        JToken jVal = null;
                        if (val != null)
                        {
                            jVal = JToken.FromObject(val);
                        }
    
                        if (!enumPropNames.Contains(prop.Name))
                        {
                            if (containerObj.Properties().Count(x => x.Name == prop.Name) <= 0)
                            {
                                containerObj.Add(prop.Name, jVal);
                            }
                            else
                            {
                                containerObj[prop.Name] = jVal;
                            }
                        }
                    }
                }
                containerObj.WriteTo(writer);
            }
        }
    
        public abstract class PropertyConverter
        {
            public abstract object Convert(object source, object obj);
        }
    
        public class DatePropertyConverter : PropertyConverter
        {
            public override object Convert(object source, object obj)
            {
                if (source != null && (source.GetType() == typeof(DateTime) || source.GetType() == typeof(DateTime?)))
                {
                    return ((DateTime)source).ToString("yyyy-MM-dd");
                }
                return null;
            }
        }
    
        public class DateTimePropertyConverter : PropertyConverter
        {
            public override object Convert(object source, object obj)
            {
                if (source != null && (source.GetType() == typeof(DateTime) || source.GetType() == typeof(DateTime?)))
                {
                    return ((DateTime)source).ToString("yyyy-MM-dd HH:mm:ss");
                }
                return null;
            }
        }
    
        /// <summary>
        /// 属性序列化
        /// </summary>
        [AttributeUsage(AttributeTargets.Property)]
        public class PropertySerilizerAttribute : Attribute
        {
            protected PropertyConverter Converter { get; set; }
            public PropertySerilizerAttribute(Type type)
            {
                if (type == typeof(PropertyConverter) || type?.BaseType == typeof(PropertyConverter))
                {
                    Converter = (PropertyConverter)Activator.CreateInstance(type);
                }
            }
    
            public object Convert(object source, object obj)
            {
                return Converter?.Convert(source, obj);
            }
        }
  • 相关阅读:
    浅入深出Vue:数据绑定
    浅入深出Vue:数据渲染
    浅入深出Vue:环境搭建
    浅入深出Vue:工具准备之PostMan安装配置及Mock服务配置
    浅入深出Vue:工具准备之WebStorm安装配置
    【重点突破】—— react使用http-proxy-middleware反向代理跨域
    【重点突破】—— 工作中git分支的基本使用
    【营销小程序】—— webview嵌套web端项目(原生开发支付功能)
    【营销App】—— react/ant design mobile项目爬坑
    【重点突破】—— UniApp微信小程序开发教程学习Three
  • 原文地址:https://www.cnblogs.com/yiyanwei/p/16304823.html
Copyright © 2020-2023  润新知