A System.Text.Json converter that deserializes a Unix timestamp in milliseconds into a DateTime. It reads an Int64 from the JSON reader, interprets it as milliseconds since Unix epoch, and returns the corresponding DateTime.
#nullable enable annotations
using System.Text.Json;
using System.Text.Json.Serialization;
namespace LichessNET.Converters;
public class MillisecondUnixConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var milliseconds = reader.GetInt64();
DateTime unixEpoch = DateTime.UnixEpoch;
return unixEpoch.Add(TimeSpan.FromMilliseconds(milliseconds));
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}