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

namespace Solar.Api.Converters;

public class TimeOnlyConverter : JsonConverter<TimeOnly>
{
    private const string serializationFormat = "HH:mm";

    public override TimeOnly Read(
        ref Utf8JsonReader reader,
        Type typeToConvert,
        JsonSerializerOptions options)
    {
        var value = reader.GetString();
        return TimeOnly.ParseExact(value!, serializationFormat);
    }

    public override void Write(
        Utf8JsonWriter writer,
        TimeOnly value,
        JsonSerializerOptions options)
        => writer.WriteStringValue(value.ToString(serializationFormat));
}