feat(DateTimeConverter): add to handle custom date time formatter http response deserilization
This commit is contained in:
parent
db3137ef9d
commit
6b2c897e5b
26
src/Leanetec.EConnect.Infrastructure/DateTimeConverter.cs
Normal file
26
src/Leanetec.EConnect.Infrastructure/DateTimeConverter.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user