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

namespace WebUI.LIB
{
    /// <summary>
    /// 自定义 时间类型    json 
    /// </summary>
    public class DateTimeStr : JsonConverter<DateTime>
    {
        /// <summary>
        /// 格式化时间格式
        /// </summary>
        public string DateTimeFormat { get; set; } = "yyyy-MM-dd HH:mm:ss";

        /// <summary>
        /// 读取 父类方法
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="typeToConvert"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => DateTime.Parse(reader.GetString());
       /// <summary>
       /// 写入 格式化
       /// </summary>
       /// <param name="writer"></param>
       /// <param name="value"></param>
       /// <param name="options"></param>
        public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) => writer.WriteStringValue(value.ToString(this.DateTimeFormat));
    }
}
