2024-04-16 18:29:40 +00:00
|
|
|
using System.Globalization;
|
2023-08-13 17:26:49 +01:00
|
|
|
using System.Text.Json;
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
|
|
namespace LootDumpProcessor.Serializers.Json.Converters;
|
|
|
|
|
|
|
|
public class NetDateTimeConverter : JsonConverter<DateTime>
|
|
|
|
{
|
2024-04-16 18:29:40 +00:00
|
|
|
private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
2023-08-13 17:26:49 +01:00
|
|
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
|
|
{
|
|
|
|
var stringDate = reader.GetString() ?? "";
|
2024-04-16 18:29:40 +00:00
|
|
|
if (!DateTime.TryParseExact(stringDate, DateTimeFormat, null, DateTimeStyles.None, out var parsedDate))
|
|
|
|
throw new Exception($"Invalid value for DateTime format: {DateTimeFormat}");
|
2023-08-13 17:26:49 +01:00
|
|
|
return parsedDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|
|
|
{
|
2024-04-16 18:29:40 +00:00
|
|
|
writer.WriteStringValue(value.ToString(DateTimeFormat));
|
2023-08-13 17:26:49 +01:00
|
|
|
}
|
|
|
|
}
|