using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

namespace StandardScene.Utils
{
    public static class JsonTool
    {
        private static readonly JsonSerializerSettings DefaultJsonSerializerSettings = new JsonSerializerSettings()
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
            DateFormatString = "yyyy-MM-dd HH:mm:ss"
        };
        public static string ToJson(this object obj)
        {
            return JsonConvert.SerializeObject(obj, DefaultJsonSerializerSettings);
        }
        public static string ToJson(this object obj, JsonSerializerSettings jsonSerializerSettings)
        {
            return JsonConvert.SerializeObject(obj, jsonSerializerSettings);
        }
        public static T JsonTo<T>(this string Json)
        {
            return JsonConvert.DeserializeObject<T>(Json);
        }
        public static object JsonToObject(this string Json)
        {
            return JsonConvert.DeserializeObject(Json);
        }
        public static JObject JsonToJObject(this string Json)
        {
            return JObject.Parse(Json);
        }
    }
}
