using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushPlatform.Common.Util
{
    public class JsonUtil
    {
        public static string ConvertToJson<T>(T data)
        {
            return JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 格式化时间格式的json序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="dateTimeFormat">时间格式</param>
        /// <returns></returns>
        public static string ConvertToJsonEx<T>(T data, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss")
        {
            JsonSerializerSettings jsSettings = new JsonSerializerSettings();
            jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            jsSettings.Converters.Add(new IsoDateTimeConverter
            {
                DateTimeFormat = dateTimeFormat
            });
            return JsonConvert.SerializeObject(data, jsSettings);
        }

        public static T ConvertToObject<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }


    }
}
