首页 > 编程语言 >C# 反序列化,支持基本数据类型处理

C# 反序列化,支持基本数据类型处理

时间:2022-12-13 20:11:07浏览次数:41  
标签:Convert IsAssignableFrom return C# object 数据类型 json typeof 序列化

C# 反序列化,支持基本数据类型处理

代码

        /// <summary>
        /// 优化对基本数据的支持
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="formatting"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static T Parse<T>(string json, JsonSerializerSettings settings = null)
        {
            if (typeof(T) == typeof(string))
            {
                return (T)(object)json;
            }
            if (string.IsNullOrWhiteSpace(json) || "null".Equals(json, StringComparison.OrdinalIgnoreCase))
            {
                return default(T);
            }
            //if(typeof(IConvertible).IsAssignableFrom(typeof(T)))
            if(typeof(DateTime).IsAssignableFrom( typeof(T))) {
                return (T)(object)Convert.ToDateTime(json);
            }
            if(typeof(int).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToInt32(json);
            }
            if(typeof(long).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToInt64(json);
            }
            if(typeof(float).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToSingle(json);
            }
            if(typeof(double).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToDouble(json);
            }
            if(typeof(decimal).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToDecimal(json);
            }
            if(typeof(bool).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToBoolean(json);
            }
            if(typeof(byte).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToByte(json);
            }
            if(typeof(char).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToChar(json);
            }
            if(typeof(ushort).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToUInt16(json);
            }
            if(typeof(uint).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToUInt32(json);
            }
            if(typeof(ulong).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToUInt64(json);
            }
            if(typeof(sbyte).IsAssignableFrom(typeof(T))) {
                return (T)(object)Convert.ToSByte(json);
            }
            if (settings == null)
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            return JsonConvert.DeserializeObject<T>(json, settings);
        }

        /// <summary>
        /// 优化对字符串的支持
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="formatting"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static string ToJsonString(object obj, Formatting formatting = Formatting.None, JsonSerializerSettings settings = null)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            if (obj is string)
            {
                return obj as string;
            }
            if (settings == null)
            {
                return JsonConvert.SerializeObject(obj, formatting);
            }
            return JsonConvert.SerializeObject(obj, formatting, settings);
        }

标签:Convert,IsAssignableFrom,return,C#,object,数据类型,json,typeof,序列化
From: https://www.cnblogs.com/muphy/p/16980508.html

相关文章