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

namespace detect.gui.Converters;

public class JsonDateTimeConverter : JsonConverter<DateTime>
{
    private readonly string _dateTimeFormat;

    public JsonDateTimeConverter(string dateTimeFormat)
    {
        _dateTimeFormat = dateTimeFormat;
    }

    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        // 反序列化逻辑（如果需要）
        return DateTime.Parse(reader.GetString() ?? throw new InvalidOperationException());
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString(_dateTimeFormat));
    }
}