using Newtonsoft.Json;
using System;

namespace IRaCIS.Core.Infrastructure.Extention
{
    public static class ObjectExtension
    {

		public static T Clone<T>(this T source)
		{
			// Don't serialize a null object, simply return the default for that object
			if (Object.ReferenceEquals(source, null))
			{
				return default(T);
			}

			var deserializeSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace };
			var serializeSettings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
			return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source, serializeSettings), deserializeSettings);
		}

		/// <summary>
		/// 将对象序列化成稽查需要的Json字符串
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public static string ToJsonStr(this object obj)
		{
		
			return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", ReferenceLoopHandling = ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Ignore });
		}

		/// <summary>
		/// 将对象序列化成稽查需要的Json字符串
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public static string ToJsonNotIgnoreNull(this object obj)
		{

			return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", ReferenceLoopHandling = ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Include });
		}
	}
}
