.netcore Json 序列化

时间:2025-02-23 08:41:40
using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;


namespace
{
    public static class JsonOperator
    {
        /// <summary>
        /// Json 序列化
        /// </summary>
        /// <param name="value"></param>
        /// <param name="converters"></param>
        /// <returns></returns>
        public static string JsonSerialize(object value, params JsonConverter[] converters)
        {
            if (value != null)
            {
                if (converters != null && > 0)
                {
                    return (value, converters);
                }
                else
                {
                    if (value is DataSet)
                        return (value, new DataSetConverter());
                    else if (value is DataTable)
                        return (value, new DataTableConverter());
                    else
                        return (value);
                }
            }
            return ;
        }




        /// <summary>
        /// Json反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="converters"></param>
        /// <returns></returns>
        public static T JsonDeserialize<T>(string value, params JsonConverter[] converters)
        {
            if ((value))
                return default(T);


            if (converters != null && > 0)
            {
                return <T>(value, converters);
            }
            else
            {
                Type type = typeof(T);


                if (type == typeof(DataSet))
                {
                    return <T>(value, new DataSetConverter());
                }
                else if (type == typeof(DataTable))
                {
                    return <T>(value, new DataTableConverter());
                }
                return <T>(value);
            }
        }




    }
}