feat(DateTimeConverter): add to handle custom date time formatter http response deserilization

This commit is contained in:
tekh 2025-08-15 13:49:37 +02:00
parent db3137ef9d
commit 6b2c897e5b

View File

@ -0,0 +1,26 @@
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Leanetec.EConnect.Infrastructure;
public class DateTimeConverter : JsonConverter<DateTime>
{
private readonly string _format;
public DateTimeConverter(string format)
{
_format = format;
}
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();
return DateTime.ParseExact(value!, _format, CultureInfo.InvariantCulture);
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(_format));
}
}