2024-04-16 18:29:40 +00:00
|
|
|
using System.Globalization;
|
2023-08-13 17:26:49 +01:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
namespace LootDumpProcessor.Serializers.Json.Converters;
|
|
|
|
|
|
|
|
public class NewtonsoftDateTimeConverter : 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 void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer)
|
|
|
|
{
|
2024-04-16 18:29:40 +00:00
|
|
|
writer.WriteValue(value.ToString(DateTimeFormat));
|
2023-08-13 17:26:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override DateTime ReadJson(
|
|
|
|
JsonReader reader,
|
|
|
|
Type objectType,
|
|
|
|
DateTime existingValue,
|
|
|
|
bool hasExistingValue,
|
|
|
|
JsonSerializer serializer
|
|
|
|
)
|
|
|
|
{
|
|
|
|
var stringDate = reader.Value?.ToString() ?? "";
|
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;
|
|
|
|
}
|
|
|
|
}
|