using Newtonsoft.Json.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OASystem.API.OAMethodLib
{

    #region 日期格式转换

    public class DateTimeJsonConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
    {
        private readonly string Format;
        public DateTimeJsonConverter(string format)
        {
            Format = format;
        }

        public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (string.IsNullOrEmpty(reader.GetString()))
            {
                return default(DateTime);
            }

            return DateTime.ParseExact(reader.GetString(), Format, null);
        }

        public override void Write(Utf8JsonWriter writer, DateTime date, JsonSerializerOptions options)
        {
            writer.WriteStringValue(date.ToString(Format));
        }
        //public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        //{
        //    return DateTime.ParseExact(reader.GetString(), Format, null);
        //}
    }
    #endregion

    #region String null值转换(读/写)


    public class NullJsonConverter : System.Text.Json.Serialization.JsonConverter<string>
    {
        public override bool HandleNull => true;

        public override string Read(
            ref Utf8JsonReader reader,
            Type typeToConvert,
            JsonSerializerOptions options) =>
            reader.GetString() ?? "";

        public override void Write(
            Utf8JsonWriter writer,
            string value,
            JsonSerializerOptions options) =>
            writer.WriteStringValue(value ?? "");

    }

    #endregion


    #region decimal格式化

    ///// <summary>
    ///// decimal格式化
    ///// </summary>
    //public class JsonConverterDecimal : Newtonsoft.Json.JsonConverter
    //{
    //    /// <summary>
    //    /// 是否可以转换
    //    /// </summary>
    //    /// <param name="objectType"></param>
    //    /// <returns></returns>
    //    public override bool CanConvert(Type objectType)
    //    {
    //        return objectType == typeof(decimal) || objectType == typeof(decimal?);
    //    }

    //    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
    //    {
    //        if (reader.ValueType == null && reader.Value == null)

    //        {
    //            return null;
    //        }
    //        else
    //        {
    //            decimal.TryParse(reader.Value != null ? reader.Value.ToString() : "", out decimal value);
    //            return value.ToString("F2");
    //        }

    //    }

    //    public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
    //    {
    //        if (value == null)

    //            writer.WriteValue(value);
    //        else
    //            writer.WriteValue(value + "");
    //    }
    //}

    #endregion

}