chomp 6e4c7abf73 dump-processor-cleanup (#6)
Co-authored-by: Alex <alex@dm-me-for-questions.com>
Reviewed-on: SPT-AKI/LootDumpProcessor#6
2024-04-16 18:29:40 +00:00

23 lines
860 B
C#

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