using System;
using Newtonsoft.Json;

namespace SGModule.Common.Helper {
    public static class SerializeHelper {
        private static readonly JsonSerializerSettings _defaultUseJsonSettings = new() {
            Formatting = Formatting.None,
            DateFormatString = "yyyy/MM/dd HH:mm:ss"
        };

        private static readonly JsonSerializerSettings _jsonIndentedSettings = new() {
            Formatting = Formatting.Indented,
            DateFormatString = "yyyy/MM/dd HH:mm:ss"
        };

        static SerializeHelper() {
            JsonConvert.DefaultSettings = () => _defaultUseJsonSettings;
        }

        public static string ToJson(object obj) {
            return JsonConvert.SerializeObject(obj, _defaultUseJsonSettings);
        }

        public static string ToJsonIndented(object obj) {
            return JsonConvert.SerializeObject(obj, _jsonIndentedSettings);
        }

        public static string ToJson(object obj, Type type) {
            return JsonConvert.SerializeObject(obj, type, _defaultUseJsonSettings);
        }

        public static string ToJson<T>(object obj) {
            return ToJson(obj, typeof(T));
        }

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